On Jun 1, 6:08 am, [EMAIL PROTECTED] (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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to