in reply to Re: printing columns of data
in thread printing columns of data

If each row of output is built from data in multiple keys of the hash (per your test code), then I don't think you have any choice but to roll your own code that traverses everything and builds up the output.

If you've got arbitrarily many keys in %hash, then you'll likely want something like this:

use strict; use warnings; my %hash; $hash{col1} = [qw(a b c d e)]; $hash{col2} = [qw(1 2 3 4 5)]; $hash{col3} = [qw(q r s t u)]; # Change this so that the keys of %hash # show up in the order you want them, # in the output. my @keys = sort keys %hash; # Assumes every key of %hash is an # arrayref with the same number of # elements in it. Also note that # this code destroys %hash, as # it goes along. If you don't want # that, make a copy first. print join(",", @keys), "\n"; my $ref_key = $keys[0]; while (@{ $hash{$ref_key} }) { my @line; for my $key (@keys) { push @line, shift(@{ $hash{$key} }); } print join(",", @line), "\n"; }

Replies are listed 'Best First'.
Re^3: printing columns of data
by klassa (Acolyte) on Sep 02, 2008 at 13:53 UTC
    Here's a non-destructive version:
    use strict; use warnings; my %hash; $hash{col1} = [qw(a b c d e)]; $hash{col2} = [qw(1 2 3 4 5)]; $hash{col3} = [qw(q r s t u)]; # Change this so that the keys of %hash # show up in the order you want them, # in the output. my @keys = sort keys %hash; # Assumes every key of %hash is an # arrayref with the same number of # elements in it. print join(",", @keys), "\n"; my $ref_key = $keys[0]; my $count = @{ $hash{$ref_key} }; my $pos = 0; while ($pos < $count) { my @line; for my $key (@keys) { push @line, $hash{$key}[$pos]; } print join(",", @line), "\n"; ++$pos; }