Solved

Dereferencing a 2D array?!

Posted on 2002-07-25
4
148 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now