in reply to my vs. local subtlety
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.
|
---|
In Section
Cool Uses for Perl