So, to build on the otherwise fine code of the first reply:
use strict; use warnings; use Data::Dumper; my @clients; # make this an array, not a hash; my @fields = qw/surname firstname town/; while (<DATA>) { next if ( /^Surname/ or /^-/ ); my @values = unpack 'A13A16A20', $_; my %entry = map { $fields[$_] => $values[$_] } ( 0..$#fields ); push @clients, \%entry; } print Dumper(\@clients); __DATA__ Surname L20, First Name L20, Town L20 -------------------------------------------------- Chaplin Charlie Basel Estevez Emilio Santa Manica Sarte Jean Paul Montmarte Rikard Frank Amsterdam Rodin Paul Montmarte
(update: it does seem odd that the field widths in the column headings do not match the actual widths used in the data and in the unpack spec. It would be better if the headings were consistent with the data, but that's a minor point...)
In reply to Re: How to read data file into an array of hashes
by graff
in thread How to read data file into an array of hashes
by fseng
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |