@me which contains four elements: your first name, your last name, your age, and the name of the town you are from.
@me = ("Mike", "Toppa", 30, "Newport");
for ($count = 0; $count <= $#me; $count++) {
print "$me[$count]\n";
}
$sumAll
$sumEven
$count to iterate a for loop 10 times.
$count to $sumAll
$count to $sumEven if $count is an even number.
$sumAll and $sumEven
$sumAll = 0;
$sumEven = 0;
for ($count = 1; $count <= 10; $count++) {
$sumAll += $count;
$sumEven += $count if ($count % 2 == 0);
}
print "The value of \$sumAll is $sumAll\n";
print "The value of \$sumEven is " . $sumEven;
@me (from question 1) in reverse alphabetical order. Then use a foreach loop to print each element.
@me = ("Mike", "Toppa", 29, "Newport");
@me = reverse(sort(@me));
foreach $item (@me) {
print "$item\n";
}
@pages = qw(index.html chapter1.html chapter4.html chapter8.html chapter1.html index.html chapter4.html chapter3.html); $previous = "";
@pagesSorted that is an alphabetically sorted copy of @pages
$previous as the last statement inside the loop. Then, as the first statement inside the loop, you can compare the current array element to the value of $previous.
@pages = qw(index.html chapter1.html chapter4.html chapter8.html chapter1.html index.html chapter4.html chapter3.html);
$previous = "";
@pagesSorted = sort(@pages);
foreach $page (@pagesSorted) {
print "$page\n" unless ($page eq $previous);
$previous = $page;
}
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime; $daysLastYear = 365 - 359; $daysThisYear = $yday + 1; $daysSince = $daysLastYear + $daysThisYear; print "There have been $daysSince days between December 25, 1999 and today.\n";
$string = "abcde"; $result = $string =~ /abc/; print "$result\n";
In this example, the number "1" is printed to the screen. This is because the variable $string contains the pattern "abc", so the =~ operator returns true. The result of the pattern matching test is assigned to $result, and since "true" is represented by "1" in Perl, the number "1" is printed to the screen.
$string = "abcde"; $result = $string !~ /xyz/; print "$result\n";
In this case the number "1" is again printed to the screen, because the pattern "xyz" was not found in the variable $string.
$string = "abcde"; $string =~ /abc/; print "$&\n";
If the pattern was not matched (e.g. if we tried to match "mnop"), then the value of $& is not changed. $& is very handy when you need to re-use a matched pattern (e.g. if you want to print it).
The + character has a special meaning when used in the context of pattern matching. It means "try to match one or more of the preceding character." For example:
$string = "abbbcde"; $string =~ /ab+/; print "$&";
The * character is similar, but it attempts to match zero or more occurrences of the preceding character. For example:
$string = "abcde"; $string =~ /aq*b/; print "the matched pattern in \$string: $&\n"; $string2 = "aqqqbcde"; $string2 =~ /aq*b/; print "the matched pattern in \$string2: $&\n";
+ and * are greedy. That is, they will try to match as many characters as possible. For example, + won't stop after it's matched "ab". It will only stop when it's matched every consecutive "b" in the string.
? is similar to * but it matches only zero or one occurrence of the preceding character:
$string = "abcde";
$string =~ /aq?b/;
print "the matched pattern in \$string: $&\n";
$string2 = "aqqqbcde";
if ($string2 =~ /aq?b/) {
print "the matched pattern in \$string2: $&\n";
}
else {
print "the pattern for \$string2 was not matched.\n";
}
The pattern match attempt on $string2 will return false, since there is more than one "q" between "a" and "b". Note the use of the if...else statement in this code. The pattern match on $string2 failed, but if we did not use an if conditional before trying to print $&, we would have simply printed the value of the last successful match, which was for $string, not $string2. Be careful of this when using $& - it contains the last successful match, which is not necessarily the same as the most recent attempt to match, since the most recent attempt may have failed, as it did here!
$string = "abcde"; $string =~ /a[Bb]c/; print "the matched pattern in \$string: $&\n"; $string2 = "aBcde"; $string2 =~ /a[Bb]c/; print "the matched pattern in \$string2: $&\n";
You can also use pattern matching characters in combination with each other:
$string = "abBbbBcde"; $string =~ /a[Bb]+c/; print "the matched pattern in \$string: $&\n";
$string = "ab+cde"; $string =~ /ab\+c/; print "the matched pattern in \$string: $&\n";
This also applies to the other metacharacters that we'll discuss tonight and next week.
$string = "abcd"; $string =~ /^abc/; print "the matched pattern in \$string: $&\n";
$ is used to match a pattern at the end of a string. You can use them in combination to force matching of the entire string:
$string = "abcd"; $string =~ /^abc$/; print "the matched pattern in \$string: $&\n";
In this case, the pattern match fails since "d" is the last character in the string, not "c".
$string = "ab4cde"; $string =~ /[0-9]/; print "the matched pattern in \$string: $&\n";
To match lowercase letters:
$string = "123a456"; $string =~ /[a-z]/; print "the matched pattern in \$string: $&\n";
You can use [A-Z] to match uppercase letters. Lastly, you can use these in combination with each other, and with the other special characters to, for example, match any consecutive combination of letters or numbers:
$string = "Mike59:::"; $string =~ /[0-9a-zA-z]*/; print "the matched pattern in \$string: $&\n";
Page 100 of Teach Yourself Perl lists the character range escape sequences. They are:
\w matches any "word" character (i.e. an underscore, or any letter or digit)
\W matches any character that is not a "word" character
\d matches any digit character
\D matches any character that is not a digit
\s matches any "whitespace" character (i.e. a space, tab, newline, carriage return, or form feed)
\S matches any non-whitespace character
We can re-write the previous example more concisely now:
$string = "Mike59:::"; $string =~ /\w*/; print "the matched pattern in \$string: $&\n";
Note that if we change the example to instead find any non-word characters, no match is printed:
$string = "Mike59:::"; $string =~ /\W*/; print "the matched pattern in \$string: $&\n";
You might expect Perl to match the 3 colons at the end, since they are the first "non-word" characters in the string. Why doesn't it? Remember that the * metacharacter is looking for zero or more non-word characters. All pattern match tests read the string from left to right. So, the pattern match sees the first character "M" and performs the test: "Have I found zero or more non-word characters?" The answer is yes: zero non-word characters have been found. The test is satisfied, so the pattern matching ceases. You can match the ":::" pattern by changing the test to \W+ which looks for one or more non-word characters.
$string = "abbbbcd";
$string =~ /ab{1,3}/;
print "the matched pattern in \$string: $&\n";
To specify an exact number of occurrences, provide a single number:
$string = "abbbbcd";
$string =~ /ab{2}/;
print "the matched pattern in \$string: $&\n";
To specify a minimum, but not a maximum, leave off the upper bound:
$string = "abbbbcd";
$string =~ /ab{1,}/;
print "the matched pattern in \$string: $&\n";
Lastly, to specify a maximum but not a minimum, use a 0 as the lower bound:
$string = "acd";
$string =~ /ab{0,2}/;
print "the matched pattern in \$string: $&\n";
$string = "Mike::Joe:Mary:::Fred"; @names = split(/:+/, $string); print "@names\n";
$question
$question contains the word "please", print the message "Thank you for being polite", otherwise print the message "That was note very polite"