CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
array of arrays question

 



alferic
Novice

Apr 22, 2014, 5:31 AM

Post #1 of 7 (2878 views)
array of arrays question Can't Post

hello Gurus,

I have 2 arrays and I wanted to add both arrays to create array of arrays. But when I tried to access the elements it is not working and does not display. It seems that it joined the arrays instead of creating array of arrays.

I have tried the code below. Please advise what is wrong.

#!/usr/bin/perl

@array1 = qw(a b c);
@array2 = qw(d e f);
@AoA = ();

push (@AoA, (@array1,@array2));
print "@AoA\n";

print "$AoA[1][1]\n";



FishMonger
Veteran / Moderator

Apr 22, 2014, 6:54 AM

Post #2 of 7 (2873 views)
Re: [alferic] array of arrays question [In reply to] Can't Post


Code
my @AoA = ( \@array1, \@array2 );


or

Code
my @AoA = (); 

push @AoA, ( \@array1, \@array2 );



(This post was edited by FishMonger on Apr 22, 2014, 6:54 AM)


Laurent_R
Veteran / Moderator

Apr 22, 2014, 10:09 AM

Post #3 of 7 (2866 views)
Re: [alferic] array of arrays question [In reply to] Can't Post

Hi,

FishMonger has given you two solutions, but let me complete it with an short explanation on your error.


In Reply To

Code
@AoA = (); 

push (@AoA, (@array1,@array2));



As you seem to suspect, yes, indeed, the above code will concatenate the two arrays, i.e. make a single simple array with all the elements of the first array and then all the elements of the second array.

Actually any time you write something like:

Code
(@array1,@array2)

you actually obtaining a list of all elements in the first array and then all the element of the second. This also happens if you try to pass two arrays to a subroutine, it does not work because the two arrays get merged into a single one. The solution is to use reference, just the same way FishMonger did.


BillKSmith
Veteran

Apr 22, 2014, 11:38 AM

Post #4 of 7 (2863 views)
Re: [alferic] array of arrays question [In reply to] Can't Post

You could also use:

Code
my @AoA = ();  
push @AoA, ( [@array1], [@array2] );


This "works", but with a subtle difference that can be important. In Fishmonger's code, the elements of the subarrays of @AoA are aliases for the corresponding elements of the original arrays. A change to one changes the other. The array above is an array of references to two anonymous arrays. They are initialized to be the same as your original arrays, but from then on, @AoA is completely independent of your original arrays. Use the one that you need.
Good Luck,
Bill


alferic
Novice

Apr 22, 2014, 10:37 PM

Post #5 of 7 (2858 views)
Re: [FishMonger] array of arrays question [In reply to] Can't Post

Hi FishMonger;

Thanks a lot for the quick solution.


alferic
Novice

Apr 22, 2014, 10:39 PM

Post #6 of 7 (2854 views)
Re: [Laurent_R] array of arrays question [In reply to] Can't Post

Thanks Laurent for the wonderful explanation.


alferic
Novice

Apr 22, 2014, 10:41 PM

Post #7 of 7 (2851 views)
Re: [BillKSmith] array of arrays question [In reply to] Can't Post

Thanks Bill, your magic always works!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives