By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,660 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,660 IT Pros & Developers. It's quick & easy.

Accessing element of array of arrays

P: 1
Hi. I'm a relatively new Perl programmer, and I'd like some help working with two arrays of arrays. I want to store a value from the first one, @AoA1, into the second one, @AoA2, without changing @AoA1. At first I was trying $AoA2[foo][bar] = $AoA1[foo][bar]. Now, I don't even really understand what's happening when I use the [][] indexing - I think I end up assigning a reference instead of a value - but the effect is that when I change that value of @AoA2 again it changes the corresponding value of @AoA1, which is not what I want. So how do I dereference the value I want from @AoA1?
Jul 5 '08 #1
Share this Question
Share on Google+
4 Replies


KevinADC
Expert 2.5K+
P: 4,059
see tek-tips.
Jul 5 '08 #2

P: 9
I think you probably write some brackets in the wrong place. That is my best guess. Try to google it out.
http://www.google.ro/search?q=perl+a...ient=firefox-a or
DELETED
Jul 8 '08 #3

P: 1
you cannot use 'foo' and 'bar' as indexes of an array. Array indexes are numeric. You can use $foo and $bar if they contain numeric values. That may be your problem.

I wrote the following program to show the proper way of doing this. Paste it into a file named atest.pl, then run it with 'perl ./atest.pl'.

-- file start --
$a1[2][3] = "one";
$a2[2][3] = "two";
$a2[2][3] = $a1[2][3];
print $a1[2][3], "\n";
print $a2[2][3], "\n";
-- file end --

The output will be:
one
one
lines 1 & 2 initialize two different arrays, each containing a reference to another array (an array of an array).
Line 3 modifies the second array, without changing the first.
The last 2 lines print the contents.

A better way to write it out if you're having trouble understanding it is as follows
Expand|Select|Wrap|Line Numbers
  1. $a1[2]->[3]
It's easy to read it from right to left as follows:
the third element of an array referenced by the second element of the array named a1. So when reading from right to left, just replace the arraow with the words "referenced by".

Hope that makes some sense to you. If you need more specific help, try pasting a few lines of actual code, and actual output that we can work with.
Jul 9 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
NIce of you to reply but this question was solved on another forum a few days ago. He posted this same question on several perl forums.
Jul 9 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.