Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

file data vs data in script

by malaga (Pilgrim)
on Jan 24, 2005 at 09:29 UTC ( [id://424534]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

I'm trying to figure out why the correct data shows up when it's in the script, but when i reference the same data in a text file I get different (and wrong) results:
WORKS: my @accounts = split /\n/, <<EOT; 1,joe,123 First St. 2,mary,234 Second St. EOT my @purchases = split /\n/, <<EOT; 1,222,veg,01/05/05 1,444,fruit,02/04/05 1,555,bread,03/09/06 1,777,butter,05/07/05 1,888,spice,09/06/07 1,999,coffee,02/06/05 2,444,veg,01/01/06 EOT my $accounts_by_id = {}; for (@accounts) { my($id, $rest) = split /,/, $_, 2; $accounts_by_id->{$id}->{info} = $rest; } for (@purchases) { my($id, $rest) = split /,/, $_, 2; push @{$accounts_by_id->{$id}->{purchases}}, $rest; } for (@accounts) { my($id, $rest) = split /,/, $_, 2; { my @five = splice @{$accounts_by_id->{$id}->{purchases}}, 0, 5; last unless @five; print "$id,$accounts_by_id->{$id}->{info},", join(',', @five), "\n"; print "<br>"; redo if @five == 5; } } RETURNS THE FOLLOWING: 1,joe,123 First St.,222,veg,01/05/05,444,fruit,02/04/05,555,bread,03/0 +9/06,777,butter,05/07/05,888,spice,09/06/07 1,joe,123 First St.,999,coffee,02/06/05 2,mary,234 Second St.,444,veg,01/01/06 DOESN'T WORK: open( XLSFILE, $file ) or die "could not open $file $!"; open( XLSFILE2, $file2 ) or die "could not open $file $!"; my @accounts; my $accounts; my @purchases; my $purchases; while (<XLSFILE>){ @accounts = split /\n/,$_; foreach $accounts(@accounts) } while (<XLSFILE2>){ @purchases = split /\n/,$_; } my $accounts_by_id = {}; for (@accounts) { my($id, $rest) = split /,/, $_, 2; $accounts_by_id->{$id}->{info} = $rest; } for (@purchases) { my($id, $rest) = split /,/, $_, 2; push @{$accounts_by_id->{$id}->{purchases}}, $rest; } for (@accounts) { my($id, $rest) = split /,/, $_, 2; { my @five = splice @{$accounts_by_id->{$id}->{purchases}}, 0, 5; last unless @five; print "$id,$accounts_by_id->{$id}->{info},", join(',', @five), "\n"; print "<br>"; redo if @five == 5; } } RETURNS THE FOLLOWING: 1,joe,123 First St. 2,mary2,234 Second St. 1,222,veg,01/05/05 1,444,fruit,02/04/05 1,555,bread,03/09/06 1,777,butter,05/07/05 2,444,veg,01/01/06 2,444,veg,01/01/06 2,444,veg,01/01/06 2,444,veg,01/01/06 2,444,veg,01/01/06 2,444,veg,01/01/06 2,mary2,234 Second St.,444,veg,01/01/06
Thank you!
Malaga

Readmore tags added by davido.

Replies are listed 'Best First'.
Re: file data vs data in script
by Anonymous Monk on Jan 24, 2005 at 09:37 UTC
    while (<XLSFILE>) already reads the file line by line, no point in splitting on newlines again (there will be just one line, ending in a newline). If you want all lines in an array, just do: @accounts = <XLSFILE>;, without an explicite while.
      and don't forget to chomp @accounts to get rid of unwanted newlines...

      Paul

Re: file data vs data in script
by CountZero (Bishop) on Jan 24, 2005 at 10:56 UTC
    If you want to have similar results betweend data in a file and data in you script, put your data after a __DATA__ token at the end of the script and read them through a <DATA> filehandle.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-25 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found