in reply to Array of Hashes

It's easy to get the element (or elements, since there can be more than one) directly by using grep.

my @elements = grep { exists $_->{$captureId} } @outputFiles;

If you really want the index of the element, rather than the element itself, you can change the grep to:

my @indexes = grep { exists $outputFiles[$_]{$captureId} } 0..$#outputFiles;

Replies are listed 'Best First'.
Re^2: Array of Hashes
by ramya2005 (Scribe) on Sep 20, 2005 at 17:42 UTC
    Thanks friedo. But I am getting the following error when trying to run your code.
    "Modification of a read-only value attempted at" the following line.

    my @elements = grep { exists $_->{$captureId} } @outputFiles;
      I don't get that error. Are you certain that what you have is an array of hashes? Here is a one-liner which shows that it works:

      perl -Mstrict -Mwarnings -MData::Dumper -e 'my @array = ( { foo => 1 }, { }, { }, { foo => 2 } ); my @elements = grep { exists $_->{foo} } @array; print Dumper \@elements' $VAR1 = [ { 'foo' => 1 }, { 'foo' => 2 } ];