





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Alexandros Labrinidis, Univ. of Pittsburgh 2 CS 1520 / Fall 2007
Use text editor (say pico) to write and save program, say hello.pl
perl -w hello.pl Next time: perl - w hello.pl
Make sure first line of hello.pl is the following #!/bin/perl -w chmod u+x hello.pl ./hello.pl Next time: ./hello.pl Alexandros Labrinidis, Univ. of Pittsburgh^3 CS 1520 / Fall 2007
(1, 2, 3) #array of three values: 1, 2, and 3 (“fred”, 4, 5) #array of three values: “fred”, 4, and 5 ($a, 42) #two values: current value of $a and 42 ($a+$b, $c+$d) #also two values () #the empty list (no elements) (1 .. 5) #same as (1, 2, 3, 4, 5) Alexandros Labrinidis, Univ. of Pittsburgh^4 CS 1520 / Fall 2007
qw stands for quote word : it creates a list from the nonwhitespace parts within the parentheses
print (“the answer is “,@a,”\n”);
Alexandros Labrinidis, Univ. of Pittsburgh 5 CS 1520 / Fall 2007
Alexandros Labrinidis, Univ. of Pittsburgh 6 CS 1520 / Fall 2007
#equivalent to @phoebe = (“smelly”, “cat”);
#chandler becomes (“here”, “is”, “smelly”, “cat”, 5, 6);
#puts 42 in front of chandler
#puts “last” at the end of chandler Alexandros Labrinidis, Univ. of Pittsburgh^7 CS 1520 / Fall 2007
#give value of 1 to $a, 2 to $b, 3 to $c
#swap $a and $b
#give $a to $d, and ($b, $c) to @ross
#remove first element of @ross and give it to $f #this makes $f = $b, and @ross = ($c) Alexandros Labrinidis, Univ. of Pittsburgh^8 CS 1520 / Fall 2007
In this case, the length of the array is returned!
@barney = @fred; # all of @fred is copied to @barney ($a) = @fred; # $a gets the first element of @fred $a = @fred; # $a gets the length of @fred
Alexandros Labrinidis, Univ. of Pittsburgh 13 CS 1520 / Fall 2007
Alexandros Labrinidis, Univ. of Pittsburgh 14 CS 1520 / Fall 2007
Numeric String Equal: == eq Not equal: != ne Less than: < lt More than: > gt Less than or equal: <= le More than or equal: >= ge Alexandros Labrinidis, Univ. of Pittsburgh^15 CS 1520 / Fall 2007
Alexandros Labrinidis, Univ. of Pittsburgh^16 CS 1520 / Fall 2007
while ($a > 5) { #do something }
until ($a <= 5) { $a++; }
Alexandros Labrinidis, Univ. of Pittsburgh 17 CS 1520 / Fall 2007
do { #do something } while ($a > 5);
do { #do something } until ($a <= 5); Alexandros Labrinidis, Univ. of Pittsburgh 18 CS 1520 / Fall 2007
Alexandros Labrinidis, Univ. of Pittsburgh^19 CS 1520 / Fall 2007
Alexandros Labrinidis, Univ. of Pittsburgh^20 CS 1520 / Fall 2007
Alexandros Labrinidis, Univ. of Pittsburgh 25 CS 1520 / Fall 2007
Adds an element to end of list @list = (2, 4, 6); push @list, 5 ; # @list now (2, 4, 6, 5 ); Push @list, (3, 10); # @list now (2, 4, 6, 5, 3, 10);
Removes the last element of a list $p = pop @list; # $p becomes 10 # @list now (2, 4, 6, 5, 3); Alexandros Labrinidis, Univ. of Pittsburgh 26 CS 1520 / Fall 2007
Adds an element to the beginning of list @list = (2, 4, 6); unshift (@list, 7); # @list now ( 7 , 2, 4, 6); unshift (@list, 1, 12); # @list now (1, 12, 7, 2, 4, 6);
Removes the first element of a list $p = shift @list; # $p becomes 1 # @list now (12, 7, 2, 4, 6); Alexandros Labrinidis, Univ. of Pittsburgh^27 CS 1520 / Fall 2007
sorts elements of an array Example: @x = qw(small medium large); @y = sort (@x); # @x stays the same # @y is (“large”, “medium”, “small”)
Reverses current order (i.e., NOT the reverse sort order) @a = (10, 5, 12, 45); @b = reverse @a; # @a stays the same # @b is (45, 12, 5, 10) Alexandros Labrinidis, Univ. of Pittsburgh^28 CS 1520 / Fall 2007
$class{“CS1520”} = “SENSQ 5502”; %class = (“CS1520” => “SENSQ 5502”, “CS1555” => “SENSQ 5502”); %class = (“CS1520”, “SENSQ 5502”, “CS1555”, “SENSQ 5502”);
Alexandros Labrinidis, Univ. of Pittsburgh 29 CS 1520 / Fall 2007
return the list of current keys $fred{“a”} = “b”; $fred{“c”} = “d”; $fred(15) = 143; @list = keys(%fred); # @list = (“a”, “c”, 15); $n = keys(%fred); # $n = 3; foreach $k (keys (%fred)) { print “we have $fred{$k} at key $k\n”; }
Alexandros Labrinidis, Univ. of Pittsburgh 30 CS 1520 / Fall 2007
Iterate over all elements of hash hashname $lastname{“Alex”} = “Labrinidis”; $lastname{“Panos”} = “Chrysanthis”; $lastname{“John”} = “Ramirez”; while ( ($first, $last) = each (%lastname) ) { print “The last name of $first is $last\n”; }
delete $lastname{“Panos”}; Alexandros Labrinidis, Univ. of Pittsburgh^31 CS 1520 / Fall 2007
Alexandros Labrinidis, Univ. of Pittsburgh^32 CS 1520 / Fall 2007
print - simple printf - formatted version
standard input is accessed via will return 0 if no more input <> form is more general; can read from files $_ to access line within loop while () { print $_; }