http://qs1969.pair.com?node_id=569812

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

Hello esteemed Monks of perl
I am being stumped on why my data is being slurped in all at once instead of line by line. I am trying to pull out only the name and id number, but my array is being loaded with the entire file. Also do I need to clear the array after each record read. I know it must be something simple because I've been staring at it for an hour.
Thanks, for your wisdom
#!/prod/bin/perl use strict; use warnings; use Data::Dumper; undef $/; my @data; my $counter = 0; while (my $line = <DATA>) { @data = split (/\t/, $line); print Dumper \@data; $counter = $counter+1; print "$counter, $data[11], $data[12]\n"; } __DATA__ LIMITS LIMITS1 LIMITS2 LIMITS3 LIMITS4 LIMITS5 LIMIT +S6 LIMITS7 UPDATEDATE STEP2 DESC2 APPR2 APPRID2 +OWNR2 OWNRID2 Limits less than $1K $1K-50K $50k-$250K more than $250K + $250K-$1M $1M-$5M more than $5M 8/16/2006 2:14:00.000 PM + 111111 Cost Center 1 Stien, Franklin N 000204 Stien, Fra +nklin N 000204 Limits less than $1K $1K-50K $50k-$250K more than $250K + $250K-$1M $1M-$5M more than $5M 8/16/2006 2:14:00.000 PM + 222222 Cost Center 2 Bear, Izzy 000598 Bear, Izzy 000 +598 Limits less than $1K $1K-50K $50k-$250K more than $250K + $250K-$1M $1M-$5M more than $5M 8/16/2006 2:14:00.000 PM + 333333 Cost Center 3 Bear, Fozzy 000485 Davis, Sammy +000404 Limits less than $1K $1K-50K $50k-$250K more than $250K + $250K-$1M $1M-$5M more than $5M 8/16/2006 2:14:00.000 PM + 444444 Cost Center 4 Frog, Kermit T 000990 Frog, Kermit +T 000990 Limits less than $1K $1K-50K $50k-$250K more than $250K + $250K-$1M $1M-$5M more than $5M 8/16/2006 2:14:00.000 PM + 555555 Cost Center 5 Cleavland, Grover 000563 Cleavland, + Grover 000563

-- Grey Fox
Perl - Hours to learn lifetime to master.

Replies are listed 'Best First'.
Re: Why is file being slurped in instead of line by line?
by Tanktalus (Canon) on Aug 26, 2006 at 16:31 UTC

    It's because you're undefing $/. That's what the perl runtime uses to determine the line-break character (or string). (Well, technically, line-end string, but let's not get carried away.) Comment out that line, and you'll get what you expect.

    If you need it for other things, you should localise your changes - see local. e.g.:

    { # I want to slurp here. local $/; # sluuuuuuuurrrrrp... } # $/ is back to normal here.
      Thanks; That's what I get for stealing code from something else I had done where I wanted to get rid of the line feed.
      I had a teacher once who used to say. If you can't find something in 5 minutes it is probably a stupid mistake (ie a missing ; or something).
      -- Grey Fox
      Perl - Hours to learn lifetime to master.
Re: Why is file being slurped in instead of line by line?
by ahmad (Hermit) on Aug 26, 2006 at 16:35 UTC

    Hello ,

    To make your code works fine ,

    1. remove "undef $/;" line ... which makes the whloe data as one line ( removes line sperator )

    2. remove "my @data;" from the top ... and declare it inside the loop ... which means ... it re-assigned a new data for each loop like

    my @data = split (/\t/, $line);

    HTH

      Point #2 is not necessary to his issue, nor will it actually fix a problem in his code execution. That already "works fine", if not optimally.

        ahmad's point is however a good one. It is generally good practice to limit the scope of variables as far as is reasonable. It reduces the chance that a variable will be inadvertantly modified and the purpose of the variable is generally clearer.


        DWIM is Perl's answer to Gödel