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

okay, I have the following code:

my %data; $data{'ID'} = 'blahvals'; $data{'OTHER'} = 'etcetc' push @array, \%data;

now, I need to extract from the @array all the %data{'id'}'s for a grep similar to:

grep /^$id$/, hashID

Now, I know its possible to do, but what is the most elegant solution to this?

Much appreciated,

Purge

Edit: chipmunk 2001-05-03

Replies are listed 'Best First'.
Re: referencing hell
by davorg (Chancellor) on May 03, 2001 at 18:35 UTC

    Your question isn't very clear, but I think you might mean something like this:

    my @wanted = grep { $_->{ID} == $id } @array;
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Thanks for all the swift replies, I think I understand how you have done it, it certainly works, though a quick explanation wouldn't go amiss.
      I will make myself more clear next time, but I wasn't quite sure what I wanted either at the time :)

      Thanks again
      Purge.
        a quick explanation wouldn't go amiss...

        Here's an explanation of what davorg suggested...

        First, let's look at your data. The line 'push @array, \%data;' in your code builds an array of references to hashes. You might picture the contents of @array as looking a little like this: (hashref1, hashref2, hashref3, hashref4, etc.) So... davorg's line of code (reading it right to left) says:

          for each value in @array...
            (and we know those values are hash references)
          dereference each hashref...
            (using the $hashref->{key} syntax)
          to give us the value associated with the ID key.
          If that value taken as a number...
            (otherwise you would want to use 'eq' instead of '==')
          equals the value of $id...
          'pass it through' to be included in @wanted.

        HTH

Re: referencing hell
by Masem (Monsignor) on May 03, 2001 at 18:36 UTC
    How about a map?
    my @id_array = map { $_->{'ID'} } @array;
    If you need to do a selective comparison, a grep after this operation works well:
    my @sel_id_array = grep { $_ < 30 } map { $_->{'ID'} } @array;
    (Also, a PM hint: use <CODE> tags instead of PRE so that you can copy and paste code directly without any markup.)


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: referencing hell
by chipmunk (Parson) on May 03, 2001 at 18:37 UTC
    I'm not completely sure what you're looking for... Either a list of all the ids, or a list of references to the specific data hashes where ID has a certain value...
    my @all_ids = map $_->{'ID'}, @array; my @specific_data = grep $_->{'ID'} == $id, @array;
Re: referencing hell
by t'mo (Pilgrim) on May 04, 2001 at 00:47 UTC

    My first thought, on seeing the title of this one in the list of new nodes, was something like this:

    $place = Hell->new; # $place is a reference to this object, righ +t? $another_place = \$place; # now there's two references to hell

    (...though if that were what the author meant, s/he would have probably put it in the "Poetry" section. I just thought that was funny...)

    Update: changed object creation from a Smalltalk-ish syntax to Perl. Oops.