in reply to Re^2: Perl program to look into the phone directory and in case of a match, print the name along with the number
in thread Perl program to look into the phone directory and in case of a match, print the name along with the number
Hi dk27,
In the example shown, the result for last element "john" is missing.
Marshall seems to be correct in that the problem occurs when the file does not end on a newline character, however the final line should still be read even without the newline - confirm by printing $line before the next statement. The problem appears (at least to me) to be your regular expression instead. Note that \s+ requires there to be whitespace after the name, even for phone book queries, which have a name only. In most lines of input, that whitespace is the newline at the end of the line, since you're not chomping the lines, but the last line is missing the newline so it does not match the regular expression.
Try this regular expression instead, it works for me: /^\s*([a-zA-Z]+)(?:\s+(\d+))?/
Hope this helps,
-- Hauke D
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Perl program to look into the phone directory and in case of a match, print the name along with the number
by Marshall (Canon) on Feb 14, 2017 at 23:07 UTC | |
|
Re^4: Perl program to look into the phone directory and in case of a match, print the name along with the number
by dk27 (Novice) on Feb 15, 2017 at 06:09 UTC | |
by haukex (Archbishop) on Feb 15, 2017 at 09:54 UTC |