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.


In reply to file data vs data in script by malaga

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.