简体   繁体   English

在Perl中复制2D数组

[英]Copying 2D array in Perl

I am trying to copy a 2D array into another array. 我正在尝试将2D数组复制到另一个数组中。 However, I am running into a strange problem where the first row does not get printed by my function. 但是,我遇到了一个奇怪的问题,我的函数没有打印第一行。 However, values are there since when I print the first row separately it does get printed. 但是,存在值,因为当我分别打印第一行时,确实会打印出来。

This is my perl code: 这是我的perl代码:

#!/usr/bin/perl
use UNIVERSAL 'isa';

#Function to print the contents of an array
sub printArray {
    my ($vHash) = @_;
    if (isa($vHash, 'ARRAY')) {
        foreach my $el (@$vHash) {
            print "A: $el\n";
            printArray($el);
        }
    } else {
        return;
    }
    return;
}

my @array = "";
my @line1 = (1, 2, 3);
my @line2 = (3, 4, 5);
my @line3 = (6, 7, 8);

#Creating a 2D array
@array = (\@line1, \@line2, \@line3);

my @array2 = "";
my $row_size = @array;
print "ROW SIZE: $row_size\n";
my $column_size = @{$array[0]};
print "COL Size: $column_size\n\n\n";

#Copying over the 2D array...element by element
for (my $i=0; $i<$row_size; ++$i) {
    for (my $j=0; $j<$column_size; ++$j) {
        $array2[$i][$j] = $array[$i][$j];
    }
}

print "\n\nORIG ARRAY\n\n";
printArray(\@array);
print "\n\nCOPY ARRAY\n";
printArray(\@array2);

print "\n\n";

#Printing the first row separately
foreach my $ele (@{$array2[0]}) {
    print "V: $ele\n";
}

This is the output of the program: 这是程序的输出:

ROW SIZE: 3
COL Size: 3




ORIG ARRAY

A: ARRAY(0x65b110)
A: 1
A: 2
A: 3
A: ARRAY(0x65b170)
A: 3
A: 4
A: 5
A: ARRAY(0x65b1d0)
A: 6
A: 7
A: 8


COPY ARRAY
A:                                                     #WHY THIS IS MISSING
A: ARRAY(0x6d1f00)
A: 3
A: 4
A: 5
A: ARRAY(0x6d1f50)
A: 6
A: 7
A: 8


V: 1                                                  #THESE VALUES ARE COMING FINE
V: 2
V: 3

I am really confused as to what am I doing wrong here!! 我真的很困惑我在这里做错了什么! Why the function printArray not printing first row? 为什么函数printArray不打印第一行? Any help would be highly appreciated. 任何帮助将不胜感激。 Thanks, Newbie 谢谢,新手

You must always use strict and use warnings at the start of your program, especially before asking for help with your code. 您必须始终在程序开始时始终 use strictuse warnings ,尤其是在寻求代码帮助之前。

Had you done so in this case you would have been warned that you couldn't assign to $array2[0][0] etc. 如果您在这种情况下这样做,将被警告您无法分配给$array2[0][0]等。

You have initialised your arrays using 您已经使用初始化了数组

@array2 = "";

which creates an array with one element equal to the null string ("") . 它创建一个数组,该数组的一个元素等于空字符串("") Now that it has a value, Perl cannot auto-vivify an array reference in the first element for you, so your assignments fail. 现在它已经有了一个值,Perl将无法为您自动调整第一个元素中的数组引用,因此您的分配将失败。

All of this would be apparent if you had enabled strict . 如果启用了strict那么所有这些都将显而易见。 The error messages would explain exactly what was going wrong. 错误消息将确切说明出了什么问题。

There is no need to initialise a new array if you want it empty, but the correct way to do so would be 如果您希望将其清空,则无需初始化新数组,但是正确的方法是

@array = ();

By the way, you do not need to write copy functions like this one. 顺便说一句,您不需要编写像这样的复制功能。

use Storable qw(dclone);
my @array2 = @{ dclone(\@array) };

dclone would make a deep copy of any perl data structure. dclone可以复制任何perl数据结构。

Moreover, if you only need to print this data, you could just use Data::Dumper for that. 此外,如果只需要打印此数据,则可以使用Data::Dumper进行打印。

use Data::Dumper;
print Dumper(\@array), "\n"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM