Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Iterating Blessed Array of Array

by neversaint (Deacon)
on Dec 06, 2008 at 15:28 UTC ( #728561=perlquestion: print w/replies, xml ) Need Help??

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

Dear Masters,
I have the following function:
my @output = myfunc("data_value1"); print Dumper \@output;
that yields:
$VAR1 = [ bless( { 'ID' => 'WBGene00000099', 'DBConnection' => bless( { 'database' => 'treefam_6', 'database_handle' => bless( {}, 'DBI::db' ) }, 'Treefam::DBConnection' ), 'species' => 'Caenorhabditis elegans', 'bootstrap' => '19', 'species_name_type' => 'LATIN' }, 'Treefam::Gene' ), bless( { 'ID' => 'WBGene00000098', 'DBConnection' => $VAR1->[0]{'DBConnection'}, 'species' => 'Caenorhabditis elegans', 'bootstrap' => '1', 'species_name_type' => 'LATIN' }, 'Treefam::Gene' ), bless( { 'ID' => 'YPL209C', 'DBConnection' => $VAR1->[0]{'DBConnection'}, 'species' => 'Saccharomyces cerevisiae', 'bootstrap' => '47', 'species_name_type' => 'LATIN' }, 'Treefam::Gene' ) ];
My question is how can iterate/print such data structure so that I can get the output like this:
WBGene00000099 - Caenorhabditis elegans WBGene00000098 - Caenorhabditis elegans YPL209C - Saccharomyces cerevisiae
namely from "ID" and "species" keys.

neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Iterating Blessed Array of Array
by moritz (Cardinal) on Dec 06, 2008 at 15:37 UTC
    Contrary to what your title says, neither is the array blessed, nor does it contain arrays.

    Actually it's a simple, unblessed array that contains blessed hash references. You can iterate over that array just like you'd do with any other array:

    for my $item (@output) { print $item->{ID}, ' ', $item->{species}, "\n"; }

    But usually it's bad practice to access the hash items of an object - most of the time the object provides accessor methods that you should use instead.

      And there are accessors, which it only took a very quick googling to find. So you could use the accessor $item->ID instead of directly accessing the hash with $item->{ID}.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2020-02-29 04:39 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (128 votes). Check out past polls.