in reply to counting and moving groups

split, join, and splice are your friends:
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"; redo if @five == 5; } }

Replies are listed 'Best First'.
Re^2: counting and moving groups
by malaga (Pilgrim) on Jan 23, 2005 at 20:38 UTC
    thank you! much much appreciated.
    Malaga