Perl: Practical Extraction Report language

Additional Examples in PERL

Note that you can learn a lot more of PERL by checking out the perl manual pages from unix. Here are a few special examples, just to give you some idea of the power of perl.


Examples using the
 split(/PATTERN/, String) 
function.

This is an example of the basic split function as you learnt in the lab.

#!/usr/local/bin/perl

     $line = <STDIN>;
     chop ($line);
     @words = split( / /, $line);
     $index = 0;
     while ( $index < @words) {
                     print ("$words[$index]\n");
                     $index++;
     }
     print( "\n");

This example basically splits the input line whenever it matches the pattern specified nside the two slash-es, "/". In this case, it happened to be the space character.


We now look at some other patterns we could use:
Example1.pl
#!/usr/local/bin/perl

     $line = <STDIN>;
     chop ($line);
     @words = split( /:/, $line);
     $index = 0;
     while ( $index < @words) {
                     print ("$words[$index]\n");
                     $index++;
     }
     print( "\n");

NOTES: This one will split the line at every instance of the ":" character.


Example2.pl
#!/usr/local/bin/perl

     $line = <STDIN>;
     chop ($line);
     @words = split(/a|b|c/, $line);
     $index = 0;
     while ( $index < @words) {
                     print ("$words[$index]\n");
                     $index++;
     }
     print( "\n");

NOTES: In this case, the input line will be split when either "a" or "b" or "c" occurs in it. The vertical bar, "|", works as an 'or' operator for the pattern matching.


Example3.pl
#!/usr/local/bin/perl

     $line = <STDIN>;
     chop ($line);
     @words = split(/[a-f]/, $line);
     $index = 0;
     while ( $index < @words) {
                     print ("$words[$index]\n");
                     $index++;
     }
     print( "\n");

NOTES: If you want to split using one of a series of characters, such as any character from among a, b, c, d, e, or f, then you can just specify the sequence as [a-f]. Note that such sequences must be inside the square brackets, [a-f].


Example4.pl
#!/usr/local/bin/perl

     $line = <STDIN>;
     chop ($line);
     @words = split(/[a-cxy]/, $line);
     $index = 0;
     while ( $index < @words) {
                     print ("$words[$index]\n");
                     $index++;
     }
     print( "\n");


NOTES: In this case, the pattern where perl will break the string is any point where characters a, b, c, x, or y occur. (that is, a-to-c, or x, or y.)


Example5.pl

#!/usr/local/bin/perl

     $line = <STDIN>;
     chop ($line);
     @words = split(/[ab]+/, $line);
     $index = 0;
     while ( $index < @words) {
                     print ("$words[$index]\n");
                     $index++;
     }
     print( "\n");

NOTES: If you need to specify the breaking point to be one or more occurences of a character, then the '+' meta-character can be used, as shown. In this case, the input string will be split at any point where one or more CONSECUTIVE 'a' appear, or one or more consecutive 'b' appear.


Example6.pl
#!/usr/local/bin/perl

     $line = <STDIN>;
     chop ($line);
     @words = split(/[ \t]+/, $line);
     $index = 0;
     while ( $index < @words) {
                     print ("$words[$index]\n");
                     $index++;
     }
     print( "\n");

NOTES: This final example shows how to get rid of multiple space-characters, or even mixture of space or tabs.


This is the end of this series of examples, and their associated notes.