Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

errors

by snowrider (Pilgrim)
on Feb 02, 2001 at 02:46 UTC ( [id://55885]=perlquestion: print w/replies, xml ) Need Help??

snowrider has asked for the wisdom of the Perl Monks concerning the following question:

i am getting an error does anyone know what this means and how i can fix it? here is the code
#!/opt/perl5/bin/perl -w use strict; my %lines; my @dupes; while($_ =<> and $_ ne "quit\n"){ $lines{$_}++; if($lines{$_} == 2){ push @dupes, $_; } } if(@dupes){ print "Here are your duplicate lines\n\n"; foreach(@dupes){ print $_; } } else { print "\nNo Duplicates Found!\n"; }
and here is the error i am getting
Value of <HANDLE> construct can be "0"; test with defined() at day1_te +st.1.pl line 8.
snowrider

Replies are listed 'Best First'.
Re: errors
by btrott (Parson) on Feb 02, 2001 at 02:54 UTC
    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. :)
Re: errors
by dws (Chancellor) on Feb 02, 2001 at 03:54 UTC
    Your intent may be expressed more clearly by
    while ( <> ) { chomp; last if /quit/i; ... }
Re: errors
by autark (Friar) on Feb 02, 2001 at 02:56 UTC
    perl is complaining about your while-test. You have to test with defined, like this: while( defined($_ = <>) && $_ ne "quit\n" ) {} The reason, as stated in the error message, is because the <> may generate a "0", and that would evaluate to false. With defined however, it will evaluate to true.

    Newer perls usually puts the defined() around any such simple constructs as $_ = <SOME_HANDLE>. (within a while). Take this as an example:

    perl -MO=Deparse -le 'while($_ = <>) {}' -e syntax OK while (defined($_ = <ARGV>)) { (); }
    However, this is not done with more complicated constructs such as yours.

    Autark.

Re: errors
by InfiniteSilence (Curate) on Feb 02, 2001 at 02:55 UTC
    Not sure why your code is breaking but perhaps a better way to do that while loop (rather than scalar assignments on $_ would be the following:
    perl -e "while (<>) { last if m/^quit$/ }"

    Celebrate Intellectual Diversity

Re: errors
by snowrider (Pilgrim) on Feb 02, 2001 at 03:03 UTC
    thanks fellow monks snowrider

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://55885]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found