2.1 Lists and Arrays 2.1
2.2 Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators: numbers: + - * / ** strings:. x Variables: my $name; Reading input: $name = ; Functions: length($name); substr($name,2,2);
2.3 Undefined variables my $a; print($a+3); Use of uninitialized value in addition (+) 3 print("a is :$a:"); Use of uninitialized value in concatenation (.) or string a is ::
2.4 Lists and arrays A list is an ordered set of scalar values: (1,2,3,"fred") An array is a variable that holds a list: = (1,2,3,"fred"); You can access an individual array element: print $a[1];2 $a[0] = "*"; scalar 4scalar 3scalar 2scalar 1
2.5 Lists and arrays You can easily get a sub-array: = (1,2,3,"fred","bob"); print $a[1];2 You can extend an array as much as you like: = (1,2,3) $b[5] = is now (1,2,3,undef,undef,6) scalar 4scalar 3scalar 2scalar 1
2.6 Lists and arrays Assigning to arrays: = (3..6); (3,4,5,6) = qw(a b cat d); ("a","b","cat","d") my = (1..5); $a=1; Counting array elements: print 4 2.6
2.7 Reading and printing arrays You can read lines from the standard input in list context: = will store all the lines entered until the user hits ctrl-z. You can interpolate arrays and array elements into strings: abcatd print a b cat d print "$b[2] is the third element of cat is the third element 2.7
2.8 Manipulating arrays – push & pop = (1,2,3,4,5); = (1,2,3,4,5); my $x = print $x;5 2.8
2.9 shift & unshift = (1,2,3); = (1,2,3); my $x = print $x;1 2.9
2.10 split & join = split(",", "hello,how are you?,goodbye"); print "$a[1]\n"; how are you? holds the list: ("hello","how are you?","goodbye") my $str = print "$str\n"; hello:how are you?:goodbye 2.10
2.11 Reversing lists = ("yossi","bracha","moshe"); print join(";", moshe;bracha;yossi (You can also reverse strings…) 2.11
2.12 Sorting lists Default sorting is alphabetical: = sort("yossi","bracha","moshe"); is ("bracha","moshe","yossi") = sort(1,3,9,81,243); is (1,243,81,9) Other forms of sorting require subroutine definition: = sort(compare_sub 1,3,9,81,243); We’ll get to that latter… 2.12
2.13 The Debugger 2.13
2.14 Debugging A complex program will never work correctly the first time you run it! So: Write the program one stage at a time and check that it works Use a debugger to execute the program step by step Next line that will be executed
2.15 Next line that will be executed Start debuggerStep one line Run continuously Add breakpoint – to run until this point
2.16 Choose “ i/o ” for interactive input 1 View printed output 2 Enter input 3
2.17 You can “ watch ” your variables as they change their values using the “ Watch List ” window Mark a variable name 1 Click “ Add Watch ” 2
2.18 Enter expression In order to “ watch ” arrays and more complex data use the “ Evaluate Watch ” button
2.19 Controls: Ifs and Loops
2.20 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions. else { print "Are you doing anything tomorrow night?\n"; } print "How old are you?\n"; my $age = ; if ($age < 18) { print "Sorry, I’m not allowed to chat with minors\n"; }
2.21 if, elsif, else It’s convenient to test several conditions in one if structure: if ($age < 18) { print "Sorry, I’m not allowed to chat with minors\n"; } elsif ($age < 25) { print "Are you doing anything tomorrow night?\n"; } elsif ($age < 35) { print "Are you married?\n"; } else { print "Do you need help crossing the street?\n"; }
2.22 Comparison operators StringNumericComparison eq==Equal ne!=Not equal lt<Less than gt>Greater than le<= Less than or equal to ge>= Greater than or equal to if ($age == 18)... if ($name eq "Yossi")... if ($name ne "Yossi")... if ($name lt "n")...
2.23 Boolean operators if (($age==18) && ($name eq "Yossi"))... if (!($name ne "Yossi"))... if (!($name eq "Yossi:" && $age==18))... And && Or || Not !
2.24 Controls: Loops Commands inside a loop are executed repeatedly (iteratively): while ($name ne "Yossi") { chomp($name = ); print "Hello $name!\n"; } foreach $name { print "Hello $name!\n"; } * There are also until, do-while and do-until loops
2.25 Loops $i=0; while { $name = $names[$i]; print "Hello $name!\n"; $i++; } for ($i=0; $i++) { $name = $names[$i]; print "Hello $name!\n"; } A for loop is controlled by three statements: 1 st is executed before the first iteration 2 nd is the stop condition 3 rd is executed before every re-iteration These are equivalent
2.26 Breaking out of loops next – skip to the next iteration last – skip out of the loop = ; foreach $line { if (substr($line,0,1) ne ">") { next; } print(substr($line,1)); if (substr($line,0,4) eq ">ehd") { last; } }
2.27 Breaking out of loops die – end the program and print an error message to the standard error if ($score<0) { die "score must be positive"; } score must be positive at test.pl line 8. Note: if you end the string with a "\n" then only your message will be printed * warn does the same thing as die without ending the program