Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

how to declare the hash

by nnvgps (Initiate)
on Jul 05, 2013 at 10:32 UTC ( #1042675=perlquestion: print w/ replies, xml ) Need Help??
nnvgps has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to declare a hash in the following way

%flag = 0;

In the above declaration, script throws a warning like odd number of hash assignments. How to resolve this declarion?

Comment on how to declare the hash
Download Code
Re: how to declare the hash
by j0se (Pilgrim) on Jul 05, 2013 at 10:34 UTC

    my %flag;

    Update: for more see Hashes in Perl and perldata.

    Well done is better than well said. -- Benjamin Franklin

Re: how to declare the hash
by rjt (Deacon) on Jul 05, 2013 at 10:42 UTC

    That's because you're trying to assign a single value to a hash. (A single value being an odd number (1) of values.) Hashes are initialized as an ordered list of key/value pairs, like this:

    my %flag = ( name_of_the_flag => 0, another_flag => 1, third_one => 0, ); # Later... print "another_flag is set\n" if $flag{another_flag};

    If you do indeed just want a single value called "flag", use a scalar:

    my $flag = 0; print "\$flag is set\n" if $flag;

    If neither of those is what you're looking for, you'll have to go into a bit more detail on what it is you're trying to accomplish (rather than only telling us how you're trying to accomplish it, as you have done.)

    But see also, Perl variable types.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1042675]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-12-25 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls