|
CSCI A348/548
|
Please use this lab to bring yourself up to date with the second assignment.
Lecture Notes Eight contain all the details, step by step. Assignment 3, coming soon, will be more straightforward, and will take less time. So we can extend the second assignment one more week, so you can take your time finishing it.
In this lab, for those of you who are already done, a few substitutions, from Tuesday.
Perl Substitutions (p. 12, 88 Learning Perl - Chapter 7)
We're using the =~ operator, together with the letter s on its right
hand side, followed by a slash delimited pattern to be matched, and a string. When the pattern
matches the string that follows the second slash will replace it. There are several rules and
exceptions and we will summarize those that we care for here, through a couple of examples.
The dot (.) matches any one character except newline.
To have the substitution happen everywhere it can happen, usefrilled.cs.indiana.edu%cat alpha #!/usr/bin/perl $a = "1234567890"; $a =~ s/./a/; print $a; frilled.cs.indiana.edu%./alpha a234567890frilled.cs.indiana.edu%
g (global) aftre the third slash.
The pattern can be bigger (or longer):frilled.cs.indiana.edu%cat alpha #!/usr/bin/perl $a = "1234567890"; $a =~ s/./a/g; print $a; frilled.cs.indiana.edu%./alpha aaaaaaaaaafrilled.cs.indiana.edu%
Parentheses can be used as memory elements:frilled.cs.indiana.edu%cat alpha #!/usr/bin/perl $a = "1234567890"; $a =~ s/../a/g; print $a; frilled.cs.indiana.edu%./alpha aaaaafrilled.cs.indiana.edu%
And they can include larger patterns:frilled.cs.indiana.edu%cat alpha #!/usr/bin/perl $a = "1234567890"; $a =~ s/(.)(.)/$2$1/g; print $a; frilled.cs.indiana.edu%./alpha 2143658709frilled.cs.indiana.edu%
To have the part between the last two slashes act as Perl code usefrilled.cs.indiana.edu%cat alpha #!/usr/bin/perl $a = "1234567890"; $a =~ s/(..)/$1+1/g; print $a; frilled.cs.indiana.edu%./alpha 12+134+156+178+190+1frilled.cs.indiana.edu%
e (evaluate) after the third slash.
Miscellaneous A few other things needed infrilled.cs.indiana.edu%cat alpha #!/usr/bin/perl $a = "1234567890"; $a =~ s/(..)/$1+1/ge; print $a; frilled.cs.indiana.edu%./alpha 1335577991frilled.cs.indiana.edu%
ReadParse are listed below.
Characters have (decimal) ASCII codes that can be obtain with ord.
frilled.cs.indiana.edu%cat alpha
#!/usr/bin/perl
@values = ('A', 'B', 'C', 'D', 'E');
foreach $value (@values) {
print $value, " has ASCII code: ", ord($value), "\n";
}
frilled.cs.indiana.edu%./alpha
A has ASCII code: 65
B has ASCII code: 66
C has ASCII code: 67
D has ASCII code: 68
E has ASCII code: 69
frilled.cs.indiana.edu%
ASCII codes can be turned into characters with chr.
frilled.cs.indiana.edu%cat alpha
#!/usr/bin/perl
@values = (65, 66, 67, 68, 69);
foreach $value (@values) {
print "ASCII code $value stands for: ", chr($value), "\n";
}
frilled.cs.indiana.edu%./alpha
ASCII code 65 stands for: A
ASCII code 66 stands for: B
ASCII code 67 stands for: C
ASCII code 68 stands for: D
ASCII code 69 stands for: E
frilled.cs.indiana.edu%
The hex function turns a hexadecimal value in a decimal one.
frilled.cs.indiana.edu%cat alpha
#!/usr/bin/perl
@values = (1, 10, 20, 100, 110, 111);
foreach $value (@values) {
print "$value in base 16 is equal to ", hex($value), " in base 10.\n";
}
frilled.cs.indiana.edu%./alpha
1 in base 16 is equal to 1 in base 10.
10 in base 16 is equal to 16 in base 10.
20 in base 16 is equal to 32 in base 10.
100 in base 16 is equal to 256 in base 10.
110 in base 16 is equal to 272 in base 10.
111 in base 16 is equal to 273 in base 10.
frilled.cs.indiana.edu%
You're now ready for Tuesday.
A348/A548