Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

array of hashes

by toniax (Scribe)
on Dec 05, 2010 at 17:45 UTC ( #875497=perlquestion: print w/ replies, xml ) Need Help??
toniax has asked for the wisdom of the Perl Monks concerning the following question:

I created this array of hashes
@array = ( { car => "ford", color => "red", pet => "dog", }, { car => "honda", color => "blue", pet => "cat", }, { car => "dodge", color => "orange", pet => "spider", }, ); { print "$array[0]{car}, $array[1]{car}\n" }
Can this line print "$array[0]{car}, $array[1]{car}\n" be condensed ?

Comment on array of hashes
Select or Download Code
Replies are listed 'Best First'.
Re: array of hashes
by GrandFather (Sage) on Dec 05, 2010 at 21:00 UTC

    Why? It can't be condensed in any useful fashion (especially without losing the comma), but it can be made more flexible. Consider:

    my @items = (0, 1); printf "%s\n", join ', ', map {$_->{car}} @array[@items];


    ford, honda
    True laziness is hard work
Re: array of hashes
by BrowserUk (Pope) on Dec 05, 2010 at 18:07 UTC

    A little:

    print map $array[$_]{car}, 0,1;; ford honda print map $_->{car}, @array[0,1];; ford honda

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      print join(', ', map $_->{car}, @array[0,1]), "\n";
Re: array of hashes
by punkish (Priest) on Dec 06, 2010 at 02:36 UTC
    @a = ( {a => "ford", b => "red", c => "dog"}, {a => "honda", b => "blue", c => "cat"}, {a => "dodge", b => "orange", c => "spider"} ); say "$a[0]{a}, $a[1]{a}"

    Other than that, shortening it doesn't achieve much. Unless, of course, there is more to this question than you are revealing.


    when small people start casting long shadows, it is time to go to bed
Re: array of hashes
by shawnhcorey (Friar) on Dec 06, 2010 at 01:08 UTC

    Why? Shawn's first Rule of Programming: KISS; Hard to understand is costly to maintain. Keep your code easy to understand; don't be clever.

Re: array of hashes
by biohisham (Priest) on Dec 05, 2010 at 20:23 UTC
    print "$array[$_]{car} " for qw(0 1);

    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
Re: array of hashes
by jandrew (Hermit) on Dec 10, 2010 at 02:42 UTC

    Both more and less.

    use 5.010; use strict; use warnings; my @array = ( { car => "ford", color => "red", pet => "dog", }, { car => "honda", color => "blue", pet => "cat", }, { car => "dodge", color => "orange", pet => "spider", }, ); say return_selected_elements( \@array, 'car', 0, 1 ); sub return_selected_elements { my $AoHref = shift; my $element = shift; my @Instances = @_; return join ", ", (map $AoHref->[$_]->{$element}, @Instances); }


    ford, honda

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2015-11-26 08:21 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (696 votes), past polls