Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Dereference array-elements

by very empty (Scribe)
on Jun 03, 2002 at 11:54 UTC ( [id://171204]=perlquestion: print w/replies, xml ) Need Help??

very empty has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have an array of hash-references and want to retrieve a single hash from it.
There is a primary key in each hash. This is done by the following code:

$hashref=grep {has_the_right_primary_key($_)} @hashes; %hash=$$hashref;

I wonder if I can get the hash instead of the hashref with a single line.

Replies are listed 'Best First'.
Re: Dereference array-elements
by crazyinsomniac (Prior) on Jun 03, 2002 at 12:09 UTC
Re: Dereference array-elements
by Juerd (Abbot) on Jun 03, 2002 at 12:01 UTC

    I wonder if I can get the hash instead of the hashref with a single line.

    This is Perl. You can :)

    First, let me state that %hash = $$hashref isn't going to do what you want. You probably meant %hash = %$hashref there. This can also be written as %hash = %{ $hashref } which brings me to:

    %hash = %{ grep { has_the_right_primary_key($_) } @hashes };
    But that too does not work. This is because grep doesn't return what you want in scalar context. Your own example has the same problem. Maybe something like this helps (I also changed the code block to a normal expression):
    %hash = %{ ( grep hash_the_right_primary_key($_), @hashes )[0] };
    BUT... I think you should have been using %hashes, not @hashes. Do not grep if you can look it up in a hash. This is something you will have to figure out yourself.

    Good luck.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      Thanks a lot, this is very helpful and you are right,  %hash = %$hashref was what I have meant.
      It is also right, that looking up in a hash is always better and more perlish, but in my case the "primary key" is only primary at that moment.

      Regards...
Re: Dereference array-elements
by Zaxo (Archbishop) on Jun 03, 2002 at 12:15 UTC

    This assumes that there is at least one:

    my %hash = %{(grep {exists $_->{'foo'}} @hashes)[0]};

    Your code is incorrect in both lines. In the first, $hashref will be set to the number of matches, and you need %hash=%$hashref in the second. Recommend tye's References quick reference to help with errors like that

    Perl hashes don't really have 'primary' keys in the way that databases do.

    After Compline,
    Zaxo

      my %hash = %{(grep {exists $_ -> {foo}} @hashes) [0] || {}};

      And then it even works if there's no match.

      Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-20 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found