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

Hi Monks,

I have array hash values for the following;

@values=$db->fetch_all_arrayhash($sql);

We can use to print the @values by using Data::Dumper Module.

But, I want to print the value @values without using any modules

Thanks in Advance
Rose

Replies are listed 'Best First'.
Re: print the array hash without using any module
by jettero (Monsignor) on May 22, 2007 at 12:22 UTC

    without using any modules

    Without anything but DBI hopefully? Avoiding using that would be a mess.

    It's probably not very efficient to do it the way you have in mind, but it might look like this:

    for my $r (@values) { for my $k (keys %$r) { print "$k: $r->{$k}\n"; } }

    A better way would be one row at a time:

    my $sth = $db->prepare($sql) or die $db->errstr; $sth->execute or die $db->errstr; while( my $h = $sth->fetchrow_hashref ) { for my $k (keys %$h) { # also see each() print "$k: $h->{$k}\n"; } }

    -Paul

Re: print the array hash without using any module
by BrowserUk (Patriarch) on May 22, 2007 at 12:35 UTC

    ## Gen some random test data my @a = map{ { map{ int rand 100 } 1 .. 10 } } 1 .. 10; ## Print the hashes one per line sorted by key print "$_\n" for map{ my $href = $_; join ', ', map{ "$_ => $href->{ $_ }" } sort{ $a <=> $b } keys %{ $href } } @a; __OUTPUT__ 2 => 28, 36 => 20, 49 => 97, 66 => 26, 72 => 28 25 => 99, 32 => 68, 46 => 70, 98 => 75, 99 => 49 37 => 64, 39 => 55, 43 => 29, 76 => 31, 90 => 48 21 => 33, 22 => 34, 76 => 36, 93 => 78, 96 => 14 31 => 69, 40 => 3, 54 => 20, 64 => 86, 86 => 7 17 => 36, 43 => 21, 53 => 8, 68 => 2, 95 => 69 3 => 12, 73 => 98, 89 => 49, 96 => 41, 98 => 46 9 => 90, 67 => 47, 73 => 22, 83 => 10, 85 => 67 20 => 12, 31 => 23, 36 => 75, 59 => 62, 92 => 28 45 => 21, 64 => 92, 90 => 45, 92 => 60, 93 => 69

    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.
Re: print the array hash without using any module
by GrandFather (Saint) on May 22, 2007 at 12:23 UTC

    To what end? For debugging, or for presenting a report to the boss? Acceptable output is very different for the two situations.

    Perhaps you could provide two or three items of data and indicate how you would like them presented. Even better would be if you were to write a little code as a first attempt at printing the data as you would like to see it, then ask how to solve any specific problems you encounter along the way.


    DWIM is Perl's answer to Gödel
Re: print the array hash without using any module
by blazar (Canon) on May 22, 2007 at 12:36 UTC
    But, I want to print the value @values without using any modules

    What for? Homework? How 'bout something like

    for my $h (@results) { print "{", (join ", ", map "'$_' => '$h->{$_}'", keys %$h), "}\n"; }

    anyway?