Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

why can't I just cat the file???

by convenientstore (Pilgrim)
on Feb 07, 2008 at 04:04 UTC ( #666729=perlquestion: print w/ replies, xml ) Need Help??
convenientstore has asked for the wisdom of the Perl Monks concerning the following question:

What is up w/ below code?
Why can't this simple script just spit out what it sees in data?
#!/usr/bin/perl -w use strict; #my @yahoo = map { split(/\n/, $_) } <DATA>; my @yahoo = <DATA>; while (@yahoo) { chomp; print $_; } __END__ hi nus mine hi no mine ### hi on mine ## hi nus mine hi hi hi hi hi hi
running this I get
Use of uninitialized value in print at ./split_file.pl line 12, <DATA> + line 9. Use of uninitialized value in scalar chomp at ./split_file.pl line 11, + <DATA> line 9. Use of uninitialized value in print at ./split_file.pl line 12, <DATA> + line 9. Use of uninitialized value in scalar chomp at ./split_file.pl line 11, + <DATA> line 9. Use of uninitialized value in print at ./split_file.pl line 12, <DATA> + line 9. Use of uninitialized value in scalar chomp at ./split_file.pl line 11, + <DATA> line 9. Use of uninitialized value in print at ./split_file.pl line 12, <DATA> + line 9. Use of uninitialized value in scalar chomp at ./split_file.pl line 11, + <DATA> line 9. Use of uninitialized value in print at ./split_file.pl line 12, <DATA> + line 9. Use of uninitialized value in scalar chomp at ./split_file.pl line 11, + <DATA> line 9. Use of uninitialized value in print at ./split_file.pl line 12, <DATA> + line 9. Use of uninitialized value in scalar chomp at ./split_file.pl line 11, + <DATA> line 9. Use of uninitialized value in print at ./split_file.pl line 12, <DATA> + line 9. Use of uninitialized value in scalar chomp at ./split_file.pl line 11, + <DATA> line 9. Use of uninitialized value in print at ./split_file.pl line 12, <DATA> + line 9. Use of uninitialized value in scalar chomp at ./split_file.pl line 11, + <DATA> line 9. Use of uninitialized
Putting dumper gets me,
use strict; use Data::Dumper; #my @yahoo = map { split(/\n/, $_) } <DATA>; my @yahoo = <DATA>; #print "@yahoo\n"; print Dumper(@yahoo);
./split_file.pl $VAR1 = 'hi nus mine '; $VAR2 = 'hi no mine '; $VAR3 = '### '; $VAR4 = 'hi on mine '; $VAR5 = '## hi nus mine '; $VAR6 = 'hi hi hi '; $VAR7 = 'hi '; $VAR8 = 'hi '; $VAR9 = 'hi ';

Comment on why can't I just cat the file???
Select or Download Code
Re: why can't I just cat the file???
by eric256 (Parson) on Feb 07, 2008 at 04:10 UTC

    While doesn't take an array or list, it takes a condition. You want while (<DATA>) or for (@yahoo)


    ___________
    Eric Hodges
      I'd like to expand upon this correct answer. It seems like a really weird distinction if you aren't familiar with Perl's filter-writing magic.
      while (<DATA>) { ... }
      One might think that the above code is equivalent to this:
      @foo = <DATA>; while (@foo) { ... }
      In reality, due to Perl's magical way of helping you implement simple STDIN-to-STDOUT text file filters, the first snippet is rephrased internally to mean the following:
      while (defined ($_ = readline(*DATA))) { ... }
      Thus, while () isn't taking a list, or even what looks like a list. It's taking a condition, the condition being the successful ability to read a single line from a filehandle (and if successful, assigning the line to $_ for checks inside the loop).

      --
      [ e d @ h a l l e y . c c ]

        wow, well done. thanks
        Ed, this is excellent stuff,
        Your reply had me read frantically more on while and defined and I have now better understanding on true and defined(as well as while)
        Just a quick question though, how/where did you know that while is doing that internally?
        I cannot find any offical doc saying that's what it does. Your explanation helped me a lot but just wondering if I am not reading the right stuff or I am just not reading it carefully
Re: why can't I just cat the file???
by jepri (Parson) on Feb 07, 2008 at 04:12 UTC
    foreach (@yahoo) { chomp; print $_; }

    That should work better.

    ___________________
    Jeremy
    I didn't believe in evil until I dated it.

      thank you guys!!
      Or even print <DATA>;
Re: why can't I just cat the file???
by johngg (Abbot) on Feb 07, 2008 at 10:47 UTC
    print without arguments defaults to operating on $_ and you could also chomp all of your lines in one go. If you don't mind destroying the array you could do this (using defined to cope with blank lines)

    chomp( my @yahoo = <DATA> ); print while defined( $_ = shift @yahoo );

    I hope this is of interest.

    Cheers,

    JohnGG

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-12-20 00:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls