简体   繁体   中英

Copying 2D array in Perl

I am trying to copy a 2D array into another array. 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:

#!/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? 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.

Had you done so in this case you would have been warned that you couldn't assign to $array2[0][0] etc.

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.

All of this would be apparent if you had enabled 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.

Moreover, if you only need to print this data, you could just use Data::Dumper for that.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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