CS 2204 Lab 11

Your name here (please print):

Your student ID number here:

    In this lab you will construct a few short PERL programs that perform the actions specified below. To begin, you will need to download the following files to your home directory:

    PerlLab1.txt, PerlLab2.txt

  1. (1 point) Read in the file "PerlLab1.txt", print each line of the file to standard output, then close the file.
    #!/usr/bin/perl

    open(IN,"PerlLab1.txt");
    while(){
          print $_;
    }
    close(IN);
  2. (2 points) Read in the file "PerlLab1.txt", print to standard output each line containing the string "print me", then close the file. #!/usr/bin/perl

    open(IN,"PerlLab1.txt");
    while(){
          if( $_ =~ /print me/ ){
                print $_;
          }
    }
    close(IN);
  3. (2 points) Read in the file "PerlLab1.txt", print to standard output each line which contains the string "print me" and does not contain the string "no print". #!/usr/bin/perl

    open(IN,"PerlLab1.txt");
    while(){
          if( $_ =~ /print me/ ){
                if( $_ !~ /no print/ ){
                      print $_;
                }
          }
    }
    close(IN);
  4. (3 points) Read each of the values contained in the array @ARGV. Print each value in the array to standard output as a line. For example, if your script for this exercise is called "PerlProb4.pl", then executing:
          ./PerlProb4.pl This is it!
    generates
            This
            is
            it!
    
    #!/usr/bin/perl

    foreach $value (@ARGV){
          print "$value\n";
    }
  5. (3 points) Read in the file "PerlLab2.txt". Each line contains three numbers seperated by spaces ("\s+"). Split each line into an array. Print the sum of all three numbers for each line. #!/usr/bin/perl

    open(IN,PerlLab2.txt);
    while(){
          chomp;
          my @arr = split(/\s+/,$_);
          print $arr[0] + $arr[1] + $arr[2], "\n";
    }
    close(IN);