There are obviously a number of ways to improve the code
You probably don't want to be using variable variable names (which makes your root problem go away).. A bunch of nodes, including the following two, refer to the
reasons:
Have to also throw out the obligatory comment about avoiding the use of
$a and
$b because of their special global-ness w/
sort
Could you also provide sample input & output to this sub? I strongly suspect this could be written w/
map &
join .. something like (note the hash slice usage):
sub meld_columns {
my ($arrays, @arrNames) = @_;
my ($maxIdx) = sort { $b<=>$a } map {$#$_} @{$arrays}{@arrNames};
my @outarray = map {
my $i = $_; # keep track of the current "row" to use in inner map
+ block
# get the respective element from each array.
# If we're beyond the individual array length, just use "\t".
# Also glue all cols together w/a "\t"
join "\t", map { $i <= $#$_ ? $_->[$i] : "\t" } @{$arrays}{@arrNam
+es}
} 0..$maxIdx;
}
my %arrayHolder = map { $_ => [] } 'a' .. 'z'; # build the initial d
+ata structure (instead of named arrays)
my @out = meld_columns( \%arrayHolder, 'a' .. 'h' ); # meld just A th
+rough H
my @out = meld_columns( \%arrayHolder, 'f', 'a', 'c' ); # meld just F
+ A C, in that order.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.