Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Printing an array of hash refs
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";

}

Report this thread to moderator Post Follow-up to this message
Old Post
Tony N.
09-22-04 09:00 PM


Re: Printing an array of hash refs
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Paul Lalli
09-22-04 09:00 PM


Re: Printing an array of hash refs
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

Report this thread to moderator Post Follow-up to this message
Old Post
Mark Clements
09-22-04 09:00 PM


Re: Printing an array of hash refs
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


Report this thread to moderator Post Follow-up to this message
Old Post
Shawn Corey
09-22-04 09:00 PM


Re: Printing an array of hash refs
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Paul Lalli
09-22-04 09:00 PM


Re: Printing an array of hash refs
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

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
09-23-04 01:59 AM


Re: Printing an array of hash refs
"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

Report this thread to moderator Post Follow-up to this message
Old Post
Tony N.
09-23-04 01:59 AM


Re: Printing an array of hash refs
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;
}


Report this thread to moderator Post Follow-up to this message
Old Post
Jay Tilton
09-23-04 01:59 AM


Re: Printing an array of hash refs
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

Report this thread to moderator Post Follow-up to this message
Old Post
Ben Morrow
09-23-04 08:58 PM


Re: Printing an array of hash refs
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";

}

Report this thread to moderator Post Follow-up to this message
Old Post
Tony N.
09-24-04 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:19 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.