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:[EMAIL PROTECTED] 
Sent: Friday, June 01, 2007 4:39 PM
To: beginners@perl.org; [EMAIL PROTECTED]
Subject: Re: Simplest hash initialization with no value

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/



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to