in reply to for loop
The output is:#!/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"; }
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
|
|---|