How so that Create a Perl Script? Roadmap Perl Programming

 www.phwiki.com

 

The Above Picture is Related Image of Another Journal

 

How so that Create a Perl Script? Roadmap Perl Programming

California State University, Fullerton, US has reference to this Academic Journal, Perl Programming WeeSan Lee cs.ucr /~weesan/cs183/ Roadmap How so that Create a Perl Script? Here Documents Three basic data types: Scalars, Arrays, Hashes Branching Iterations File IO Function/Subroutine String Manipulation Regular Expression How so that Create a Perl Script? Edit hello.pl #!/usr/bin/perl print “Hello World!n”; Make hello.pl an executable $ chmod +x hello.pl Run hello.pl $ ./hello.pl Hello World!

 Garcia, Ken California State University, Fullerton www.phwiki.com

 

Related University That Contributed for this Journal are Acknowledged in the above Image

 

Here Documents #!/usr/bin/perl print << $b $a <= $b $a > $b $a >= $b Strings $a eq $b # Is $a string-equal so that $b? $a ne $b # Is $a string-unequal so that $b?

Arrays @fruit = (“apples”, “oranges”); print “$fruit[0]n”; apples push(@fruit, “banana”); print $fruit[2] . “n”; banana $a = pop(@food); print $a banana $len = @fruit; print $len 2 $str = “@fruit”; print $str apples oranges $” = “/”; print “@fruitn”; apples/oranges ($a, $b) = @fruit; print “$a – $bn”; apples – oranges print “last index = $#fruitn”; last index = 1 $#fruit = 0; print @fruit apples Arrays @ARGV is a special array $ARGV[0]: 1st command-line argument $ARGV[1]: 2nd command-line argument ? Hashes/Associative Arrays %scores = (“Alice”, 80, “Bob”, 90, “Claire”, 92, “David”, 60); print “The score in consideration of Claire = $scores{‘Claire’}n”; %scores = (“Alice” => 80, “Bob” => 90, “Claire” => 92, “David” => 60); print “The score in consideration of Claire = $scores{Claire}n”;

Chapter 1 The Art of Problem Solving Chapter 1 Section 1-1 Solving Problems by Inductive Reasoning Solving Problems by Inductive Reasoning Characteristics of Inductive in addition to Deductive Reasoning Example: determine the type of reasoning Example: predict the product of two numbers Example: predicting the next number in a sequence Pitfalls of Inductive Reasoning Example: pitfalls of inductive reasoning Example: pitfalls of inductive reasoning Example: use deductive reasoning

Hashes/Associative Arrays foreach $i (keys %scores) { print “The score in consideration of $i = $scores{$i}n”; } The score in consideration of Bob = 90 The score in consideration of Claire = 92 The score in consideration of Alice = 80 The score in consideration of David = 60 print “nAll the scores are:”; in consideration of $i (values %scores) { print ” $i”; } print “nn”; All the scores are: 90 92 80 60 Hashes/Associative Arrays How so that display all sorted by the students? in consideration of $i (sort keys %scores) { printf(“%10s: %dn”, $i, $scores{$i}); } Alice: 80 Bob: 90 Claire: 92 David: 60 Hashes/Associative Arrays How so that display all sorted by the scores? in consideration of $i (sort { $scores{$b} <=> $scores{$a} } keys %scores) { printf(“%10s: %dn”, $i, $scores{$i}); } Claire: 92 Bob: 90 Alice: 80 David: 60 A subroutine in consideration of the sort function. $a in addition to $b are the elements so that be compared. <=> is a special 3-way numeric comparison. Eg. 3 <=> 1 returns 1 1 <=> 1 returns 0 1 <=> 3 returns -1

Hashes/Associative Arrays while (($person, $score) = each(%scores)) { print “The score in consideration of $person = $scoren”; } The score in consideration of Bob = 90 The score in consideration of Claire = 92 The score in consideration of Alice = 80 The score in consideration of David = 60 Hashes/Associative Arrays exists() can be used so that check the existence of a hash key If (!exists($scores{weesan})) { print “No score in consideration of weesann”; } %ENV is a special hash variable print “$ENV{USER} is using $ENV{SHELL}n”; weesan is using /bin/tcsh Arrays vs. Hashes #!/usr/bin/perl @a = ( 0 ); %a = ( 0 => 1 ); print “$a[0] = $a[0], $a{0} = $a{0}n”; What?s the output? $a[0] = 0, $a{0} = 1

Branching – if Syntax if () { } elsif () { } else { } Example if (!$a) { print “Empty stringn”; } elsif (length($a) == 1) { print “Len = 1n”; } elsif (length($a) == 2) { print “Len = 2n”; } else { print “Len > 2n”; } Branching – unless Syntax unless () { } else { } Example unless ($my_grade >= 60) { print “I failed CS183!n”; } else { print “I passed CS183!n”; } Branching – switch or not? $_ = ; chop($_); SWITCH: { /[a-z]/ && do { print “$_ is a lower case.n”; last SWITCH; }; /[A-Z]/ && do { print “$_ is a upper case.n”; last SWITCH; }; /[0-9]/ && do { print “$_ is a number.n”; last SWITCH; }; print “$_ is a punctuation.n” }

for loop Syntax in consideration of () { } Examples in consideration of ($i = 0; $i < 10; ++$i) { print "$in"; } Syntax foreach () { } Example foreach $i (0.7, 8, 9) { print "$in"; } foreach $i (@fruit) { print "$in"; } Range operator while loop Syntax while () { } Example print "Password? "; # Ask in consideration of input $a = ; # Get input chop $a; # Remove the newline while (lc($a) ne "weesan") { print "sorry. Again? "; # Ask again $a = ; # Get input again chop $a; # Chop off newline again } Lower case function Chop off the last char. of a string regardless. chomp() remove the trailing n. until loop Syntax until () { } Example print "Password? "; # Ask in consideration of input $a = ; # Get input chop $a; # Remove the newline until (lc($a) eq "weesan") { print "sorry. Again? "; # Ask again $a = ; # Get input again chop $a; # Chop off newline again } do ? while loop Syntax do { } while (); Examples do { print "Password? "; # Ask in consideration of input $a = ; # Get input chop $a; # Chop off newline } while (lc($a) ne "weesan"); # Redo while wrong input File IO #!/usr/bin/perl $file = '/etc/passwd'; open(PASSWD, $file) or die "Failed so that open $file: $!n"; @lines = ; close(PASSWD); print @lines; Open the file in addition to assign the file handle so that PASSWD. $! returns the error string. Read everything into an array. Close the file. Similar so that || in Bash. Print the whole array. File IO #!/usr/bin/perl $file = '/etc/passwd'; open(PASSWD, $file) or die "Failed so that open $file: $!n"; foreach $line () { print $line; } close(PASSWD); Read one line at a time. Print one line at a time. File IO open(INFO, $file); # Open in consideration of input open(INFO, ">$file”); # Open in consideration of output open(INFO, “>>$file”); # Open in consideration of appending open(INFO, ”

Function/Subroutine #!/usr/bin/perl sub bar { print “@_n”; print “1st argument = $_[0]n”; } bar(“1”, “2”, “3”); bar(1, 2, “3”, 4); bar(); Function/Subroutine #!/usr/bin/perl sub max { if ($_[0] > $_[1]) { $_[0]; # return is optional } else { $_[1]; } #return ($_[0] > $_[1] ? $_[0] : $_[1]); } $i = max(3, 1); print “$in”; Function/Subroutine sub foo { $a = 2; } $a = 1; print “$a = $an”; foo(); print “$a = $an”; $a = 1 $a = 2 sub bar { my $a = 2; } $a = 1; print “$a = $an”; bar(); print “$a = $an”; $a = 1 $a = 1

Regular Expression – Misc To ignore case when matching /w+/i More info $ man perlre www p.leeds /Perl/matching.html perl /pub/a/2000/11/begperl3.html Debug Perl $ perl -w debug.pl $ perl -d debug.pl print() or printf() is your friend ? References Programming Perl Ch 2-5 Beginner’s Introduction so that Perl perl /pub/a/2000/10/begperl1.html Perl Tutorial www p.leeds /Perl/start.html

Garcia, Ken Host

Garcia, Ken is from United States and they belong to Host and work for Destination PHX: Sky Harbor! – Phoenix Channel 11 in the AZ state United States got related to this Particular Article.

Journal Ratings by California State University, Fullerton

This Particular Journal got reviewed and rated by File IO open(INFO, $file); # Open in consideration of input open(INFO, “>$file”); # Open in consideration of output open(INFO, “>>$file”); # Open in consideration of appending open(INFO, ” and short form of this particular Institution is US and gave this Journal an Excellent Rating.