0

私はこのperlスクリプトを持っています:

my %perMpPerMercHash;

foreach my $sheet () {   #proper ranges specified
    foreach my $row ( ) {    #proper ranges specified
        #required variables declared.
        push(@{$perMpPerMercHash{join("-", $mercId, $mpId)}}, $mSku); 
    }
}

#Finally 'perMpPerMercHash' will be a hash of array`
foreach my $perMpPerMerc ( keys %perMpPerMercHash ) {
    &genFile($perMpPerMerc, $perMpPerMercHash{$perMpPerMerc});
}

sub genFile {
    my ( $outFileName, @skuArr ) = @_;
    my $output = new IO::File(">$outFileName");
    my $writer = new XML::Writer( OUTPUT => $output, DATA_MODE => 1, DATA_INDENT => 2);
 #mpId is generated.
    &prepareMessage($writer, $mpId, @skuArr);
}

sub prepareMessage {
    my ( $writer,  $mpId, @skuArr ) = @_;
    my $count = 1;
    print Dumper \@skuArr;    #Printing correctly, 8-10 values.
    foreach my $sku ( @skuArr ) {   #not iterating.
        print "loop run" , $sku, "\n";   #printed only once.
    }
}

なぜこれが起こっているのか誰か助けてください。私は perl に不慣れで、この異常を理解できませんでした。

編集: ダンパーの出力:

$VAR1 = [
          'A',
          'B',
          'C',
        ];
4

1 に答える 1

3

あなたがするとき

&genFile($perMpPerMerc, $perMpPerMercHash{$perMpPerMerc});

配列への参照を渡しています。

だから

sub genFile {
    my ( $outFileName, @skuArr ) = @_;

あなたがしなければなりません :

sub genFile {
    my ( $outFileName, $skuArr ) = @_;

を使用します@$skuArr

参考文献を見てみよう

変更された genFile サブは次のようになります。

sub genFile {
    my ( $outFileName, $skuArr ) = @_;
    my $output = new IO::File(">$outFileName");
    my $writer = new XML::Writer( OUTPUT => $output, DATA_MODE => 1, DATA_INDENT => 2);
 #mpId is generated.
    &prepareMessage($writer, $mpId, @$skuArr);
}

そして、他のサブは変更する必要はありません。

skuArr または、常に参照渡しすることもできます。

&genFile($perMpPerMerc, $perMpPerMercHash{$perMpPerMerc});
...
sub genFile {
    my ( $outFileName, $skuArr ) = @_;
    ...
    &prepareMessage($writer, $mpId, $skuArr);
}

sub prepareMessage {
    my ( $writer,  $mpId, $skuArr ) = @_;
    my $count = 1;
    print Dumper $skuArr; 
    foreach my $sku ( @$skuArr ) {
        print "loop run" , $sku, "\n";
    }
}
于 2013-10-30T13:06:35.660 に答える