Code Comments
Programming Forum and web based access to our favorite programming groups.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";
}
Post Follow-up to this message"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
Post Follow-up to this messageTony 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
Post Follow-up to this messagePaul 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
Post Follow-up to this message"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
Post Follow-up to this messageTony 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
Post Follow-up to this message"Paul Lalli" <mritty@gmail.com> wrote in message news:<Xuj4d.3400$Ec4.1940@trndny04>...[col or=darkred] > "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. >[/color] 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
Post Follow-up to this messagetnitzke@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;
}
Post Follow-up to this message
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 ga
ll
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 int
o ]
/Alcestis/) [ flame, and falls out of sight. ] ben@morrow.me.
uk
Post Follow-up to this messagetiltonj@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";
}
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.