sstruthe has asked for the wisdom of the Perl Monks concerning the following question:

Hi my code below which is sanatised and might not work here runs fine reading a file with $1 $2, ie two elements first one is key and second is value into a hash. But now I want to have more than one value I would like $1{key} $2,$3,$4 so I assume hash of arrays.

my $input_file = "dat.txt"; our %dedicated; open(DATA, "<$input_file" ) or die; while ( my $line = <DATA> ) { chomp $line; my ($a, $b) = split ' ', $line; $dedicated{$a} = $b; } keys %dedicated; while (my ($k, $v) = each %dedicated ) { chomp($k, $v); blah } } }

How do I change from reading in and using a hash, to a hash of arrays on a file like. names1 john,bob,stu names2 mike,john,bob names3 blah. ...... Many thanks

Replies are listed 'Best First'.
Re: read a file and build HoA
by Athanasius (Archbishop) on Dec 10, 2015 at 12:19 UTC

    Hello sstruthe,

    I think you’re looking for something like this:

    #! perl use strict; use warnings; use Data::Dump; my %dedicated; while (my $line = <DATA>) { chomp $line; my ($key, $values) = split ' ', $line; push @{ $dedicated{$key} }, $_ for split ',', $values; } dd \%dedicated; __DATA__ names1 john,bob,stu names2 mike,john,bob names3 blah

    Output:

    22:16 >perl 1478_SoPW.pl { names1 => ["john", "bob", "stu"], names2 => ["mike", "john", "bob"], names3 => ["blah"], } 22:17 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: read a file and build HoA
by Discipulus (Canon) on Dec 10, 2015 at 12:20 UTC
    hello sstruthe

    Perl is intelligent enogh to let you to say "A scalar and A list = Another List" and just works:
    $scalar, @array = split ' ', $_
    and you get the key (scalar) and the rest (a list), obviously after you chomped the line of the file.

    Note also that DATA is a special filehandle: is the file containing the source code of your running script, seeked at the point where the special token __DATA__ is found. So if the data is in a separte file use another name for the filehandle or even better use the modern form: open my $file_handle_name, '<', $file_name or die "unable to open $file_name for read!" as in the right part of the docs (perlintro and peropentut)

    As final note, why you use our (it is not the plural form of my)?

    The oneliner version also can be useful:
    perl -MData::Dump -lane "my($scalar,@array)=split; dd $scalar; dd @arr +ay" exampledata.txt "A" ("another", "awesome", "awful") "E" ("example", "estinguished")

    L*

    UPDATE: if you want directly the HoA created take a look at @F in special variables:
    perl -MData::Dump -lane "$HoA{$F[0]} = [ @F[1..$#F] ]; END{dd %HoA}" e +xampledata.txt ( "A", ["another", "awesome", "awful"], "E", ["example", "estinguished"], )


    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: read a file and build HoA
by sstruthe (Novice) on Dec 10, 2015 at 14:02 UTC

    You are awesome thanks, just what I was looking for tried a few versions in my head of this but seeing the code makes sense now.