Download presentation
Presentation is loading. Please wait.
1
2ex.1 Lists and Arrays
2
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 email 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 email 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.
3
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;
4
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";
5
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 ::
6
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: my @a = (1,2,3,"fred"); print @a;123fred You can access an individual array element: print $a[1];2 $a[0] = "*"; print @a;*23fred 3210 scalar 4scalar 3scalar 2scalar 1 3210 "fred"321
7
2ex.7 Lists and arrays You can easily get a sub-array: my @a = (1,2,3,"fred","bob"); print @a;123fredbob print $a[1];2 my @sub_a = @a[2..3] print @sub_a;3fred You can extend an array as much as you like: my @b = (1,2,3) $b[5] = 6; @b is now (1,2,3,undef,undef,6) 4 3210 "bob""fred"321
8
2ex.8 Lists and arrays Assigning to arrays: my @a = (3..6); (3,4,5,6) my @b = qw(a b cat d); ("a","b","cat","d") my ($a,$b,@c) = (1..5); $a=1; $b=2; @c=(3,4,5) Counting array elements: print scalar(@a); 4
9
2ex.9 Reading and printing arrays You can read lines from the standard input in list context: my @a = ; @a will store all the lines entered until the user hits ctrl-z. You can interpolate arrays and array elements into strings: print @b; abcatd print "@b"; a b cat d print "$b[2] is the third element of \@b"; cat is the third element of @b
10
2ex.10 Manipulating arrays – push & pop my @a = (1,2,3,4,5); print @a;12345 push(@a,6); print @a;123456 ----------------------- my @a = (1,2,3,4,5); my $x = pop(@a); print $x;5 print @a;1234
11
2ex.11 shift & unshift my @a = (1,2,3); print @a;123 unshift(@a,0); print @a;0123 --------------------- my @a = (1,2,3); my $x = shift(@a); print $x;1 print @a;23
12
2ex.12 split & join my @a; my @b; @a = split(" ", "You talkin to me? You talkin to me?"); @a = ("You","talkin","to","me?","You","talkin","to","me?") @b = split("", "You talkin to me? You talkin to me?"); @b = ("Y","o","u"," ","t","a","l","k","i","n"," ",...) my $str = join("-", @a); print "$str\n"; "You-talkin-to-me?-You-talkin-to-me?"
13
2ex.13 Reversing lists my @a = ("yossi","bracha","moshe"); print join(";", reverse(@a)); moshe;bracha;yossi (You can also reverse strings…)
14
2ex.14 Sorting lists Default sorting is alphabetical: my @a = sort("yossi","bracha","moshe"); # @a is ("bracha","moshe","yossi") my @b = sort(1,3,9,81,243); # @b is (1,243,81,9) Other forms of sorting require subroutine definition: my @c = sort(compare_sub 1,3,9,81,243); We’ll get to that latter…
15
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
16
2ex.16 Perl Express http://www.perl-express.com http://www.perl-express.com
17
2ex.17 The Perl-Express editor
18
2ex.18 Output tab Output of run Perl Express – running a script Run the script Warnings and errors
19
2ex.19 Perl Express – entering input Click “ Std. Input ”
20
2ex.20 Click “ i/o ” Perl Express – entering input
21
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 with: @a =
22
2ex.22 Class exercise 2 (cont.) Run the script from question 2 in Perl Express
23
2ex.23 The Debugger
24
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
25
2ex.25 Next line that will be executed Start debuggerStep one line Run continuously Add breakpoint – to run until this point
26
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
27
2ex.27 Enter an expression that will be evaluated In order to “ watch ” arrays and more complex data use the “ Evaluate ” button
28
2ex.28 Class exercise 2 (cont.) Follow the working of the script from question 2 in the debugger. Watch the value of each variable.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.