Perl array of arrays

Hi,

I am trying to assign an array as a value to one of the array element, I mean

I have an array @KS and array @kr.

I want array @KS to hold @kr as an element.

So I am doin this

$KS[1]=@kr;

But the value stored is number of elements in the @kr array.

Can someone help me with this.

Thanks in advance.

-Sunny

You cannot do that. An array element can hold a scalar, i.e. a string, a number, or a reference. The reference can be a "handle" to another array, so you can say

$KS[1] = \@kr;

but that's probably not what you want; printing $KS[1] will produce something like ARRAY (0x1CC01BED) and you have to use ${$KS[1]}[0] to access the first element of @kr, etc.

Another option would be to use splice to replace $KS[1] with all the elements of @kr, meaning $KS[1] will be the first element or @kr, $KS[2] will be the second, etc. splice - Perldoc Browser

See era post above. How you do it depends on your ultimate goal. The way era showed will store a reference to the array in $KS[1] which means any changes you make to $KS[1] will alter the array the reference points to. If you want to store a copy of the array on $KS[1] you would do this:

$KS[1] = [@kr];

That stores a copy of the array as it was at that point in the script. Any changes you make to it will not affect the original array. You will still have to use dereferencing to get to the data stored in $KS[1], but how also depends on what you will be doing later with that data. To loop through it:

for (@{$KS[1]} ) {
   print "$_\n";
}

Or to access individual indicies:

print $KS[1][0];