Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi everyone. Ok i give up. How do

Status
Not open for further replies.

crackn101

Programmer
Dec 27, 2002
63
US
Hi everyone.
Ok i give up.
How does one reference the below data.
I keep geting an "ARRAY(0x1852970)", but not the data.
I've tried everything i could think of.
Thanks for the suggestions.

@gData = ( ['BIFFwriter.pm', 275],
['Big.pm', 99],
['Chart.pm', 269],
['Format.pm', 724]
);
 
The "rows" of @gData are references to anonymous arrays. You need to dereference them in order to get at the data, e.g.
Code:
#!perl
use strict;
use warnings;

my @gData = (  ['BIFFwriter.pm',   275],
            ['Big.pm',           99],
            ['Chart.pm',        269],
            ['Format.pm',       724]
          ); 

for my $ref (@gData) {
    printf "%-15s%3d\n", @$ref
}
Output:
Code:
BIFFwriter.pm  275
Big.pm          99
Chart.pm       269
Format.pm      724
I've tried everything i could think of.
Have you tried reading the documentation? There's loads on this subject. See

perldoc perlreftut
perldoc perlref
perldoc perldsc
perldoc perllol


HTH

 
I was reading something about this the other day, multi dimensional arrays generally work in a very similar way to standard arrays. i.e., for each dimension you require a reference. For example:
Code:
print $gData[2][1]; # print 269
If you can get hold of 'Programming Perl' by O'Reilly, there's a huge section starting from page 257. If you just wanted to print the entire set of arrays you could try:
Code:
for $array_ref(@gDate)  {
   print "\t [ @array_ref ],\n";
}
which is what mikevh said!

Rob Waite
 
for $array_ref(@gDate) {
print "\t [ @array_ref ],\n";
}
Did you try that? Maybe you meant this.
Code:
for my $array_ref(@gData)  {
   print join("\t", @$array_ref),"\n";
}
[ @array_ref ] would create a new reference to the array @array_ref, if such an array existed. However, it doesn't. There is the scalar $array_ref, which is a reference to one of the rows of @gData, and that is an array, which can be gotten to by dereferencing. But even assuming you got the dereferencing part right, e.g.,
Code:
for my $array_ref(@gData)  {
   print "\t", [ [b]@$array_ref[/b] ],"\n";
}
you're still trying to print out a reference. The @ dereferences $array_ref, but the [] create a new reference, and that's what you'd end up printing, which would give you something like this:

ARRAY(0x22515c)
ARRAY(0x22515c)
ARRAY(0x22515c)
ARRAY(0x22515c)


which is exactly what crackn101 was trying to avoid.
which is what mikevh said!
Nope, sorry, that ain't what I said. [3eyes]




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top