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

Fellow Monasterians:

We all know how to print the elements of an array:

my @Array = qw (aaaaa bbbbb ccccc); print @Array;

but is there an equivalent to print all the values of an AoH (or in this case, a reference to) withOUT iterating over it?

my $AoH = [{ keyname => "aaaaa" }, { keyname => "bbbbb" }, { keyname => "ccccc" }]; print $AoH; ???

Thanks!


—Brad
"Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton

Replies are listed 'Best First'.
Re: Printing all values of an AoH
by ambrus (Abbot) on Oct 16, 2004 at 19:38 UTC
    ($,, $\) = ($", $/); print map %$_, @$AoH;
    would print
    keyname aaaaa keyname bbbbb keyname ccccc
    in your case.
Re: Printing all values of an AoH
by CountZero (Bishop) on Oct 16, 2004 at 20:10 UTC
    Data::Dumper?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Of course, Data::Dumper is great for development, but I was looking for a something to run within the actual script. As in:

      $stmt = qq/SELECT * FROM table WHERE id IN (/ . join(',', ('?') x $categories ) . qq/)/; $sth = $dbh->prepare($stmt); $sth->execute($categories);

      where $categories is a ref to an AoH, and there is only one key name.


      —Brad
      "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton
        Perhaps have a look into the Data::Dumper source code and see how it gets at the AoH data.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Printing all values of an AoH
by TedPride (Priest) on Oct 16, 2004 at 20:02 UTC
    use strict; use warnings; my $AoH = [{ keyname => "aaaaa" }, { keyname => "bbbbb" }, { keyname => "ccccc" }]; print map ((values %$_, " "), @$AoH), "\n";
    This doesn't leave the separator set differently from default, and doesn't print keys too.