Contrary to toolic's suggestion, I think you are right to try for an array of hashes, because you probably can't count on the "last name" field to be unique across all records.

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

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.