2011-06-10 72 views
2

好了的列方向,這僅僅是興趣的緣故:如何循環利用細胞在二維數組

如果由於某種原因,我要向下移動一個二維數組的「列」。我會怎麼做?

#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"; 
    } 
} 

一些粗略的想法:switch for loop?或運行索引[0-5]

+0

你能舉一個例子,輸入/輸出應該是什麼樣子? 3x3陣列就足夠了。 (目前看起來,如果您將列向下移動,則底部會有新行) – 2011-06-10 20:22:12

+0

請停止在標題中寫入標籤。 – 2011-06-10 20:49:47

+0

@Tomalak Geret'kal:我認爲如果你看到需要什麼編程知識,對你們來說會更容易。我承認這個問題是無關緊要的(幾乎)。我不會再這樣做了,謝謝你讓我知道。 – Jon 2011-06-11 03:00:59

回答

2

由於您的數據結構是面向行的,因此您必須循環遍歷列索引。

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

說一個散列,(0..5)是否會切換到散列的鍵? – Jon 2011-06-12 20:42:28

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) 

會來救我被被指抄襲。