in reply to read multiple record data and populate in a new data stucture
This appears to do what you want:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my ( $number, %accounts ); while ( <DATA> ) { chomp; if ( /^A,/ ) { ( undef, $number, my @fields ) = split /,/, $_, -1; @{ $accounts{ $number } }{ qw/ surname firstname job balance / + } = @fields; } elsif ( /^B,/ ) { my $transaction = ( split /,/ )[ 1 ]; push @{ $accounts{ $number }{ transactions } }, $transaction; } } print Dumper \%accounts; __DATA__ A,01,Newton,Issac,Physics, 5.6 B,144.56 B,1034.65 B,12.23 A,34,Einstein,Albert,Physics,12.22 B,2346.89 B,876.23 B,7656.81 B,745.21 B,25.63 B,89.56 B,7789.23 A,78,Feynman,Richard,Physics,-17.34 B,7412.36 B,2589.35 B,95.32 B,12.85 B,789.68
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: read multiple record data and populate in a new data stucture
by fseng (Novice) on Jun 30, 2009 at 02:34 UTC | |
by stevemayes (Scribe) on Jun 30, 2009 at 06:03 UTC | |
by Marshall (Canon) on Jun 30, 2009 at 05:34 UTC |