Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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?

Replies are listed 'Best First'.
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.

Re: how to declare the hash
by reisinge (Friar) 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

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?
[holli]: No, that's not true. I started it.
[holli]: I was company to my mother, bringing her to the therapy she has to take. There was another so called therapist outside smoking. We engaged in a talk with her and he turned out to be one of these lunatics telling people when they are sick it's their own
[holli]: fault for not thinking positively enough.
[LanX]: did he seriously hurt his fist at your nose in the end?
LanX .oO(positively thought)
[holli]: :) No. I might be small, but you don't want to be around me when I flip.
[LanX]: I can imagine ...
LanX hurts to be bitten between the legs ;-)
[holli]: You see, I have a friend whose daughter has been damaged seriously because she wasn't vaccinated against measles. (Her mother being anti-vaxx).

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2017-12-13 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (373 votes). Check out past polls.

    Notices?