yeap's profileyeap's spaceBlogGuestbookNetwork Tools Help

Blog


    May 29

    Talking about Perl tutorial: Split from http://www.comp.leeds.ac.uk/Perl/split.html

    Split

    A very useful function in Perl is split, which splits up a string and places it into an array. The function uses a regular expression and as usual works on the $_ variable unless otherwise specified.

    The split function is used like this:

    $info = "Caine:Michael:Actor:14, Leafy Drive";
    @personal = split(/:/, $info);
    

    which has the same overall effect as

    @personal = ("Caine", "Michael", "Actor", "14, Leafy Drive");
    

    If we have the information stored in the $_ variable then we can just use this instead

    @personal = split(/:/);
    

    If the fields are divided by any number of colons then we can use the RE codes to get round this. The code

    $_ = "Capes:Geoff::Shot putter:::Big Avenue";
    @personal = split(/:+/);
    

    is the same as

    @personal = ("Capes", "Geoff",
                 "Shot putter", "Big Avenue");
    

    But this:

    $_ = "Capes:Geoff::Shot putter:::Big Avenue";
    @personal = split(/:/);
    

    would be like

    @personal = ("Capes", "Geoff", "",
                 "Shot putter", "", "", "Big Avenue");
    

    A word can be split into characters, a sentence split into words and a paragraph split into sentences:

    @chars = split(//, $word);
    @words = split(/ /, $sentence);
    @sentences = split(/\./, $paragraph);
    

    In the first case the null string is matched between each character, and that is why the @chars array is an array of characters - ie an array of strings of length 1. 

     

     

    Exercise

    A useful tool in natural language processing is concordance. This allows a specific string to be displayed in its immediate context whereever it appears in a text. For example, a concordance program identifying the target string the might produce some of the following output. Notice how the occurrences of the target string line up vertically.

    discovered (this is the truth) that when he
    t kinds of metal to the leg of a frog, an e
    rrent developed and the frog's leg kicked,
     longer attached to the frog, which was dea
    normous advances in the field of amphibian
    ch it hop back into the pond -- almost.  Bu
    ond -- almost.  But the greatest Electrical
    ectrical Pioneer of them all was Thomas Edi
    

    This exercise is to write such a program. Here are some tips:

    • Read the entire file into array (this obviously isn't useful in general because the file may be extremely large, but we won't worry about that here). Each item in the array will be a line of the file.
    • When the chop function is used on an array it chops off the last character of every item in the array.
    • Recall that you can join the whole array together with a statement like $text = "@lines";
    • Use the target string as delimiter for splitting the text. (Ie, use the target string in place of the colon in our previous examples.) You should then have an array of all the strings between the target strings.
    • For each array element in turn, print it out, print the target string, and then print the next array element.
    • Recall that the last element of an array @food has index $#food.

    As it stands this would be a pretty good program, but the target strings won't line up vertically. To tidy up the strings you'll need the substr function. Here are three examples of its use.

    substr("Once upon a time", 3, 4);	# returns "e up"
    substr("Once upon a time", 7);		# returns "on a time"
    substr("Once upon a time", -6, 5);	# returns "a tim"
    

    The first example returns a substring of length 4 starting at position 3. Remember that the first character of a string has index 0. The second example shows that missing out the length gives the substring right to the end of the string The third example shows that you can also index from the end using a negative index. It returns the substring that starts at the 6th character from the end and has length 5.

    If you use a negative index that extends beyond the beginning of the string then Perl will return nothing or give a warning. To avoid this happening you can pad out the string by using the x operator mentioned earlier. The expression (" "x30) produces 30 spaces, for example.

     

    Perl tutorial: Split

    How to take the specific number of character in specific location of strings or numbers

    #!/usr/bin/perl

    $abc = "12345678"; #for this, it will take as string.but it take as digit by taken out the double quotes is also can work
    $A=0;
    $B =2;
    for($i = 0; $i < 4; $i ++ )
    {
    $array[$i] = substr($abc,$A,$B);
    $A+=2;
    }
    foreach $element(@array)
    {
    print"$element";
    print"\n";
    }

    Talking about Sebastian Bleasdale - Obfuscated Perl

      i found out the very interesting perl script at this website, http://www.chiark.greenend.org.uk/~sbleas/creative/obfuscatedperl/index.html. I extracted the scripts form from the website.

    Quote

    Sebastian Bleasdale - Obfuscated Perl
    The Perl Journal's obfuscated perl contest (which was unfortunately shut down before we could submit it). #!/usr/bin/perl -w$_=$0;s|.*\W||;sub K{s;[^a-z\n ];"/".ord($&)."j";ge;y; 0-9k-uyo/jv-xz;`ja-|;d;s\.\for($O=0,$o=-96+ord$&;$o>7-$O;$o-=7-$O++){}($o+=$O)+$O;exit}(?o?&&open O,"|-")?(y|_| |,print OK):do{s`.\..`'esg'&$&`eg,y,'|/EDd%G\\,50-{,,s,(.*\n)(.*\n?),$1|$2,eg,s;(.)"(.);chr 125-(7.5-($1>$2?$2:$1))**2/2+abs$2-$1;ge,y}`j-|} k-uy\bjv-xz},s!\n([ka-i]+)j!$_=$1;y;ka-{;0-};;chr!esg,print while$_=<>.<>};

     

    The next program we created was a perl implementation of Tetris, in under 640 bytes. You use the arrow keys to control the pieces, and space to drop them. It comes complete with higher score for scoring multiple lines and the ability to move your piece after you've dropped it using space.

    #!/usr/bin/perl
    `stty cbreak -echo`;END{`stty sane`}$;=0 .($p=" "x10)."0 \n";sub
    j{/$*/&&return,s/\S/0/g,$a=$_,s/0/\e[7m \e[m/g,s/^/$p/g,s/ / /g
    ,print"\e[;H$_"for$b|$k&$*x length$b;$a}sub n{$_=qw(@J@Eon@J@@J@
    dJbEOEHJa HJdEOEaJb @N@Mog@K@ @OEEOJ@@J EO@JoeJ@@ @Oo@oO)[rand
    7];$*=$|=s/.../````$&``````\n/g;s/n`/eE/;$k=$_;j}print"\e[H\e[J"
    ;$b=$;x20 .($;^"\cP"x12);for(n;@_=($k,$*);!$||++$i>6**6/(@ARGV?
    $ARGV[0]:1)?$_="B":do{(select$t=chr 1,"","",0)[0]||next;sysread
    STDIN,$_,1;$|^=/ /}){$*=1 if($*<<=/A/)>8;$i=$k=" $p \n$k"if/B/;
    $k=~s/(.*)`/`$1/g if/C/;$k=~s/`(.*)/$1`/g if/D/;j or($k,$*)=@_,
    /B/and$|++and$b=$;x(($b=j)=~s/0{12} \n//g).$b,n||exit}

    Sokoban

    One thing that I learnt when creating these programs was that the key part of any program was working out the correct data structure to encode the data in as minimalist a way as possible, and the next most important part was working out how to handle the data structure to get it to the state that you wanted it to be in. When creating a version of sokoban, David got around the first problem by using the level data from xsok. I thought this was cheating, and by dint of some creative hacking, created the following .sig sized version.

    #!/usr/bin/perl -w
    END{`stty sane`}`stty cbreak -echo`;$p=27;@b=split//,7x9 .60210x2 .61020602416 .
    7x9;for(print"\e[H\e[J";$_=join"",@b and/2/&s/./(" ",qw{:: [] XX <> <>},"#\n#",
    "#")[$&]/ge&print"\e[;H$_";$p+=$i){read STDIN,$_,1;$i=(-5,5,1,-1)[-65+ord]or$i=0
    ;(*y,*z)=\@b[$p+$i,$p+2*$i];$y>3|$z>1&$y>1&&redo;$z|=($y+=4)&2;$y&=~2;@b[$p]-=4}

    May 27

    Perl's uc() function

     


    $STRING = uc($STRING);

     

    Perl's uc() function takes a string, makes the entire thing uppercase, and then returns the new string.

    $myWord = 'word';

    $myUpperWord = uc($myWord);

     First, $myWord is set to a value of 'word', then the uc() function is run on $myWord.

    The uc() function takes the entire string $myWord and converts it to it's uppercase equivalent. The value of $myUpperWord is then equal to 'WORD'