Solved

Dereferencing a 2D array?!

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validating the data using Oracle DBD module. 5 65
Reading fields from the text file. 4 92
stftime format 4 55
Add additional column to .csv using Perl. 8 128
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

23 Experts available now in Live!

Get 1:1 Help Now