This is one way to handle this problem. It is tied to the exact layout of @matrix, so beware:
#!/usr/bin/perl
use warnings;
use strict;
my @matrix = ([3, 4, 10, 2],
[2, 7, 12, 3],
[0, 3, 4, 5],
[6, 5, 9, 11]);
print_2by2(\@matrix);
sub print_2by2 {
my $aref = shift;
my @copy = @{$aref};
my $length = scalar @matrix;
my $count = 1;
while ( $length > 0 ) {
$length -= 2;
my $rowref1 = shift @copy;
my $rowref2 = shift @copy;
my $rowlength = scalar @{$rowref1};
for ( my $j = 0; $j < $rowlength/2; $j++ ) {
my $index = $j + $count;
print $index, ":";
print " ";
}
print "\n";
$count += $rowlength/2;
rowprint($rowref1);
rowprint($rowref2);
print "\n";
}
}
sub rowprint {
my $rowref = shift;
my $rowlength = scalar @{$rowref};
for ( my $i = 0; $i < $rowlength; $i++ ) {
print $$rowref[$i];
if ( $i % 2 == 0 ) {
print ", ";
}
else {
print " ";
}
}
print "\n";
}
The output is:
C:\Code>perl fancy_print.pl
1: 2:
3, 4 10, 2
2, 7 12, 3
3: 4:
0, 3 4, 5
6, 5 9, 11
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.