IT 441: Network Services Administration
Class #12 Exercise

Complete by: 4/8/2018


Log in to users.cs.umb.edu

The split function

perl>  print ("Enter the word you wish to search for: ");

perl>  chomp($term = <STDIN>);

perl>  open ( INFILE , '<' , './gettysburg.txt' ) or die $!;

perl>  foreach (<INFILE>) { foreach $word (split) { if ($word eq $search) { print ("I found the word $search in the file \n"); last; } } }

perl>  close (INFILE);
(What is the code foreach $word (split) doing here? Consider the context of the outer foreach loop.)

Previous code -- see how it would look inside a script file!

perl>  open ( PERLCODE , '<' , './readAndPrint1.pl' ) or die $!;

perl>  while (<PERLCODE>) { chomp $_ ; print "$_\n" ; }

perl>  close (PERLCODE);

More split function

perl>  open ( INFILE , '<' , './gettysburg.txt' ) or die $!;

perl>  $string = <INFILE>;

perl>  close (INFILE);

perl>  print $string;

perl>  @array = split (/,/ , $string);

perl>  foreach $part (@array) { print "$part\n" ; }

perl>  @words = split (/ /, $string);

perl>  foreach $word (@words) { print "$word\n" ; }

perl>  @lines = split (/\./, $string);

perl>  foreach $line (@lines) { print "$line\n" ; }
(For the last one, do some of the lines look funny?)

The join function

perl>  @array = (1..9) ;

perl>  print @array;

perl>  $string = join (" ", @array);

perl>  print $string;

perl>  $string = join (",", @array);

perl>  print $string;

perl>  $string = join (", ", @array);

perl>  print $string;

perl>  @array = ("Billy", "Joel", "David", "Bowie") ;

perl>  print (join (", ", @array));

perl>  print (join ("\n#####\n", @array));
(The join function can be very useful, particularly with arrays!)

First regex

perl>  open ( INFILE , '<' , './alice.txt' ) or die $!;

perl>  foreach (<INFILE>) { if (/and/) { print "$_\n" ; } }

perl>  close (INFILE);
(What is the code if (/and/) doing here? Consider the context of the default loop variable, which is known as $_)

Previous code -- see how it would look inside a script file!

perl>  open ( PERLCODE , '<' , './readAndPrint.pl' ) or die $!;

perl>  while (<PERLCODE>) { chomp $_ ; print "$_\n" ; }

perl>  close (PERLCODE);

Regexes - Anchors (start and end of line)

perl>  open ( INFILE , '<' , './alice.txt' ) or die $!;

perl>  @lines = <INFILE> ;

perl>  close (INFILE);

perl>  use Time::HiRes;

perl>  foreach $line (@lines) { if ($line =~ /^Alice/) { print "$line\n" ; Time::HiRes::sleep (0.1); } }

perl>  $DELAY = 0.5;

perl>  foreach $line (@lines) { if ($line =~ /^Alice did/) { print "$line\n" ; Time::HiRes::sleep ($DELAY); } }

perl>  $DELAY = 0.1;

perl>  foreach $line (@lines) { if ($line =~ /she$/) { print "$line\n" ; Time::HiRes::sleep ($DELAY); } }

perl>  $DELAY = 0.5;

perl>  foreach $line (@lines) { if ($line =~ /so she$/) { print "$line\n" ; Time::HiRes::sleep ($DELAY); } }

End the Perl Session

perl>  exit

End the script session

Use the exit command. You should then have a file called typescript in your current working directory.