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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array References (i.e. ARRAY(0x27ca184) ) 1

Status
Not open for further replies.

jez

Programmer
Joined
Apr 24, 2001
Messages
370
Location
VN
Hi all,

I keep running into these Array refs like ARRAY(0x27ca184), I have fixed them a couple of times, but I don't know how i did it.
In the following snippet, where the first commented print instruction is, I get the real value, but in the next 'foreach' loop, I just get the 'ARRAY(0x27ca184)'...any enlightenment on how I can deal with these would be great..

snippet of script:-

sub mainone {
print header;
opendir(MDS, $startpath) ||
die "Cannot open $lookpath: $!\n";
@dirs=grep(!/^\.\.?$/, readdir MDS);
closedir(MDS);

foreach $file(@dirs) {
if ($file =~m/^h(.*)/i) {
push (@historical, [$file]);
#print $file;} ## this print the file name
else { push (@current, [$file]);
#print $file;} ## this too
}

if ($hist > 0) {
print &quot;H<br>&quot;;
foreach $files(@historical) {
$inform = $startpath . $files . $slash . $infofile;
print $inform;
print $files; ## this shows the array ref.
open (FIL, $inform) ||
die &quot;Cannot open data file: $!\n&quot;;
@indat = <FIL>;
close FIL;
push (@datafil, [@indat]);
}
}
else {
print &quot;C<br>&quot;;
foreach $files(@current) {
print $files;
$inform = $startpath . $files . $slash . $infofile;
print $inform;
open (FIL, $inform) ||
die &quot;Cannot open data file: $!\n&quot;;
@indat = <FIL>;
close FIL;
push (@datafil, [@indat]);
}
}

As you can see this sub is returning a listing of files / folders in a specific directory, and separating them only whether the folder (in this case) has a 'H' at the beginning of the name.

Any help is much appreciated.

Jez
 
Square brackets means create a reference to an anonymous array, so I think that when you do

push (@historical, [$file]);

you are asking perl to push a ***reference*** to an anonymous array(and that anonymous array contains 1 element - that being the value of $file) onto array @historical. Why are you using the square brackets? It seems that you weren't even aware what that was causing. Why not just do

push(@historical, $file);

That pushes the value of $file onto array @historical, which may be what you intended in the first place.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Thankyou, you sussed it out exactly, I was using the square brackets without knowing they had the effect of creating a reference.
I am still new (ish) to Perl, and in a previous script I did this same type of push (with sqaure brackets) and it seemed to work ok, so I did it again in this script.

Thanks for the help, I am now getting the results I want.


Jez

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top