473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Accessing element of array of arrays

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
4 1468
KevinADC
4,059 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
DELETED
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:
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
4,059 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

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

Similar topics

2
by: maciej | last post by:
I got an array that consists of elements that are arrays also. Now I wish I could add an element to the middle of it. Let mi give you an example: array ( - array(1,15,apple), -...
8
by: [RaZoR] | last post by:
hello, main script creates IE window, put an array there and then calls a child script. chils script makes an array element equal to some object and returns control to main script. then main...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
8
by: Dawn Minnis | last post by:
Hey guys If I have a program (see codeSnippet1) that I compile to be called test.o Then run it as test.o n n 2 3 4 I want the code to be able to strip out the two characters at the start...
2
by: George W. | last post by:
In perl I'm used to using hashtables of arrays and accessing a particular element like this: $HoA{flintstones} Simple as that. How do I access and update values of arrays stored in my hashtable...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
38
by: ssg31415926 | last post by:
I need to compare two string arrays defined as string such that the two arrays are equal if the contents of the two are the same, where order doesn't matter and every element must be unique. ...
5
by: Paul Brettschneider | last post by:
Hello, I have a global static array of structs and want to access a given element using an identifier. I don't want to use the element subscript, because it will change if I insert elements...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.