Re: Printing an array of hash refs

From: Shawn Corey (shawn.corey_at_sympatico.ca)
Date: 09/22/04


Date: Wed, 22 Sep 2004 14:08:13 -0400

Paul Lalli wrote:
> "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
>
>
Isn't it?

   print join( "\t", map { $row->{$_} } sort keys %$row ), "\n";

        --- Shawn



Relevant Pages

  • Re: Another Dereference Post by me
    ... Paul Lalli wrote: ... you're creating an anonymous array reference. ... You said you want to sort by the fifth column. ...
    (comp.lang.perl.misc)
  • Re: code explanation please...
    ... This makes perfect sense. ... I appreciate the explanation. ... Paul Lalli wrote: ... This is using what is known as an array slice. ...
    (perl.beginners)
  • Re: Best way to read a large file in line by line?
    ... Paul Lalli wrote: ... Why do you think you need the file into an array at all? ... lines in memory at the same time. ... If the current line's identifier has already been seen, ...
    (perl.beginners)
  • Re: Printing an array of hash refs
    ... "Tony N." ... > I'm using the following code to print an array of hash refs. ... Paul Lalli ...
    (comp.lang.perl.misc)
  • Re: Can this be more efficient?
    ... >> Also, is there any reason you can't just use grep, rather than ... > grep I guess but apart from speed the program works pretty well so I'm ... line, storing each line onto an array, and then going through the array ... Paul Lalli ...
    (comp.lang.perl.misc)