grinder has asked for the wisdom of the Perl Monks concerning the following question:
Sometimes I think I have perlman:perldsc down pat, and then a situation comes up that shows me that I still have much to learn.
It's only a minor thing, I wanted to print out a complex data structure (well, a HoAoA) in one statement. It's easy enough to print out an HoA, but I'm getting my braces, ats and arrows mixed up when it comes to printing out at HoAoA.
In the example below, the first print, to print out an HoA seems clear to me (for some definition of clear). In the second example, I don't seem to able to do away with the map operator. I thought I should be about to do something like join( ', ' => @{@{$hoaoa{$_}}->[0] ), but that doesn't work correctly. Is there Another Way To Do It? This is not a call to arms^Wgolf, I want the code to remain more or less readable!
--#! /usr/bin/perl -w use strict; my %hoa = ( 'foo' => [ 1, 3, 5 ], 'bar' => [ 4, 5, 6 ], 'rat' => [ 8, 4, 2 ], ); print( "$_ ", join( ', ' => @{$hoa{$_}} ), "\n" ) for sort keys %hoa; print "\n"; =over bar 4, 5, 6 foo 1, 3, 5 rat 8, 4, 2 =cut my %hoaoa = ( 'Foo' => [ [1,2], [3,4], [5,6] ], 'Bar' => [ [3,5], [5,7], [7,9] ], 'Rat' => [ [0,1], [2,1], [4,1] ], ); print( "$_ ", join( ', ' => map { $_->[0] } @{$hoaoa{$_}} ), "\n" ) for sort keys %hoaoa; print "\n"; =over Bar 3 5 7 Foo 1 3 5 Rat 0 2 4 =cut __END__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Printing a hash of arrays of arrays in one statement?
by dragonchild (Archbishop) on Oct 09, 2001 at 19:23 UTC | |
|
Re: Printing a hash of arrays of arrays in one statement?
by arturo (Vicar) on Oct 09, 2001 at 19:21 UTC | |
|
Re: Printing a hash of arrays of arrays in one statement?
by thinker (Parson) on Oct 10, 2001 at 00:01 UTC |