Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
P is for Practical
 
PerlMonks  

What is the error in my script or file setup?

by Anonymous Monk
on Jul 24, 2001 at 14:12 UTC ( #99278=note: print w/ replies, xml ) Need Help??


in reply to File Input and Output

How come this never displays "Yes!" ? input.dat:

fred 20.4 harv 5.6 tony 5.13 dennis 401.1
My code:
open(FILE, "input.dat"); @list; while(<FILE>) { chomp; push @list, $_; } $i="harv"; $loc=-1; $cnt=0; foreach $list(@list) { if($i eq $list) { print "YES!\n"; } }

Edit kudra, 2001-07-25 Changed title


Comment on What is the error in my script or file setup?
Select or Download Code
Re: Re: File Input and Output
by Hofmator (Curate) on Jul 24, 2001 at 14:39 UTC

    mmhhh, different possibilites that this doesn't work

    • try if this works:
      use strict; use warnings; my @list; while(<DATA>) { chomp; push @list, $_; } my $i="harv"; foreach my $list (@list) { if($i eq $list) { print "YES!\n"; } } __DATA__ fred 20.4 harv 5.6 tony 5.13 dennis 401.1
      The DATA section behaves exactly like a file - inside your file and you don't have to open it. If it does then
    • check if your file opened correctly (and specify the opening mode): open (FILE, "<", "input.dat") or die "Couldn't open input.dat: $!";
      The die will abort the program if 'input.dat' couldn't be opened. The $! variable contains an error message and is set automatically if an error during the open occured.
    • You should use strict; and use warnings; (equivalent to perl -w). It protects you from typing mistakes - there aren't any in your example, this is more general advice. And it shows useful warnings if you do some possibly 'stupid' things.

    -- Hofmator

Re: Re: File Input and Output
by tye (Cardinal) on Jul 24, 2001 at 14:45 UTC

    Trailing space after "harv" in your input file? Or perhaps a carriage return and you are on Unix?

            - tye (but my friends call me "Tye")
Re: What is the error in my script or file setup?
by tachyon (Chancellor) on Jul 30, 2001 at 18:37 UTC

    This works:

    @list = qw( fred 20.4 harv 5.6 tony 5.13 dennis 401.1 ); $i="harv"; foreach $list(@list) { if($i eq $list) { print "YES!\n"; } }

    Suggest you add open (FILE, "input.dat") or die "Oops can't open input.dat Perl says $!\n"; which will *probably* tell you that perl cna't find your file. Also you don't need to push your data into an array as you do but that is by the by.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: What is the error in my script or file setup?
by Anonymous Monk on Sep 01, 2001 at 23:08 UTC
    I recently had a similar problem. The error is in your comparison ($i eq $list). Then the loop gets to the point that you are expecting a "Yes", the values of your variables are $i="harv" $list="harv\n" I solved my problem by changing $i to include the carriage return. You could also solve this problem by using pattern matching rather than a straight comparison, or by chopping off the last character of the input lines.
      It looked like he was chomping in his code. He was chomping the list as he added each element to the array. That should take care of the carriage return that you are referring to.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://99278]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2014-04-24 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls