It looks like just two passes through a 2D array to me, which can be factorised into one pass through the outer dimension:
my @TwoD = ( ['A','B','C'], ['A','D','C'], ['A','B','C'] );
my ( $slotH, %slotH ) = (-1,());
my @result = ();
for ( my $i1 = 0; $i1 <= $#TwoD; $i1++ ) {
for ( my $i2 = 0; $i2 <= $#{ $TwoD[ $i1 ]}; $i2++ ) {
my $v = $TwoD[ $i1 ][ $i2 ];
defined( $slotH{ $v } ) or $slotH{ $v } = ++$slotH;
$result[$i1][$slotH{ $v }] = $v;
}
for ( my $i2 = 0; $i2 <= $slotH; $i2++ ) {
$i2 and print "\t";
print ( $result[$i1][$i2] || '' );
}
print "\n";
}
produces
A B C
A C D
A B C
Update: actually my version needs its output rotating 90 degrees to get exactly the same output, but the technique is similar for doing that, resulting in minor change to the above script.
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.