Perl hash or array

Posted on 2011-05-13
Last Modified: 2012-05-11
I want to create a hash of list in the following fashion:
keys     values
gr4       john
gr5       alice
gr4       mike
gr2       andrew
gr5       adam
gr4       don

It should create a hash of lists in a sequential fashion on the fly (dynamically) as follows

%hash = (gr4 => [john,mike,don], gr2 =>[andrew], gr5 =>[alice,adam])

I want as least conditions as possible.  Hopefully should get a way to add to empty lists and existing lists in the same condition
Question by:farzanj
    LVL 10

    Assisted Solution

    This should work
    %hash = (gr4 => [qw(john mike don)], gr2 =>['andrew'], gr5 =>[qw(alice adam)]);
    and if you want to add more you can do something like thisL
      push(@{$hash{gr4}}, "farzan");
      push(@{$hash{gr4}}, qw(peter paul));
      my @lists = qw(julie jane);
      push(@$hash{gr4}, @lists);

    For an empty list:
      $hash{gr0} = [];

    Good luck!

    LVL 26

    Accepted Solution

    This should do it...

    The most direct would be using this pair of lines where you want to add values:

    $hash{$key} = [] unless $hash{$key};
    push @{$hash{$key}}, $val;

    If you need to do that a lot, it might be worth creating a sub like this:

    # used wherever needed
    add_val($key, $val);

    # defined once
    sub add_val {
        my ($key, $val) = @_;
        $hash{$key} = [] unless $hash{$key};
        push @{$hash{$key}}, $val;

    Please let me know if you need more assistance...
    LVL 31

    Author Closing Comment

    Thank you so much for your precious time.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now