Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^2: perl array of hashes help

by Anonymous Monk
on May 02, 2012 at 05:24 UTC ( #968355=note: print w/replies, xml ) Need Help??

in reply to Re: perl array of hashes help
in thread perl array of hashes help

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

Replies are listed 'Best First'.
Re^3: perl array of hashes help
by trizen (Hermit) on May 02, 2012 at 12:20 UTC
    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] ;


       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


        Hello Ragilla,

        The code $all->{text}[0] is equivalent with $all->{text}->[0]. This implies an implicit dereference. Similar with $array_ref->[0].

        In the second code, ${$all->{text}}[0] is similar with $array[0], because $all->{text} is an ARRAY REF, and @{ARRAY_REF} is similar with @array, which can lead to ${ARRAY_REF}[index].

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968355]
Corion orders some gifts online :)
[ambrus]: We're spending Christmas and the week after at my brother's place. First time ever, because the last four years they could came home instead.
[ambrus]: robby_dobby: wait what? Glasgow? isn't that one of the most boring towns ever?
[marto]: ambrus: no :)
[robby_dobby]: ambrus: Dunno. marto is from Glasgow. I'm in London and I say London is quite colourful, but boring :-)
[CPUlidster]: Is the glass pyramid still in glasgow

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2017-12-15 10:49 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (431 votes). Check out past polls.