It's telling you that when you read a line from a file (the
<HANDLE> construct), the value of that line can be "0"
(0 without a newline following it). This would evaluate to false,
which probably isn't what you want, because you could exit
the loop too early this way (before eof).
So what you should do is test for the defined-ness of the value:
while (defined($_ = <>) and $_ ne "quit\n") {
...
By the way, you really don't need to build the @dupes array in the
above example; you have all the information you need in
your hash. The duplicate lines are the keys in the hash where
the value is greater than 1:
my @dupes = grep $lines{$_} > 1, keys %lines;
Just something to think about.
And by the way, here's a one-liner that does basically the
same thing:
% perl -ne 'END { print grep $s{$_}>1, keys %s } last if $_ eq "qu
+it\n"; $s{$_}++' file
I'm sure someone else could shorten this. :)
In reply to Re: errors
by btrott
in thread errors
by snowrider
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.