Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: perl array of hashes help

by trizen (Hermit)
on Apr 30, 2012 at 13:04 UTC ( #968054=note: print w/ replies, xml ) Need Help??


in reply to perl array of hashes help

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;


Comment on Re: perl array of hashes help
Select or Download Code
Replies are listed 'Best First'.
Re^2: perl array of hashes help
by Anonymous Monk on May 02, 2012 at 05:24 UTC
    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;
        Hi Trizen,

        I have one question regarding access of the perl hash key which is an array reference like below i have an hash

         my $all = {'text' => [                                      'HJ0039_x.pdf' , 'HJ0039' , 'HJ0039'                                      ]};

        To access the above elements i can use two ways like below

         print " $all->{text}[0] ;

        and

         print "${$all->{text}}[0]

        can you please explain me clearly what is the difference between the above two methods , without de-referencing the array how we are able to access the array elements

        Thanks

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2015-07-31 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls