RE: Simplest hash initialization with no value  But when decalared this way it gives a warning message :"Use of
uninitialized value in concatenation (.) or string at"
	
in a print statement which is something like this :

		my %myhash = map { $_ => undef } qw/A B C D E F G H I J
K/;
		foreach my $key (%myhash){
			print $myhash{$key}
		} 


-----Original Message-----
From: Paul Lalli [mailto:mritty@gmail.com] 
Sent: Friday, June 01, 2007 4:39 PM
To: beginners@perl.org; perl-beginners@moderators.isc.org
Subject: Re: Simplest hash initialization with no value

On Jun 1, 6:08 am, alok.n...@hp.com (Alok Nath) wrote:

> Why it gives syntax error when -
>         my @myhash{qw/A B/} = ();

Er... because it's not valid syntax.  That's why you'd get a syntax
error. Not sure what you're asking. You can't "declare" a hash slice.
You declare variables.

> It works when I say -
>         my %myhash ;
>         @myhash{qw/A B/} = ();

Right.  Because that is valid syntax.  First you declare %myhash, then
you initialize two keys of that hash using a slice.

Your original question was "how to initialize a hash".  It's apparent
now that you wanted to know how to "declare and initialize a hash in one
step".  This question comes up so frequently that it really should be a
FAQ.  The way you did so was fine originally.  What's wrong with it?  If
you have a longer list of keys, you could use the map operator as a
shortcut, so you don't write 'undef' multiple times:

my %myhash = map { $_ => undef } qw/A B C D E F G H I J K/;

Paul Lalli


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org For additional
commands, e-mail: beginners-help@perl.org http://learn.perl.org/



-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/



