in reply to Re^2: How to concatenate a generic array of arrays and an array of hashes
in thread How to concatenate a generic array of arrays and an array of hashes
OK, here's some code that does what you want. I've not included the part that reads the data.
#!/usr/bin/perl use warnings; use strict; use List::Util qw/max/; my $VAR1 = { 'sample.txt' => [ 'Line_1_3long', 'Line_2_3' ], 'sample_2.txt' => [ 'Line_3_3longer' ], 'sample_3.txt' => [ 'Line_4_3', 'Line_5_3', 'Line_6_3evenlonger', 'Line_7_3', ] }; my %maxlength = (); foreach my $key (sort keys %$VAR1) { $maxlength{$key} = max map { length } @{$VAR1->{$key}}; } my $maxelements = max map { $#$_ } values %$VAR1; foreach my $line (0..$maxelements) { foreach my $key (sort keys %$VAR1) { if($line >= @{$VAR1->{$key}}) { print " " x ($maxlength{$key} + 1); } else { print $VAR1->{$key}->[$line]; print " " x ($maxlength{$key} - length($VAR1->{$key}->[$line]) + + 1); } } print "\n"; }
This prints:
$ perl test.pl Line_1_3long Line_3_3longer Line_4_3 Line_2_3 Line_5_3 Line_6_3evenlonger Line_7_3 $
Notes:
I hope this helps. My bill for an hour (rounded up) of paid consultant work's in the mail. :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: How to concatenate a generic array of arrays and an array of hashes
by Cristoforo (Curate) on Jul 07, 2014 at 19:00 UTC | |
by AppleFritter (Vicar) on Jul 07, 2014 at 19:35 UTC | |
by thanos1983 (Parson) on Jul 08, 2014 at 00:34 UTC | |
by Cristoforo (Curate) on Jul 08, 2014 at 01:00 UTC | |
|
Re^4: How to concatenate a generic array of arrays and an array of hashes
by thanos1983 (Parson) on Jul 07, 2014 at 14:11 UTC | |
by AppleFritter (Vicar) on Jul 07, 2014 at 19:35 UTC |