Printing an array of hash refs

T

Tony N.

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";

}
 
P

Paul Lalli

Tony N. said:
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
 
M

Mark Clements

Tony said:
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
 
S

Shawn Corey

Paul said:
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
 
P

Paul Lalli

Shawn Corey said:
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
 
J

John W. Krahn

Tony said:
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
 
T

Tony N.

Paul Lalli said:
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
 
J

Jay Tilton

(e-mail address removed) (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;
}
 
B

Ben Morrow

Quoth Shawn Corey said:
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
 
T

Tony N.

(e-mail address removed) (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";

}
 
J

Joe Smith

Tony said:
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
 
T

Tony N.

Joe Smith said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top