Solved

Dereferencing a 2D array?!

Posted on 2002-07-25
4
151 Views
Last Modified: 2006-11-17
Hello all,

I'm having a nasty time with the following code:

push @endArray, [$arrayToSort[$i][0], $arrayToSort[$i][1]];

I'm trying to push the contents of one 2D array into another, one item at a time.  In the above example, I'm trying to do it by (making a fool of myself and) creating a mini-array to push onto @endArray.

The thing is, almost every flavor of what I've tried has given me the ADDRESS of the array item, and not the 2D value of the item itself.  What am I doing wrong?  Do I need to dereference this address?  Is there another way to pass this info??

Thanks,

Raydot.
     
0
Comment
Question by:Raydot
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:sstouk
ID: 7178604

Here is  a demo for you of how to grab your data back.

$arrayToSort[1][0] = "First Element";
$arrayToSort[1][1] = "Second Element";

push @endArray, [$arrayToSort[1][0],$arrayToSort[1][1]];

foreach $Level1 (@endArray)
{
@ArrToSort = @$Level1;
     foreach $Level2 (@ArrToSort)
     {
     print "$Level2\n";
     };
};

Try it.
0
 
LVL 3

Author Comment

by:Raydot
ID: 7178660
Ah...it's not the grabbing back that isn't working, it's the putting in.  The push command itself is no good...

Or wait, no it isn't, I did try it and it worked.  So you're saying the problem was not my push, but my pop?

Could you just break down for me what you're doing with:

@ArrToSort = @$Level1;

?

Thanks...
0
 
LVL 6

Accepted Solution

by:
sstouk earned 100 total points
ID: 7179009
This is a dereferencing:
@ArrToSort = @$Level1;

OK,
Let me explain:

You have an array with say, 3 elements

@MainArray = ($el1,$el2,$el3);
They might be simple scalars, holding a number, letter or a string.
Each of them might also be a memory address, pointing to the start of another array.
e.g.
Let's have 3 arrays
@arr1 = ("Elem11","Elem12","Elem13");
@arr2 = ("Elem21","Elem22","Elem23");
@arr3 = ("Elem31","Elem32","Elem33");

How do we put the addresses - pointers of these thee arrays into @MainArray?

Here is how - using referencing:

@MainArray = (\@arr1,\@arr2,\@arr3);

Now if we loop through the first @MainArray and get each element, it will be a memory address internally represented as "ARRAYXX" or something like that. (print it out and check)
So it is not a scalar, but a reference to an ARRAY

we need to take that element (reference) and De-reference it into array. Reference has to be converted back to the original type like this:
@arr1 = @$MainArray[0];
@arr2 = @$MainArray[1];
@arr3 = @$MainArray[2];

And now you can access each element in any array:

print $arr1[0];
# Would print "Elem11"

print $arr2[2];
# Would print "Elem23"

And so on.

I hope this clears things out.




0
 
LVL 3

Author Comment

by:Raydot
ID: 7180376
Just the answer I was looking for.  If only all respondents were quite this diligent...

Thanks,

Raydot.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question