in reply to Array of Hashes

You'll have to loop over the array, like so:
my $wanted; for my $href (@outputFiles) { $wanted = $href, last if exists $href->{$captureId}; }
My code gives you $wanted which IS the hash reference, rather than the index in the array. You'd print $wanted->{$captureId} and $wanted->{type}.

If you don't have duplicate hash keys, though, why are you using an array of hash refs?


Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Array of Hashes
by ramya2005 (Scribe) on Sep 20, 2005 at 17:39 UTC
    Thanks that works!

    <If you don't have duplicate hash keys, though, why are you using an array of hash refs?>
    I will not have duplicate captureID's. But I want to record the data in the following table using some Perl data structure.

    captureID | outputFileLocation| Filetype
    ------------|--------------------|---------
    captureID1 | c:/temp/test.xml | 4
    captureID2 | c:/test/temp.xml | 6 ......

    In this captureID is unique. Is there any other way of handling this data other than array of hashes?
      Is there any other way of handling this data...

      Since all the "captureID" values are unique, and these are the values that you want to use for fetching information about a given entry, I guess the question is, "why use an array at all?" I think a HoH (or even a simple hash) would work just as well:

      my %outputFiles; sub setOutputFile { my ($id,$path,$type) = @_; # HoH method: $outputFiles{$id}{path} = $path; $outputFiles{$id}{type} = $type; # or, simple hash method: # $outputFiles{$id} = join "=:=", $type, $path; } sub getOutputFiles { my $id = shift; # HoH method: my $type = $outputFiles{$id}{type}; my $path = $outputFiles{$id}{path}; # or, simple hash method: # my ( $type, $path ) = split /=:=/, $outputFiles{$id}; # ... do something with $path and $type ... }
Re^2: Array of Hashes
by Angel (Friar) on Sep 20, 2005 at 19:42 UTC
    But though it is less efficient?

    Could you not keep a second index. Or multiple indicies? Or is there a better way. I am asking since I am holding data from a database in a loh for html::template's happiness and the second index prevents a linear scan each time.

    %primary_key{ $capture_id } = $array_index;