如何在2D数组的列方向上按单元循环

时间:2011-06-10 20:17:40

标签: perl multidimensional-array

好的,这只是为了感兴趣:

如果由于某种原因,我想向下移动二维阵列的“列”。我该怎么做?

#LEGEND FOR BELOW#
##Each index of @all_matches, de-referenced, (ie. $$arrayref[0-5]) is a COLUMN
##Each @$arrayref is a ROW
##Current Set-up: Each $item is one cell in chart, moving across the row.
##How to move down column?

for my $arrayref (@all_matches) { 
    for my $item (@$arrayref) { 
        print $item, "\n\n\n";
    }
}

一些粗略的想法:切换循环?或者通过[0-5]

的指数运行

2 个答案:

答案 0 :(得分:2)

您必须遍历列索引,因为您的数据结构是面向行的。

for my $i (0 .. 5) { 
  for my $arrayref (@all_matches) { 
    print $arrayref->[$i], "\n\n\n";
  }
}

答案 1 :(得分:0)

“使用(数字)指数”的想法是(c)cjm。但我希望我的“框架”能够处理SO Perl问题:

# !! http://stackoverflow.com/questions/6311638
#    perl-how-to-cycle-by-cell-in-the-column-direction-of-a-2d-array
# =============================================================================
###############################################################################

use strict;
use warnings;
use English qw( -no_match_vars  );
use Time::HiRes qw( time );

our $Problem = [
  ['R0C0','R0C1','R0C2','R0C3'] ,
  ['R1C0','R1C1','R1C2','R1C3'] ,
  ['R2C0','R2C1','R2C2','R2C3']
];

my %XplFncs = ();

$XplFncs{ lc( 'Problem' ) } = [ 'Problem', 'by row is easy', \&problem ];
sub problem {
  my $exit_code = 1; # assume error
  for my $rowref (@$Problem) { 
      for my $cell (@$rowref) { 
        print $cell, " ";
    }
    print "\n";  
  }
  return $exit_code;
}

$XplFncs{ lc( 'usenumidx' ) } = [ 'usenumidx', 'use numerical indices', \&usenumidx ];
sub usenumidx {
  my $exit_code = 1; # assume error
  for (my $col = 0; $col < scalar @{$Problem->[0]}; ++$col) { 
      for (my $row = 0; $row < scalar @$Problem; ++$row) { 
        print $Problem->[$row]->[$col], " ";
    }
    print "\n";  
  }
  return $exit_code;
}

sub Main {
  my ( $ExitCode, $ToBlame ) = ( 3, join( '::', $PROGRAM_NAME, 'Main' ) );
  printf "%s started using Perl %s on %s.\n", $ToBlame, $], $OSNAME;
  my $FncNam   = $ARGV[ 0 ] || 'Problem';
  my $FncNamLC = lc( $FncNam );
  if (exists( $XplFncs{ $FncNamLC })) {
     my $Xpl = $XplFncs{ $FncNamLC };
     printf "will call %s - %s\n", $Xpl->[ 0 ], $Xpl->[ 1 ];
     my $tmStart = time();
     $ExitCode   = $Xpl->[ 2 ]();
     my $tmEnd   = time();
     printf "%s returned %d [%f secs]\n", $Xpl->[ 0 ], $ExitCode, $tmEnd - $tmStart;
  } else {
     printf   "sorry, no '%s' in:\n  %s\n"
            , $FncNam
            , join( "\n  ", map { sprintf( '%s - %s', $_, $XplFncs{$_}[1] ) } sort( keys( %XplFncs ) ) );
  }
  printf "%s done. (%d)\n", $ToBlame, $ExitCode;
  return $ExitCode;
}

exit Main();

输出:

perl xpl.pl
xpl.pl::Main started using Perl 5.010000 on MSWin32.
will call Problem - by row is easy
R0C0 R0C1 R0C2 R0C3
R1C0 R1C1 R1C2 R1C3
R2C0 R2C1 R2C2 R2C3
Problem returned 1 [0.001498 secs]
xpl.pl::Main done. (1)

perl xpl.pl usenumidx
xpl.pl::Main started using Perl 5.010000 on MSWin32.
will call usenumidx - use numerical indices
R0C0 R1C0 R2C0
R0C1 R1C1 R2C1
R0C2 R1C2 R2C2
R0C3 R1C3 R2C3
usenumidx returned 1 [0.002094 secs]
xpl.pl::Main done. (1)

将使我免于被指控抄袭。