| Author |
Printing an array of hash refs
|
|
| Tony N. 2004-09-22, 4:00 pm |
| I'm using the following code to print an array of hash refs. It seems
to me there should be a more concise way to do this. I don't like the
trailing \t or the extra line of code to get the \n. Any pointers
would be appreciated.
print "$_\t" for sort keys %{$rows[0]};
print "\n";
for my $row (@rows) {
print "$row->{$_}\t" for sort keys %{$row};
print "\n";
}
| |
| Paul Lalli 2004-09-22, 4:00 pm |
| "Tony N." <tnitzke@simpson.com> wrote in message
news:638483db.0409220735.49486c78@posting.google.com...
> I'm using the following code to print an array of hash refs. It seems
> to me there should be a more concise way to do this. I don't like the
> trailing \t or the extra line of code to get the \n. Any pointers
> would be appreciated.
> for my $row (@rows) {
> print "$row->{$_}\t" for sort keys %{$row};
> print "\n";
> }
for my $row (@rows){
print (join ("\t", sort keys %$row), "\n");
}
Paul Lalli
| |
| Mark Clements 2004-09-22, 4:00 pm |
| Tony N. wrote:
> I'm using the following code to print an array of hash refs. It seems
> to me there should be a more concise way to do this. I don't like the
> trailing \t or the extra line of code to get the \n. Any pointers
> would be appreciated.
>
> print "$_\t" for sort keys %{$rows[0]};
> print "\n";
>
> for my $row (@rows) {
> print "$row->{$_}\t" for sort keys %{$row};
> print "\n";
>
> }
check out Data::Dumper.
Mark
| |
| Shawn Corey 2004-09-22, 4:00 pm |
| Paul Lalli wrote:
> "Tony N." <tnitzke@simpson.com> wrote in message
> news:638483db.0409220735.49486c78@posting.google.com...
>
>
>
> for my $row (@rows){
> print (join ("\t", sort keys %$row), "\n");
> }
>
> Paul Lalli
>
>
Isn't it?
print join( "\t", map { $row->{$_} } sort keys %$row ), "\n";
--- Shawn
| |
| Paul Lalli 2004-09-22, 4:00 pm |
| "Shawn Corey" <shawn.corey@sympatico.ca> wrote in message
news:Fbj4d.17679$pA.1240792@news20.bellglobal.com...
> Paul Lalli wrote:
>
> Isn't it?
>
> print join( "\t", map { $row->{$_} } sort keys %$row ), "\n";
Ah, quite correct. I neglected to realize the OP wanted the hash values
rather than the hash keys. Thanks for the correction.
Paul Lalli
| |
| John W. Krahn 2004-09-22, 8:59 pm |
| Tony N. wrote:
> I'm using the following code to print an array of hash refs. It seems
> to me there should be a more concise way to do this. I don't like the
> trailing \t or the extra line of code to get the \n. Any pointers
> would be appreciated.
>
> print "$_\t" for sort keys %{$rows[0]};
> print "\n";
>
> for my $row (@rows) {
> print "$row->{$_}\t" for sort keys %{$row};
> print "\n";
>
> }
print map { my $row = $_; join( "\t", map $row->{$_}, sort keys %$row ) . "\n"
} @rows;
John
--
use Perl;
program
fulfillment
| |
| Tony N. 2004-09-22, 8:59 pm |
| "Paul Lalli" <mritty@gmail.com> wrote in message news:<Xuj4d.3400$Ec4.1940@trndny04>...
> "Shawn Corey" <shawn.corey@sympatico.ca> wrote in message
> news:Fbj4d.17679$pA.1240792@news20.bellglobal.com...
>
> Ah, quite correct. I neglected to realize the OP wanted the hash values
> rather than the hash keys. Thanks for the correction.
>
Thanks Paul and Shawn. That's exactly what I was looking for. The
first print join I'll use once for the headings (hash keys) and the
second for all of the values.
Regards,
Tony
| |
| Jay Tilton 2004-09-22, 8:59 pm |
| tnitzke@simpson.com (Tony N.) wrote:
: I'm using the following code to print an array of hash refs. It seems
: to me there should be a more concise way to do this. I don't like the
: trailing \t or the extra line of code to get the \n. Any pointers
: would be appreciated.
:
: print "$_\t" for sort keys %{$rows[0]};
: print "\n";
:
: for my $row (@rows) {
: print "$row->{$_}\t" for sort keys %{$row};
: print "\n";
: }
{
local($, , $\) = ("\t", "\n");
print sort keys %{$rows[0]};
print @$_{sort keys %$_} for @rows;
}
| |
| Ben Morrow 2004-09-23, 3:58 pm |
|
Quoth Shawn Corey <shawn.corey@sympatico.ca>:
> Paul Lalli wrote:
> Isn't it?
>
> print join( "\t", map { $row->{$_} } sort keys %$row ), "\n";
I would prefer
{
local ($,, $\) = ("\t", "\n");
print map $row->{$_}, sort keys %$row;
}
or maybe with a hash slice
{
local ($,, $\) = ("\t", "\n");
print @{$row}{sort keys %$row};
}
Ben
--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
/Alcestis/) [ flame, and falls out of sight. ] ben@morrow.me.uk
| |
| Tony N. 2004-09-23, 8:58 pm |
| tiltonj@erols.com (Jay Tilton) wrote in message news:<4152125e.292436952@news.erols.com>...
> tnitzke@simpson.com (Tony N.) wrote:
>
> {
> local($, , $\) = ("\t", "\n");
> print sort keys %{$rows[0]};
> print @$_{sort keys %$_} for @rows;
> }
Thanks Jay. I liked this technique, so I created a second version of
the program using this (I had already implemented the technique from
Paul and Shawn).
I had to make a modification and was hoping you guys would look at.
The hash ref array is populated using DBI and my SQL statement was
missing an outer join. After I fixed that now I have the possibility
of null values coming the DB server.
Thanks to everyone who responded. I learned a lot from your examples.
Modified version of Jay's technique:
{
local ($, , $\) = ("\t", "\n");
print sort keys %{$rows[0]};
for my $row (@rows) {
print map {$row->{$_}?$row->{$_}:'' } sort keys %{$row};
}
}
Modified version of Paul's and Shawn's technique:
print join("\t", sort keys %{$rows[0]}), "\n";
for my $row (@rows) {
print join("\t", map { $row->{$_} ? $row->{$_}:'' } sort keys %$row),
"\n";
}
| |
| Joe Smith 2004-09-24, 8:58 am |
| Tony N. wrote:
> The hash ref array is populated using DBI and my SQL statement was
> missing an outer join. After I fixed that now I have the possibility
> of null values coming the DB server.
>
> print map {$row->{$_}?$row->{$_}:'' } sort keys %{$row};
That will print '0' as '', which is bad if 0 is a legitmate value.
print map {(defined $row->{$_}) ? $row->{$_} : '' } sort keys %{$row};
-Joe
| |
| Tony N. 2004-09-24, 4:01 pm |
| Joe Smith <Joe.Smith@inwap.com> wrote in message news:<gAQ4d.99101$MQ5.94815@attbi_s52>...
> Tony N. wrote:
>
>
> That will print '0' as '', which is bad if 0 is a legitmate value.
>
> print map {(defined $row->{$_}) ? $row->{$_} : '' } sort keys %{$row};
>
> -Joe
Good point.
Thanks,
Tony
|
|
|
|