Hello Monks,

I have a script that reads from stdin, expecting the first line to contain column headings (delimited by commas) that will be stored in an array for use as hash keys later. Each remaining line should contain the values for an individual record (also delimited by commas). I have an array of hashes (LoH) that stores the data from each line in a record.
#!/usr/bin/perl -w use strict; # first line has column headings my @fieldnames = split /,/, <>; # @entries = ( { Last => 'zacks', First => 'evan', ... }, ... ) my @entries; # create and store a record for each line of input while (<>) { my %h; @h{@fieldnames} = split /,/; push @entries, \%h; } foreach my $h (@entries) { foreach my $k (@fieldnames) { print "$k: $h->{$k}\n" } } __DATA__ sample input: Last,First,Address,Apt,City,St,Zip,Email,Notes zacks,evan,123 main,,ann arbor,mi,48104,evan@zacks.org,perlmonks++
Given the pleasant spring weather as of late, I started thinking about golf. Then I came up with a few different ways to create and store each record. Some of mine are below. The problem is creating the mapping from the column name (from @fieldnames) to values (retrieved from split).

Does anyone have some other solutions?
# pretty much the same as above (weak, i know) while (<>) { my %h; push @entries, do { my %h; @h{@fieldnames} = split /,/; \%h }; } # different, but still using a temp var while (<>) { my @f = @fieldnames; push @entries, { map { shift @f => $_ } split /,/ }; } # hmm. gets rid of the temp var, but it's pretty ugly. while (<>) { push @entries, { map { push @fieldnames, shift @fieldnames; $fieldnames[-1] => $_ } split /,/ }; } # tye++ (see http://www.perlmonks.org/index.pl?node_id=44763) use mapcar; while (<>) { push @entries, { mapcar { @_ } \@fieldnames, [ split /,/ ] }; }
Golf (or at least other solutions), anyone?

--sacked

In reply to golfing hash slices by sacked

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.