Perl array of arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl array of arrays
# 1  
Old 10-06-2008
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
# 2  
Old 10-06-2008
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

Code:
$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. http://perldoc.perl.org/functions/splice.html

Last edited by era; 10-06-2008 at 03:27 PM.. Reason: Link to doc
# 3  
Old 10-06-2008
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:

Code:
$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:

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

Or to access individual indicies:

Code:
print $KS[1][0];

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiply elements of 2 arrays together into another array

So I need to Write an array processing program using a Linux shell programming language to perform the following. Load array X of 20 numbers from an input file X. Load array Y of 20 numbers from an input file Y. Compute array Z by multiply Xi * Yi then compute the square-root of this... (2 Replies)
Discussion started by: sarapham409
2 Replies

2. Programming

Looping an array of 2d arrays in C

Le sigh... Hopefully this will be the last time I have to ask for help on this topic. For a while now I've been working with a 1d array that holds 2d arrays. For reference you can view here. Now I'm just trying to loop through the elements with the following: #include <stdio.h> void... (3 Replies)
Discussion started by: Azrael
3 Replies

3. Programming

Calling an array of arrays in C.

Corona688 was great in helping me learn how to create arrays that hold other two dimensional array here. Unfortunately I didn't think ask about how to implement or call them. Basically, I'm trying to call an array of two-dimensional arrays like this: declaration: int (*side_one) = { { white_l1,... (6 Replies)
Discussion started by: Azrael
6 Replies

4. Programming

Making an array of 2D arrays in C

I hate I'm asking for help again. Unfortunately it seems there just aren't any links I can find on making an array that holds a bunch of two dimensional arrays. Maybe my google-fu is lacking. Basically I have a header file like this: #define MATRIX 10 int white_l1; int white_l2; int... (2 Replies)
Discussion started by: Azrael
2 Replies

5. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

6. Shell Programming and Scripting

using arrays and also help with array.contains functionality

here is what i have... i=1 while read line do if grep -i-q "create procedure"<<<$line then startline="$line" endline="blahblah" Get procedure name into a variable named procName procName="procedure name is stored" do some... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Shell Programming and Scripting

perl - need help with 2 arrays to hash or 2d array?

I have 2 arrays: @array1 outputs the following: 1 1 1 2 @array2 outputs the following A B C D (2 Replies)
Discussion started by: streetfighter2
2 Replies

8. Programming

Perl arrays and loop through array

Hi All I need to get <STDIN> from a user. the <STDIN> is a range of number delimited by "," (comma) and can be with range delimited by "-". Example: 1,2,3,4-9,12,15,34-36,70 Now I need to get this from the user and go on each number and "Do something"... but when trying to do this as above... (2 Replies)
Discussion started by: RedGrinGo
2 Replies

9. Shell Programming and Scripting

PHP arrays as array elements

PHP question...I posted this on the Web Development forum, but maybe this is a better place! I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person... (3 Replies)
Discussion started by: JerryHone
3 Replies

10. Shell Programming and Scripting

Searching array of arrays in perl

Suppose there are two arrays of arrays: @A = ( , , , ); @B = ( , , , , ); For each of $A, $A, $A..., I want to find the corresponding one in @B (match the letter, like $A eq $B), and print out both the second item, for example, $A and $B. How can I do this in perl? grep + map? Hope I... (1 Reply)
Discussion started by: zx1106
1 Replies
Login or Register to Ask a Question