CS 1520 Fall 2005: Perl, Lists/Arrays, Control Structures, I/O, Hashes & Regex - Prof. Pan, Study notes of Computer Science

Notes from the university of pittsburgh cs 1520 fall 2005 course on programming languages for web applications. Topics covered include running perl programs, lists/arrays with slices, push/pop, shift/unshift, sort/reverse, control structures such as if/then, while, until, do/while, input/output, and hashes. The document also introduces regular expressions.

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-op4
koofers-user-op4 🇺🇸

8 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 1520 / Fall 2005
Programming Languages
for Web Applications
Alexandros Labrinidis
University of Pittsburgh
05 – Perl (cont)
Alexandros Labrinidis, Univ. of Pittsburgh 2CS 1520 / Fall 2005
How to run Perl programs (revised)
Use text editor (say pico) to write and save program, say hello.pl
Option 1:
perl -w hello.pl
Next time:
perl - w hello.pl
Option 2:
Make sure first line of hello.pl is the following
#!/bin/perl -w
chmod u+x hello.pl
./hello.pl
Next time:
./hello.pl
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS 1520 Fall 2005: Perl, Lists/Arrays, Control Structures, I/O, Hashes & Regex - Prof. Pan and more Study notes Computer Science in PDF only on Docsity!

CS 1520 / Fall 2005

Programming Languages

for Web Applications

Alexandros Labrinidis

University of Pittsburgh

05 – Perl (cont)

Alexandros Lab rinidis, Univ. of Pittsb urgh 2 C S 1520 / Fall 2005

How to run Perl programs (revised)

 Use text editor (say pico) to write and save program, say hello.pl

 Option 1:

perl -w hello.plNext time: perl - w hello.pl

 Option 2:

 Make sure first line of hello.pl is the following #!/bin/perl -w chmod u+x hello.pl ./hello.plNext time: ./hello.pl

Alexandros Lab rinidis, Univ. of Pittsb urgh^3 C S 1520 / Fall 2005

Lists/Arrays/Slices (recap)

 Assignment (LHS vs RHS)

 @beniffer = (“ben affleck”, “jennifer lopez”);  $beniffer[1] = “jennifer gardner”;  ($fred, $barney) = (14, 25);  ($a, $b) = ($b, $a);

 Scalar vs List context

 $a = @beniffer;  print $a.”\n”;  print @bennifer.”\n”; Alexandros Lab rinidis, Univ. of Pittsb urgh 4 C S 1520 / Fall 2005

Lists/Arrays/Slices: Push/Pop

 Push (to right end of array)

 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);

 Pop (from right end of array)

 Removes the last element of a list  $p = pop @list; # $p becomes 10  # @list now (2, 4, 6, 5, 3);

Alexandros Lab rinidis, Univ. of Pittsb urgh^7 C S 1520 / Fall 2005

Control Structures: if/then

 if/else

 if ($a > 5) { #do nothing } else { $a++; }

 unless

 Unless ($a>5) { $a++; } Alexandros Lab rinidis, Univ. of Pittsb urgh 8 C S 1520 / Fall 2005

Control Structures: while

 while loop

 while ($a > 5) { #do something }

 until

 until ($a <= 5) { $a++; }

Alexandros Lab rinidis, Univ. of Pittsb urgh^9 C S 1520 / Fall 2005

Control Structures: do/while

 do/while loop

 do { #do something } while ($a > 5);

 do/until loop

 do { #do something } until ($a <= 5); Alexandros Lab rinidis, Univ. of Pittsb urgh 10 C S 1520 / Fall 2005

Input/Output

 Output:

print - simple  printf - formatted version

 Input:

 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 $_; }

Alexandros Lab rinidis, Univ. of Pittsb urgh^13 C S 1520 / Fall 2005

Hash Functions

 keys(%hashname)

 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”; }

 values(%hashname)

Alexandros Lab rinidis, Univ. of Pittsb urgh 14 C S 1520 / Fall 2005

Hash functions (cont)

 each (%hashname)

 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

 delete $lastname{“Panos”};

Alexandros Lab rinidis, Univ. of Pittsb urgh^15 C S 1520 / Fall 2005

Regular Expressions (intro)

 grep abc somefile > results

 while (<>) {

if (/abc/) {

print $_;