CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
A simple hash and arrays problem?

 



KeithBoynton
New User

Feb 25, 2004, 3:27 PM

Post #1 of 3 (623 views)
A simple hash and arrays problem? Can't Post

A simple problem very probably:

I can define an hash containing an array like this (and it works fine):


Code
my @array=('one','two'); 
my %hash= ( 1 => { 'Name' =>Something',
'Root' =>'Something else'
},

2 => { 'Name' =>'Something Again',
'Root' =>'Something else ..',
'anArray' => \@array
}
);


Yet if I try to define it like this, it doesn't work:


Code
my %hash= ( 	1 => {	'Name'  =>Something', 
'Root' =>'Something else'
},

2 => { 'Name' =>'Something Again',
'Root' =>'Something else ..',
'anArray' => \('one','two')
}
);



Any ideas why this doesn't work and how I should be doing it?

Thanks in advance :)


davorg
Thaumaturge / Moderator

Feb 25, 2004, 11:45 PM

Post #2 of 3 (621 views)
Re: [KeithBoynton] A simple hash and arrays problem? [In reply to] Can't Post

From "perldoc perlref":

Quote
Taking a reference to an enumerated list is not the same as using
square brackets--instead itís the same as creating a list of references!

@list = (\$a, \@b, \%c);
@list = \($a, @b, %c); # same thing!


You can't create a reference to a list. You need to take the reference of an array. You can avoid having to use an intermediate array variable by using the anonymous array constructor - [ .. ].


Code
my %hash= ( 	1 => {	'Name'  =>Something',  
'Root' =>'Something else'
},

2 => { 'Name' =>'Something Again',
'Root' =>'Something else ..',
'anArray' => ['one','two']
}
);


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KeithBoynton
New User

Feb 26, 2004, 12:39 AM

Post #3 of 3 (618 views)
Re: [davorg] A simple hash and arrays problem? [In reply to] Can't Post

Great stuff, thank you very much! Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives