Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

problem with array of hashes

by Priti24 (Novice)
on Oct 09, 2012 at 10:18 UTC ( #997967=perlquestion: print w/ replies, xml ) Need Help??
Priti24 has asked for the wisdom of the Perl Monks concerning the following question:

i have an array of hashes like this

$VAR1 = [ { '201' => 'i', '101' => 'f', '41' => 't' } ]

i have to fetch 41 from here. how can i do that??

Comment on problem with array of hashes
Download Code
Re: problem with array of hashes
by McA (Deacon) on Oct 09, 2012 at 10:28 UTC
    $VAR1->[0]->{'41'};
      Same as: $VAR1->[0]{41};

      Quality is not an act, it is a habit. --Aristotle

        There may be good reason to qualify the seemingly redundant use of the arrow dereferencer between the deepest subscripts. If not solely to understand what is happening in a different way.

        ...is the same as

        ${${$VAR1}[0]}{41}

        The manner in which dereferencing occurs is different but the result is the same. Most feel the arrow manner is simpler on the brain, being more intuitive once you have grasped the notion of dereferencing.

        lets reduce the complexity for a moment

        my $VAR2 = [ 101, 201, 42 ]; print $VAR2->[0],' ',${$VAR2}[0]; ------- 101 101

        we can now reach the first level reference to the array and by all accounts either syntax is fairly comprehensible.

        Lets increase the complexity back up.

        my $VAR3 = [ {'101'=>'hel', '201'=>'lo', '42'=>'world'} ]; print $VAR3->[0]->{42},' ',${${$VAR3}[0]}{42}; ------- world world

        You can already see the way of the arrow is clearer as the start of the dereference does not accumulate the scalar sigil for each depth of level of complexity. And to be fair this starts literally looking subsequently more expensive too.

        The toppler of the cake is that the arrow way also adds the optimisation of saying, hey you know what, seeing as we're already dereferencing here lets not bother making a christmas decoration out of the expression and voila! you get...

        print $VAR3->[0]{42}; --- world

        Making the array actual, you use dereffing where its needed, not between the first level subscript but between the second level subscript.

        my @VAR4 = ( {'101'=>'hel', '201'=>'lo', '42'=>'world'},' dereffed' ); print @VAR4,' ',$VAR4[0]->{42},' ',${$VAR4[0]}{42}; ------- HASH(0x3e81a4) dereffed world world

        Comparing the position of the arrow between $VAR3 and $VAR4[0], it would seem this gives us a qualifier for telling us when in a complex structure a 'reference to' or 'an actual' hash/array resides at the first level.

        The reason being data structures only hold scalars. The very reason for the requirement of referencing. As a reference to a complex structure is a scalar.

        And this of course means the arrow way can safely bah humbug the merry season with all the capacity of your ram.


        my $ah={h=>'a'};print %$ah for 0..2;
Re: problem with array of hashes
by daxim (Chaplain) on Oct 09, 2012 at 10:34 UTC
    The expression +(sort { $a <=> $b } keys %{ $VAR1->[0] })[0] returns 41.

      So does:

      my ($value) = map { join('array of hashes', 41) } $VAR;

      Update: added parentheses. Thanks McA.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Oh, oh, oh

        Guys, look at this:

        use strict; use warnings; use Test::More tests => 2; my $the_ultimate_answer = 42; my $VAR1 = [ { '201' => 'i', '101' => 'f', '41' => 't', } ]; is(+(sort { $a <=> $b } keys %{ $VAR1->[0] })[0], $the_ultimate_answer +, 'Testing for ultimate answer'); my $value = map { join('array of hashes', 41) } $VAR1; is($value, $the_ultimate_answer, 'Testing for ultimate answer');

        The second proposal is more wrong than I expected. ;-)

      ...and print +(sort {$VAR1{$a} <=> $VAR1{$b}} keys %{$VAR1->[0]})[-1]
Re: problem with array of hashes
by moritz (Cardinal) on Oct 09, 2012 at 11:09 UTC

    Hash keys aren't ordered, so there is no getting of the "last" hash key or so.

    So the question is, how do you identify which hash key you want to retrieve? If it's the key with 't' as a value, you have to iterate through the whole hash -- which likely means that the data structure is poorly chosen for you application. Also if you want to search by value, what do you do if multiple keys have the same value?

    Or is there any other logic by which you identify the '41'?

Re: problem with array of hashes
by wirito (Acolyte) on Oct 09, 2012 at 16:13 UTC
    With no restrictions, we can say that:
    #! /usr/bin/perl my $VAR1 = [ { '201' => 'i', '101' => 'f', '41' => 't', } ]; print +(%{$VAR1->[0]})[4];

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2014-07-25 00:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls