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!

references and dereferences 1

Status
Not open for further replies.

wimvanherp

Programmer
Mar 3, 2001
149
BE
Can anybody explain this :

my %Hash;
my @List=qq(test1 test2 test3);
$Hash{ListPtr}=\@List;
my $ptr=\@List;

my @secondlist=@$Hash{ListPtr};
foreach (@secondlist)
{ print $_;}

my @thirdlist=@$ptr;
foreach (@thirdlist)
{ print $_;}

@thirdlist has the same contents as @List but @secondlist is an empty array, what 's wrong ?

greets

Wim Vanherp
 
What you want is the following
Code:
my @secondlist = @{$Hash{ListPtr}};

Your original statement was dereferencing the scalar $Hash, instead of the reference found in $Hash{ListPtr}. You must therefore use brackets to indicate exactly what you are dereferencing.

If you had included a "use strict;" statement at the beginning of your script, an error would have been thrown alerting you to the syntax problem.
 
thanks ,

i allready tried with () brackets but that did not work.


Wim Vanherp
 
(), these aren't brackets, they're parenthesis. {} these are brackets (they're the same ones that enclose if and foreach blocks).

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Not to be hugely pedantic (ok, who am I kidding ;-)), but {} are braces. The term "brackets" when used by itself usually refers to square brackets [].
 
ishnid

What exactly do you mean by 'pedantic'? [smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I was just going by the terminology used in my all-time favorite Perl error:

Code:
Unmatched square or curly bracket at test.pl line 30

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top