467,202 Members | 1,027 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,202 developers. It's quick & easy.

Accessing element of array of arrays

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
  • viewed: 1348
4 Replies
Expert 2GB
see tek-tips.
Jul 5 '08 #2
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
Jul 8 '08 #3
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:
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
Expert 2GB
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.

Similar topics

6 posts views Thread by Chris Styles | last post: by
2 posts views Thread by George W. | last post: by
13 posts views Thread by Joseph Garvin | last post: by
1 post views Thread by Rob Griffiths | last post: by
5 posts views Thread by Paul Brettschneider | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.