Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

perl array of hashes help

by ragilla (Initiate)
on Apr 30, 2012 at 11:38 UTC ( #968045=perlquestion: print w/ replies, xml ) Need Help??
ragilla has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have array of hashes and each key has array like below.
@array1 = ( { 'url' => [ 'wp-content/uploads/2012/04/HB-177-Veto-Letter.pdf' ], 'bill' => [ 'CS3', 'HB', '177', 'Inmate Reentry', 'Porth', '3/9/12', '3/30/12', 'VETO' ] }, { 'url' => [ 'uploads/2012/03/3.29.12-Transmittal-Letter-3.pdf' ], 'bill' => [ 'CS1', 'HB', '697', ' School Board of Sumter County', 'McBurney', '3/9/12', '3/16/12', 'VETO' ] }, { 'url' => [ 'tent/uploads/2012/04/HB-865-Veto-Letter.pdf' ], 'bill' => [ '', 'HB', '865', 'Pinellas Suncoast Transit Authority, Pinellas County', 'Hooper', '3/9/12', '3/30/12', 'VETO' ] }, { 'url' => [ 't/uploads/2012/04/4.13.12-HB-917-Veto-Letter.pdf' ], 'bill' => [ '', 'HB', '917', 'Jurisdiction of the Courts', 'Bileca', '3/7/12', '3/30/12', 'VETO' ] }, { 'url' => [ 's/2012/04/HB-1117-Veto-Letter.pdf' ], 'bill' => [ 'CS1', 'HB', '1117', 'Conservation of Wildlife', 'Harrison', '3/9/12', '3/30/12', 'VETO' ] } );

Now from the above array i want to grep for 1117 which should be value of key 'bill' of the different hashes.. i can do that using for loop, but i hope there would be some one liner to grep all those hashes into an array whose 'bill' key value contains 1117..

so please help me here ...

thanks in Advance

Comment on perl array of hashes help
Download Code
Re: perl array of hashes help
by trizen (Friar) on Apr 30, 2012 at 13:04 UTC
    my @hashes = grep { '1117' ~~ $_->{bill} } @array;

    or for an older version of Perl, you can use:
    use List::Util qw(first); my @hashes = grep { first { $_ eq '1117'} @{$_->{bill}} } @array;
      Hi Trizen, Thanks for your help , the code is working fine, can you please explain the two codes, similar to first one when i tried like this grep { $_->{bill} =~ '1117'} @array dint worked.. so what does ~~ this mean could you please explain me clearly. regards Sridhar
        Hi Sridhar! Please take a look at the smart match operator. As you can see, 'item' ~~ \@array returns true if the 'item' exists inside the array passed as an array reference.

        Your code doesn't work because $_->{bill} is a reference to an array, so what you are actually trying is something like this: 'ARRAY(0x886b7f8)' =~ /1117/; (i.e.: return true if the left side contains the substring from the right side).

        Of course, if you're trying to match a substring inside an array, you can use: grep { qr/1117/ ~~ $_->{bill} } @array;
Re: perl array of hashes help
by mrguy123 (Hermit) on Apr 30, 2012 at 13:05 UTC
    Hi,
    First of all it will make it easier to understand your question if you put the code in code tags like below:
    @array1 = ( { 'url' => 'wp-content/uploads/2012/04/HB-177-Veto-Letter. +pdf' , 'bill' => 'CS3', 'HB', '177', 'Inmate Reentry', 'Porth', '3/9/ +12', '3/30/12', 'VETO' }, { 'url' => 'uploads/2012/03/3.29.12-Transmi +ttal-Letter-3.pdf' , 'bill' => 'CS1', 'HB', '697', ' School Board of +Sumter County', 'McBurney', '3/9/12', '3/16/12', 'VETO' }, { 'url' => + 'tent/uploads/2012/04/HB-865-Veto-Letter.pdf' , 'bill' => '', 'HB', +'865', 'Pinellas Suncoast Transit Authority, Pinellas County', 'Hoope +r', '3/9/12', '3/30/12', 'VETO' }, { 'url' => 't/uploads/2012/04/4.13 +.12-HB-917-Veto-Letter.pdf' , 'bill' => '', 'HB', '917', 'Jurisdictio +n of the Courts', 'Bileca', '3/7/12', '3/30/12', 'VETO' }, { 'url' => + 's/2012/04/HB-1117-Veto-Letter.pdf' , 'bill' => 'CS1', 'HB', '1117', + 'Conservation of Wildlife', 'Harrison', '3/9/12', '3/30/12', 'VETO' +} );
    Writeup Formatting Tips can also help you in your next posts :)
    To answer your question, I'm assuming there are also other solutions, but doing a loop is a very easy and simple solution that you should take into account:
    for my $line_hash (@array1){ if ($line_hash->{'bill'} =~ /1117/){ print "Found a match"; } }
    Hope this helps
    Mister Guy

      mrguy123:

      Not to nitpick or anything, but the example you gave for code tags belies the suggestion you made. In fact, when you reformatted it, you removed the brackets, invalidating the changes. I would suggest something more like:

      @array1 = ( { 'url' => [ 'wp-content/uploads/2012/04/HB-177-Veto-Letter.pdf' ], 'bill' => [ 'CS3', 'HB', '177', 'Inmate Reentry', 'Porth', '3/9/12', '3/30/1 +2', 'VETO' ] }, { 'url' => [ 'uploads/2012/03/3.29.12-Transmittal-Letter-3.pdf' ], 'bill' => [ 'CS1', 'HB', '697', ' School Board of Sumter County', 'McBurney' +, '3/9/12', '3/16/12', 'VETO' ] }, { 'url' => [ 'tent/uploads/2012/04/HB-865-Veto-Letter.pdf' ], 'bill' => [ '', 'HB', '865', 'Pinellas Suncoast Transit Authority, Pinellas +County', 'Hooper', '3/9/12', '3/30/12', 'VETO' ] }, . . . );

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Thanks for the advice
        My example was mainly on how to use the code tags, but it makes sense to display the hash array in a more readable fashion

        a truth that's told with bad intent beats all the lies you can invent
Re: perl array of hashes help
by Anonymous Monk on Apr 30, 2012 at 20:15 UTC
    Life would be much simpler indeed if this were instead an XML file and you could do XPath expressions to query it. (Which you could do with Perl or otherwise.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://968045]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-09-21 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (175 votes), past polls