Monday, November 21, 2011

PERL SPLIT OPERATOR

PERL SPLIT OPERATOR


 

REVISED: Sunday, March 3, 2013




You will learn how to use the Perl split operator.

I.  PERL SPLIT OPERATOR

The Perl split operator is commonly used when parsing data from a file or from another program.  The Perl split operator allows you to break up a string into an array or a list, by using a specific pattern.

The split operator is used to split a string into smaller sections.  You can split a string on a single character, a group of characters or a regular expression, a pattern.  You can also specify how many pieces to split the string into.

The Perl split operator syntax is as follows:

LIST = split(/PATTERN/, EXPR, LIMIT);

LIST is a list, array or hash that is returned by the split operator.  If LIST is omitted, the Perl split operator will be called in a scalar context and the EXPR will be split in the @_ array.   If LIST is omitted the operator will return the number of fields in which the EXPR will be split.  If your call to a split operator is inside the body of a subroutine it alters the value of @_.

PATTERN is normally a regular expression; however,  PATTERN can be a single character or a string.  ILIMIT is not specified the EXPR is split on every occurrence of the PATTERN.  If the PATTERN parameter is omitted, the Perl split operator will split on whitespaces, skipping any leading whitespaces.

EXPR is the string expression that will be split into an array or a list.  If EXPR is omitted, the content of the special scalar variable $_ will be split.

LIMIT is the maximum number of fields the EXPR will be split into.
Only the word split is compulsory.  You can omit the parenthesis, the LIST that will be returned and any of the third arguments presented above.

If you use one of the following metacharacter inside the pattern you need to escape the metacharacter by using the \ backslash character to indicate that the metacharacter is to be regarded as something to match and not some fancy character:

^     $     ( )     \     |     @     [     {     ?     .     +     *

For example, if you want to split your string using the pattern /&&/ you need to write it as /\&\&/.

The Perl join operator is the opposite of the Perl split operator.  There are a lot of situations where they are used together.

The Perl join operator concatenates; i.e., joins the elements of a list into a single string.

The Perl split operator breaks up a single string into a list of strings and returns that list.

II.  PERL SPLIT OPERATOR SOURCE CODE

Copy paste the following into your text editor.

#Provides undefined value warnings.
use warnings;

#Helps you catch typos.
#Forces you to use my() function to declare all variables.
use strict;

#Specify the file.
my($file) = "C:/Strawberry/splitOperator.pl";
#Open the file and read data.
#Die with grace if it fails.
open (FILE, "<$file") or die "Can't open $file: $!\n";

#initialize array

my(@fields) = split(/-/, "808-253-4321");
print "The telephone number is: @fields\n\n";

#Close the file.
close FILE;

From your text editor, "File Save As" splitOperator.pl using the path to the folder of your Perl download.

From the Perl prompt type the following:

C:\WINDOWS\system32>C:\Strawberry\splitOperator.pl

Press Enter and the following will display on your screen:

C:\WINDOWS\system32>C:\Strawberry\splitOperator.pl
The telephone number is: 808 253 4321

C:\WINDOWS\system32>

You have learned how to use the Perl split operator.

Elcric Otto Circle


















-->   -->   -->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"






PERL JOIN OPERATOR

PERL JOIN OPERATOR



 

REVISED: Sunday, March 3, 2013




You will learn how to use the Perl join operator.

I.  PERL JOIN OPERATOR INTRODUCTION

The syntax form of Perl join operator is as follows:

$string = join (EXPR, LIST);

The join operator has two parameters:

EXPR may be any string and it represents a separator for the element of the list or array.

LIST represents a list or array whose elements will be merged into a string.

The join operator will return a string that contains the elements of the array or list, connected through a string separator.

II.  PERL JOIN OPERATOR EXAMPLE

The following example demonstrates the use of the join operator:

#initialize an array
my @perlFunc = ("Mary","had","a","little","lamb.");
my $perlFunc = join " ", @perlFunc;
print "Perl Functions: $perlFunc\n";

Output: Mary had a little lamb.

We used the space separator " " to glue the elements of the @perlFunc array. You can omit the parentheses when you call the Perl join operator.

You have learned how to use the Perl join operator.

Elcric Otto Circle






-->




-->




-->












How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"






Friday, November 18, 2011

PERL FOR LOOP

PERL FOR LOOP


 

REVISED: Sunday, March 3, 2013




You will learn how to use the Perl for loop.

I.  PERL FOR LOOP

The basic kinds of loops in Perl are the for loop, the while loop, and the foreach loop. The Perl for loop is used to run a block of code a specific number of times.

The Perl for loop syntax is as follows:

for(initialization; test; increment){
   block of statements;
}

The for loop has three arguments separated by two semicolons.

Step 1. The first argument, the counter, is initialized.  Step 1. is executed only once at the beginning of the for statement.

Step 2. The second argument, the test, is evaluated. If the test evaluates to true, the block of statements are executed; otherwise the for loop is terminated.

Step 3. After the block of statements, enclosed by the curly braces, are executed, the counter is incremented by the third argument and "Step 2." is repeated.

Perl loops through "Step 2." and "Step 3." until the test expression is false and the for loop is terminated.

If the test expression is never false, you have an infinite loop which can be stopped by pressing Ctrl+c.

Here is a code snippet to print a message 10 times.

for($counter = 1; $counter <= 10; $counter++){
    print "for loop #$counter\n";
}

Step 1. $counter is initialized to 1.

Step 2. Perl checks to see if the $counter is less than or equal to 10.  $counter is 1; the test is true therefore, Perl executes the code inside the block.

Step 3. Perl increases $counter by 1.

The process continues looping through "Step 2." and "Step 3." until $counter is equal to 11 at which time the test is false.

$counter is no longer less than or equal to 10; therefore, the loop exits, after the code block has executed 10 times.

II.  PERL FOR LOOP SOURCE CODE

You can initialize the counter outside the loop, and increment the control variable inside the block as shown by the following example.

Copy paste the following into your text editor.

#Unicode
use utf8;
#Provides undefined value warnings.
use warnings;
#enables -w flag.

use diagnostics;
#Helps you catch typos.
#Forces you to use my() function to declare all variables.
use strict;

#stack trace
use Carp ();
      local $SIG{__WARN__} = \&Carp::confess;

#Specify the file.
my($file) = "C:/Strawberry/forLoop.pl";

#Open the file and read data.
#Die with grace if it fails.
open (FILE, "<$file") or die "Can't open $file: $!\n";

my($counter) = 1;
for(; $counter <= 10;){
     print "for loop #$counter\n";
     $counter++;
}

#Close the file.
close FILE;

From your text editor, "File Save As" forLoop.pl using the path to the folder of your Perl download.

From the Perl prompt type the following:
C:\WINDOWS\system32>C:\Strawberry\forLoop.pl

Press Enter and the following will display on your screen:

C:\WINDOWS\system32>C:\Strawberry\forLoop.pl
for loop #1
for loop #2
for loop #3
for loop #4
for loop #5
for loop #6
for loop #7
for loop #8
for loop #9
for loop #10

C:\WINDOWS\system32>

You have learned how to use the Perl for loop.

Elcric Otto Circle



-->   -->   -->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"






PERL IF STATEMENT

PERL IF STATEMENT


 

REVISED: Sunday, March 3, 2013




You will learn how to use the Perl "ifelse if, and else" statements to write simple logic in your Perl scripts.

I.  PERL QUICK REVIEW

Before we introduce something new, we will have a quick review:

A.  CALCULATIONS

Perl uses double-precision floating point values for calculations.

B.  STRING

Perl defines string as a sequence of characters.  The shortest string is the null string it contains no characters.  The longest string is only limited by the available memory of your computer.

C.  INTERPOLATION

Interpolation of scalar variables is a mechanism to convert a scalar variable inside a string into string.

D.  BLOCK

Perl groups statements into blocks of code.  A block is surrounded by a pair of ({ }) curly braces and can be nested within a block.

E.  LOGICAL COMPARISON

In Perl, everything is true except the number zero 0, empty string "", zero string "0", and undefined (undef) values.

II.   PERL "IF" CONTROL STRUCTURE

Decision statements use the "if" keyword to execute a statement block depending on the evaluation of conditional expressions.  Therefore, you can use the Perl "if" control structure to write simple logic in your code.

The "if" control structure can be nested.

The syntax of an "if" control structure is as follows:

if(condition){
statements;
}

If the condition is true the statements inside the block will be executed: for example:

$x = 1;
$y = 1;

if($x == $y){
   print "$x is equal to $y";
}

The first and second lines define two variables $x and $y with their values initialized to 1.

In the fourth line we use the "if "statement to print a message if $x is equal to $y.

The message is only printed if the expression ($x == $y) is evaluated as true.

III.   PERL "IF-ELSE" CONTROL STRUCTURE

If you need an alternative choice, Perl provides an "if-else" control structure as follows:

if(condition){
   if-statements;
}

else{
   else-statements;
}

If the condition is false the else-statements will be executed; for example:

$x = 2;
$y = 3;

if($x == $y){
   print "x is equal to y";
}

else{
   print "x is not equal to y";
}

IV.   PERL "IF-ELSE-IF" CONTROL STRUCTURE

A. MULTIPLE CHOICES

Perl also provides an "if-else-if" control structure to make multiple choices based on conditions.

if(condition1){
   if-statements;
}

else if(condition2){
   else if-statements;
}

else if(condition3){
   else if-statements;
}

...

else{
   else-statements;
}

Here is the source code example:

$x = 2;
$y = 3;

if($x > $y){
   print "x is greater than y";
}

else if ($x < $y){
   print "x is less than y";
}

else{
   print "x is equal to y";
}

B. READABILITY

Perl helps you create a self documenting style of easy to read code.

A year after you write a program you should be able to read and understand what you wrote.

The following is another way to format your Perl code:

if (condition1) {

    task;

} elsif (condition2) {

    a different task;

} else {

    a task if all else fails;

}

V.   PERL SCRIPT SOURCE CODE
Copy paste the following into your text editor.

#Unicode
use utf8;

#Provides undefined value warnings.
use warnings;

#enables -w flag.
use diagnostics;

#Helps you catch typos.
#Forces you to use my() function to declare all variables.
use strict;  
 
#stack trace
use Carp ();
   local $SIG{__WARN__} = \&Carp::confess;
#Specify the file.
my($file) = "C:/Strawberry/ifElse.pl";
#Open the file and read data.
#Die with grace if it fails.
open (FILE, "<$file") or die "Can't open $file: $!\n";
my($x) = 2;
my($y) = 3;

if($x == $y){
print "x is equal to y";
}

else{
print "x is not equal to y";
}
#Close the file.
close FILE;

From your text editor, "File Save As" ifElse.pl using the path to the folder of your Perl download.

From the Perl prompt type the following:

C:\WINDOWS\system32>C:\Strawberry\ifElse.pl

Press Enter and the following will display on your screen:

C:\WINDOWS\system32>C:\Strawberry\ifElse.pl
x is not equal to y
C:\WINDOWS\system32>

The my( ) function makes the variables local to the main package.  Packages are classes.

A running Perl program has a built-in namespace called "main", which is the default name.  For example, a subroutine called Sub1 can be called as Sub1( ) or main::Sub1( ).

With a variable the appropriate sigil is placed in front of the namespace; so a scalar variable called $var1 can also be referred to as $main::var1, or even $::var1.  Other namespaces can be created at any time.

Now you have a "learning feed-back loop."  Change the values of $x and $y.  Think about what you expect Perl will do.  Then run the program and see if Perl does what you expect.

If pearl does not do what you expect, look at your code and make the appropriate changes, then rerun the program.

You have learned how to use the Perl "if, else if, and else" statements to write simple logic in your Perl scripts.

Elcric Otto Circle




-->   -->   -->








How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"






Friday, November 11, 2011

PERL REGEX REGULAR EXPRESSIONS

PERL REGEX REGULAR EXPRESSIONS




REVISED: Sunday, March 3, 2013




You will learn how to use Perl REGEX, regular expressions.

I.  REGEX

In its simplest form, a regular expression is just a word or phrase to search for.

REGEX are Perl REGular EXpressions.  Perl REGEX syntax make it easy to do the following:

Complex string comparisons.

Complex string selections.

Complex string replacements.

Parsing based on the above abilities.

A.  COMPLEX STRING COMPARISONS

1.  The following is a very basic string logical comparison:

$string =~ m/sought_text/;

The above returns true if the string $string contains substring "sought_text", and false otherwise.

2.  If you only want the strings where the sought_text appears at the "very beginning" of $string, write:

$string =~ m/^sought_text/;

3.  The $ operator indicates "end of string". If you want to find out if the sought_text is the very last text in the $string, write:

$string =~ m/sought_text$/;

4.  If you want the comparison to be true only if $string contains the sought_text and nothing but the sought_text, write:

$string =~ m/^sought_text$/;

5.  If you want the comparison to be case insensitive add the letter i after the ending delimiter:

$string =~ m/^sought_text$/i;

6.  Wild Cards

 Match any character
\w  Match "word" character alphanumeric, plus "_"
\W  Match non-word character
\s  Match whitespace character
\S  Match non-whitespace character
\d  Match digit character
\D  Match non-digit character
\t  Match tab
\n  Match newline
\r  Match return
\f  Match formfeed
\a  Match alarm bell, beep, etc.
\e  Match escape
\021  Match octal char 21
\xf0  Match hex char f0

7.  Repetition

*                 Match 0 or more times
+                Match 1 or more times
?                 Match 1 or 0 times
{n}           Match exactly n times
{n,}          Match at least n times
{n,m}      Match at least n but not more than m times

8.  Using Groups ( ) in Matching

Groups are regular expression characters surrounded by parentheses ( ).

a.  Groups are used to allow alternative phrases; e.g.: 

/(Jack|Jill|Hill)/i

For single character alternatives, use character classes [ ].  Everything inside the brackets represents one character, listing all its alternative possibilities.  Character classes are alternative single characters within square brackets [ ].  There are two commonly used special characters inside the square brackets:

A hyphen (-) is used to indicate all characters in the colating sequence between the character on the hyphen's (-left and the character on the hyphen's (-right.

An uparrow (^) at immediately following the opening square bracket means "anything but these characters", and effectively negates the character class.

Character classes have three main advantages:

i.     Shorthand notation, as [AEIOUY] instead of (A|E|I|O|U|Y).
ii.   Character Ranges, such as [A-Z].
iii.  One to one mapping from one class to another; e.g.: tr/[a-z]/[A-Z]/.

b.  Groups are also used as a means of retrieving selected text in selection, translation and substitution, used with scalers; $1, $2; etc.

B.  COMPLEX STRING SELECTIONS

Replace every "Jack" with "Jill"

$string =~ s/Jack/Jill/;

C.  COMPLEX STRING REPLACEMENTS

Translations are like substitutions, except they happen on a letter by letter basis instead of substituting a single phrase for another single phrase.

What if you wanted to make all vowels upper case:

$string =~ tr/[a,e,i,o,u,y]/[A,E,I,O,U,Y]/;

Change everything to upper case:

$string =~ tr/[a-z]/[A-Z]/;

Change everything to lower case

$string =~ tr/[A-Z]/[a-z]/;

II.  SYMBOLS

=~

This operator appears between the string var you are comparing, and the regular expression you are looking for.  In selection or substitution a regular expression operates on the string var rather than comparing; for example:

$string =~ m/Jack/;
#return true if var $string contains the name Jack

$string =~ s/Jack/Jill/;
#replace Jack with Jill

!~

Just like =~, except negated.   Returns true if it does not match. 

/

This is the usual "delimiter" for the text part of a regular expression; for example:

$string =~ m/Jack/;
#return true if var $string contains the name Jack

$string =~ s/Jack/Jill/;
#replace Jack with Jill

If the sought-after text contains slashes, it is easier to use pipe symbols (|) for delimiters.

m

This is the "match" operator.  The match operator comes before the opening delimiter.   The match operator means read the string expression on the left of the =~, and see if any part of it matches the expression within the delimiters following the m.  If the delimiters are slashes (/), the m is optional and often not included.  Whether the match operator is there or not, it is still a match operation; for example:

$string =~ m/Jack/;
#return true if var $string contains the name Jack

$string =~ /Jack/;
#same result as previous statement

^

This is the "beginning of line" symbol.  When used immediately after the starting delimiter, it signifies "at the beginning of the line"; for example:

$string =~ m/^Jack/;
#true only when "Jack" is the first text in the string

$

This is the "end of line" symbol.  When used immediately before the ending delimiter, the "end of line" symbol signifies "at the end of the line"; for example:

$string =~ m/Jack$/;
#true only when "Jack" is the last text in the string

i

This is the "case insensitivity" operator when used immediately after the closing delimiter; for example:

$string =~ m/Jack/i;
#true when $string contains "Jack" or "jAcK"

III.  TABLES


Some characters have a special meaning to the searcher. These characters are called metacharacters. 

METACHARACTERS

CHARMEANING
^
beginning of string
$
end of string
.
any character except newline
*
match 0 or more times
+
match 1 or more times
?match 0 or 1 times; or: shortest match
|
alternative
( )
grouping; “storing”
[ ]
set of characters
{ }
repetition modifier
\
quote or special


REPETITION


a*
zero or more a’s
a+
one or more a’s
a?
zero or one a’s (i.e., optional a)
a{m}
exactly m a’s
a{m,}
at least m a’s
a{m,n}
at least m but at most a’s
repetition?
same as repetition but the shortestmatch is taken



SPECIAL NOTATIONS WITH \

Single characters
\ttab
\nnewline
\rreturn (CR)
\xhhcharacter with hex. code hh
“Zero-width assertions”
\b“word” boundary
\Bnot a “word” boundary
Matching
\wmatches any single character classified as a “word” character (alphanumeric or “_”)
\Wmatches any non-“word” character
\smatches any whitespace character (space, tab, newline)
\Smatches any non-whitespace character
\dmatches any digit character, equiv. to [0-9]
\Dmatches any non-digit character


CHARACTER CLASS [...]
Inside a "character class" denoted by [...] the following rules apply:
[characters]matches any of the characters in the sequence
[x-y]matches any of the characters from x to y (inclusively) in the ASCII code
[\-]matches the hyphen character “-
[\n]matches the newline; other single character denotations with \ apply normally, too
[^something]matches any character except those that [something] denotes; that is, immediately after the leading “[”, the circumflex “^” means “not” applied to all of the rest

EXPRESSION
MATCHES
abcabc (that exact character sequence, but anywhere in the string)
^abcabc at the beginning of the string
abc$abc at the end of the string
a|beither of a and b
^abc|abc$the string abc at the beginning or at the end of the string
ab{2,4}can a followed by two, three or four b’s followed by a c
ab{2,}can a followed by at least two b’s followed by a c
ab*can a followed by any number (zero or more) of b’s followed by a c
ab+can a followed by one or more b’s followed by a c
ab?can a followed by an optional b followed by a c; that is, either abc or ac
a.can a followed by any single character (not newline) followed by a c
a\.ca.c exactly
[abc]any one of ab and c
[Aa]bceither of Abc and abc
[abc]+any (nonempty) string of a’s, b’s and c’s (such as aabbaacbabcacaa)
[^abc]+any (nonempty) string which does not contain any of ab and c (such as defg)
\d\dany two decimal digits, such as 25; same as \d{2}
\w+a “word”: a nonempty sequence of alphanumeric characters and low lines (underscores), such as cp3o and r2d2 and cool_1
100\s*mkthe strings 100 and mk optionally separated by any amount of white space (spaces, tabs, newlines)
abc\babc when followed by a word boundary (e.g. in abc! but not in abcd)
perl\Bperl when not followed by a word boundary (e.g. in perlert but not in perl stuff)


You have learned how to use Perl REGEX, regular expressions.

Elcric Otto Circle





-->   -->   -->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"