Lab 7 More basics of Perl by hcw25539

VIEWS: 10 PAGES: 32

									      Lab 7
More basics of Perl
     LING 5200
      Fall 2003
     Kevin Cohen
      Iteration: foreach

foreach $element (@array) {
  # do whatever
}
             Iteration: foreach
@array = ("Bloomfield", "Harris");   babel>printStructuralists.pl
                                     Bloomfield
foreach $element (@array) {          Harris
     print "linguist: $element\n";   babel>
}
                Iteration: for
@array = ("Bloomfield", "Harris");
$numberOfThingsInArray = @array;
for($i = 0; $i < $numberOfThingsInArray; $i++) {
     print "linguist: $array[$i]\n";
}

babel>printStructuralists.pl
Bloomfield
Harris
babel>
                Iteration: for
@array = ("Bloomfield", "Harris");
$numberOfThingsInArray = @array;
for($i = 0; $i < $numberOfThingsInArray; $i++) {
     print "linguist: $array[$i]\n";
}

babel>printStructuralists.pl
Bloomfield
Harris
babel>               1
          for (initialValueOfIndex; condition; changeIndex)
                Iteration: for
@array = ("Bloomfield", "Harris");
$numberOfThingsInArray = @array;
for($i = 0; $i < $numberOfThingsInArray; $i++) {
     print "linguist: $array[$i]\n";
}

babel>printStructuralists.pl
Bloomfield
Harris
babel>                                     2
          for (initialValueOfIndex; condition; changeIndex)
                Iteration: for
@array = ("Bloomfield", "Harris");
$numberOfThingsInArray = @array;
for($i = 0; $i < $numberOfThingsInArray; $i++) {
     print "linguist: $array[$i]\n";
}

babel>printStructuralists.pl
Bloomfield
Harris
babel>

          for (initialValueOfIndex; condition; changeIndex)

                                   3: execute code
                Iteration: for
@array = ("Bloomfield", "Harris");
$numberOfThingsInArray = @array;
for($i = 0; $i < $numberOfThingsInArray; $i++) {
     print "linguist: $array[$i]\n";
}

babel>printStructuralists.pl
Bloomfield
Harris
babel>                                             4
          for (initialValueOfIndex; condition; changeIndex)
                Iteration: for
@array = ("Bloomfield", "Harris");
$numberOfThingsInArray = @array;
for($i = 0; $i < $numberOfThingsInArray; $i++) {
     print "linguist: $array[$i]\n";
}

babel>printStructuralists.pl
Bloomfield
Harris
babel>                                     5
          for (initialValueOfIndex; condition; changeIndex)
                Iteration: for
@array = ("Bloomfield", "Harris");
$numberOfThingsInArray = @array;
for($i = 0; $i < $numberOfThingsInArray; $i++) {
     print "linguist: $array[$i]\n";
}

babel>printStructuralists.pl
Bloomfield
Harris
babel>

          for (initialValueOfIndex; condition; changeIndex)

                                   6: execute code
                Iteration: for
@array = ("Bloomfield", "Harris");
$numberOfThingsInArray = @array;
for($i = 0; $i < $numberOfThingsInArray; $i++) {
     print "linguist: $array[$i]\n";
}

babel>printStructuralists.pl
Bloomfield
Harris
babel>                                             7
          for (initialValueOfIndex; condition; changeIndex)
                Iteration: for
@array = ("Bloomfield", "Harris");
$numberOfThingsInArray = @array;
for($i = 0; $i < $numberOfThingsInArray; $i++) {
     print "linguist: $array[$i]\n";
}

babel>printStructuralists.pl
Bloomfield
Harris
babel>                                     8
          for (initialValueOfIndex; condition; changeIndex)
                Iteration: for
@array = ("Bloomfield", "Harris");
$numberOfThingsInArray = @array;
for($i = 0; $i < $numberOfThingsInArray; $i++) {
     print "linguist: $array[$i]\n";
}

babel>printStructuralists.pl
Bloomfield
Harris
babel>

          for (initialValueOfIndex; condition; changeIndex)

                                   9: execute code
         Iteration: for
• What if you wanted to go backwards
  through the list?
            Iteration: for
• What if you wanted to go backwards
  through the list?

• Initial value of index doesn't have to be
  zero
• Condition doesn't have to be "less than
  number of things in the array"
• Update to index doesn't have to be
  increment
             Iteration: for
• What if you wanted to go backwards
  through the list?

for ($i = $numberOfThingsInArray – 1; $i >= 0; $i--) {
  # whatever
}
               Iteration
0 Bloomfield
1 Harris
                Try it!
Write a script that will read in a list of
 words off of the command line, and
 for each word in the list, print it to
 the screen on its own line. For
 example, if the input is
                   Try it!
myScript.pl Bloomfield Sapir Harris Gleason



...then the output should look like this:

Bloomfield
Sapir
                         HINT: @ARGV
Harris
Gleason
                  Try it!
If you get done quickly, then for grins, think
  about this: how would you get it to treat a
  sequence of words as a single word? for
  instance, suppose that you were passing
  this a list of verbs, and you wanted the
  verbs to include "eat", "drink", and "throw
  up"?
• Hint: think about the command line, not
  about the script.
   Cute regular expression
           tricks
• $&: whatever was matched by the last
  regular expression
• $`: whatever preceded whatever was
  matched by the last regular
  expression
• $': whatever followed whatever was
  matched by the last regular
  expression
     Cute regular expression
             tricks
tones: HHH             $data = "tIlIlIN HHH smallest drum";
segments: tIlIlIN
gloss: smallest drum   $data =~ / [HL]+ /;
                       Print "tones: $&\n";
                       Print "segments: $`\n";
                       Print "gloss: $'\n";
     Cute regular expression
             tricks
tones: _HHH_           $data = "tIlIlIN HHH smallest drum";
segments: tIlIlIN
gloss: smallest drum   $data =~ / [HL]+ /;
                       Print "tones: $&\n";
                       Print "segments: $`\n";
                       Print "gloss: $'\n";
     Cute regular expression
             tricks
tones: < HHH >           $data = "tIlIlIN HHH smallest drum";
segments: <tIlIlIN>
gloss: <smallest drum>   $data =~ / [HL]+ /;
                         Print "tones: <$&>\n";
                         Print "segments: <$`>\n";
                         Print "gloss: <$'>\n";
     Cute regular expression
             tricks
tones: <HHH>             $data = "tIlIlIN HHH smallest drum";
segments: <tIlIlIN>
gloss: <smallest drum>   $data =~ / ([HL]+) /;
                         Print "tones: <$1>\n";
                         Print "segments: <$`>\n";
                         Print "gloss: <$'>\n";




    $digit variables
     Cute regular expression
             tricks
tones: <HHH>             $data = "tIlIlIN HHH smallest drum";
segments: <tIlIlIN>
gloss: <smallest drum>   $data =~ /^(.+) ([HL]+) (.+)/;
                         print "tones: <$2>\n";
                         print "segments: <$1>\n";
                         print "gloss: <$3>\n";
              Hashes


%names;

$names{Leonard} = "Bloomfield";
                Hashes


%names;

$names{Leonard} = "Bloomfield";


          key
                Hashes


%names;

$names{Leonard} = "Bloomfield";


          key         value
              Hashes


%names;

%names = ("Leonard", "Bloomfield");


             key         value
 Hashes: the two things you
      must remember
• There is no (apparent) order to the
  keys of a hash
• Keys are unique
             Hashes
• Open file /home/kev/demoHash.pl

								
To top