hashes of arrays

Posted on 2009-04-16
Last Modified: 2012-05-06
I have a file like this:


and was wondering how if it's possible for me to create a hash, whose keys are giong to be the first element in each line of this file and the rest will just be values for that key

also after i have this created i need to grep each array in in the hash to see if the second element matches any element is found in another array that looks like this:

array = [789633, 963852, 159753]

can somebody help me? thanks! :D
Question by:cucugirl
    LVL 84

    Expert Comment

    my %hash;
    while( <DATA> ){
      my @array = split/:/;
      $hash{shift @array} = [@array];

    my $array=[789633, 963852, 159753];
    my %array;
    for( values %hash ){
    print "@$_\n" if exists $array{$_->[1]}
    LVL 5

    Accepted Solution

    Check the this script if this helps.
    If you name the script as and text file as zyx.txt
    then run as:
    $ xyz.txt

    Do let me know, if you need any clarifications.

    my %fileData;
    @arr = (789633, 963852, 159753, 741852);
            @line = split(/:/, $_);
            $fileData{shift @line} = [@line];
    for $value (@arr)
            for $line ( keys %fileData )
                    print $value." ".$fileData{$line}[1]."\n";
                    if ( $value == $fileData{$line}[1])
                            print "\Found! $value\n";

    Open in new window

    LVL 84

    Expert Comment

    Comparing with every element of an array can be a lot slower than looking up a hash.

    And something seems absurd about searching through a hash looking for a value.
    Is there a reason why you want to index by $line[0] instead of by $line[1]?

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    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…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    746 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

    18 Experts available now in Live!

    Get 1:1 Help Now