2ex.1 Lists and Arrays
2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings submitted scripts should not produce any warnings When you us problems you run into, send the script, and copy the running of it (with “ perl -w ”) and the output from the command prompt window When submitting exercises by write your name and the exercise number in the subject line (e.g. “ Israel Israeli perl ex. 2 ”) Write a separate file for each question, and name the scripts: “ ex2.1.pl ”, “ ex2.2.pl ”, “ ex2.3.pl ”… Use meaningful name for variables.
2ex.3 Adding comments Comments: The # symbol, and anything from it to the end of the line is ignored. # get start and stop values from the user my $start = my $stop = # calculate string length my $length = $start - $stop + 1;
2ex.4 Adding comments Comments: If you want to insert a comment of multiple lines, you can use =begin and =cut. =begin This program prints stuff. Here you can write any text you want and you don’t need any # =cut print "stuff\n";
2ex.5 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 ::
2ex.6 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] = "*"; 3210 scalar 4scalar 3scalar 2scalar "fred"321
2ex.7 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) "bob""fred"321
2ex.8 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
2ex.9 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
2ex.10 Manipulating arrays – push & pop = (1,2,3,4,5); = (1,2,3,4,5); my $x = print $x;5
2ex.11 shift & unshift = (1,2,3); = (1,2,3); my $x = print $x;1
2ex.12 split & join my = split(" ", "You talkin to me? You talkin to = = split("", "You talkin to me? You talkin to = ("Y","o","u"," ","t","a","l","k","i","n"," ",...) my $str = print "$str\n"; "You-talkin-to-me?-You-talkin-to-me?"
2ex.13 Reversing lists = ("yossi","bracha","moshe"); print join(";", moshe;bracha;yossi (You can also reverse strings…)
2ex.14 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…
2ex.15 Class exercise 2 Write the following scripts: 1.Read a number from the first line of input, and then read the rest of the lines and print the one selected by that number 2.Read a list of numbers separated by spaces, and print those numbers in reverse order, separated by slashes (/) 3.Read a list of words separated by spaces, sort and print them 4*.Like in 2, but double the first and the last numbers 5*.Like in 3, but reverse the order of the letters of the last word
2ex.16 Perl Express
2ex.17 The Perl-Express editor
2ex.18 Output tab Output of run Perl Express – running a script Run the script Warnings and errors
2ex.19 Perl Express – entering input Click “ Std. Input ”
2ex.20 Click “ i/o ” Perl Express – entering input
2ex.21 Go back to “ Std. Output ” Perl Express – entering input Enter input Note: Perl Express can’t be given input with a ctrl-Z, so you can’t run a script =
2ex.22 Class exercise 2 (cont.) Run the script from question 2 in Perl Express
2ex.23 The Debugger
2ex.24 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
2ex.25 Next line that will be executed Start debuggerStep one line Run continuously Add breakpoint – to run until this point
2ex.26 You can “ watch ” your variables as they change their values using the “ Watch List ” window Mark a variable name 1 Click “ Add Watch ” 2 The variable will be displayed 3
2ex.27 Enter an expression that will be evaluated In order to “ watch ” arrays and more complex data use the “ Evaluate ” button
2ex.28 Class exercise 2 (cont.) Follow the working of the script from question 2 in the debugger. Watch the value of each variable.