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

What causes HASH to print?

by kmullin5016 (Acolyte)
on Feb 27, 2007 at 15:38 UTC ( [id://602312]=perlquestion: print w/replies, xml ) Need Help??

kmullin5016 has asked for the wisdom of the Perl Monks concerning the following question:

OK, this is a "what's wrong with my code" question, which is probably an inappropriate use of this site, but I'm stuck and I have nowhere else to go, and I think I've nailed the problem done pretty well, I just don't know the solution. Here we go.

Here is the definition of the problem hash in my code:

%SA_DATA_LOCK_DOWN = { 'SECALERT_ALERTS_ID'=>0, 'SECALERT_NUM'=>0, 'TECH_SPEC_ID'=>0, 'OS_APP_NAME'=>0,

and there is more to it. I test the population of this with this code:

foreach (keys(%SA_DATA_LOCK_DOWN)) { print "SA_DATA_LOCK_DOWN key: $_ SA_DATA_LOCK_DOWN Value: $SA_DATA_L +OCK_DOWN{$_}<br>"; }

The print statement is all on one line. It prints this:

SA_DATA_LOCK_DOWN key: HASH(0x4024fe60) SA_DATA_LOCK_DOWN Value:

Only one line, and what is the HASH print? Can somebody help me with this.

Replies are listed 'Best First'.
Re: What causes HASH to print?
by holli (Abbot) on Feb 27, 2007 at 15:41 UTC
    Because you don't have a hash, or better you have a hash with a hashref as a key and no value assigned.
    Replace the curlies and make that
    %SA_DATA_LOCK_DOWN = ( 'SECALERT_ALERTS_ID'=>0, 'SECALERT_NUM'=>0, 'TECH_SPEC_ID'=>0, 'OS_APP_NAME'=>0, )


    holli, /regexed monk/
      If you have "use warnings;" in your code (or run with "perl -w"), you should get the warning:
      Reference found where even-sized list expected
      If you don't have "use warnings;", then put it in before asking a question. The warnings are there to help you, not annoy you.

      UPDATE: And, if the warning is a little obscure, which in this case it is if you haven't seen it before, you can also "use diagnostics;". For example, perl -Mdiagnostics -w pm602312.pl gives:

      Reference found where even-sized list expected at pm602312.pl line 2 (W misc) You gave a single reference where Perl was expecting a list with an even number of elements (for assignment to a hash). This usua +lly means that you used the anon hash constructor when you meant to use parens. In any case, a hash requires key/value pairs. %hash = { one => 1, two => 2, }; # WRONG %hash = [ qw/ an anon array / ]; # WRONG %hash = ( one => 1, two => 2, ); # right %hash = qw( one 1 two 2 ); # also fine
Re: What causes HASH to print?
by davorg (Chancellor) on Feb 27, 2007 at 16:33 UTC
    OK, this is a "what's wrong with my code" question, which is probably an inappropriate use of this site

    Not at all. It looks like you've made a effort to cut the problem down to the minimum amount of code. That's exactly the kind of problem that we're happy to help with.

    You've been given the correct answer by a few people, but I thought you might be interested in a couple of other clues that you could have got for yourself.

    If you had "use warnings" in your code, then you would have seen this error message:

    Reference found where even-sized list expected

    Which is true (as you now know from seeing what the problem is) but perhaps not as clear as it could have been. To get more help when tracking down a weird error like that, you can add "use diagnostics" to your code and Perl will then give you a complete description of the problem. In this case, it would have said the following:

    You gave a single reference where Perl was expecting a list with an even number of elements (for assignment to a hash). This usually means that you used the anon hash constructor when you meant to use parens. In any case, a hash requires key/value pairs.

    %hash = { one => 1, two => 2, }; # WRONG %hash = [ qw/ an anon array / ]; # WRONG %hash = ( one => 1, two => 2, ); # right %hash = qw( one 1 two 2 ); # also fine

    So, in this case, I think that Perl would have given you exactly the help you were looking for to solve your problem.

Re: What causes HASH to print?
by Paladin (Vicar) on Feb 27, 2007 at 15:41 UTC
    You create a hash with () not {}.
    %SA_DATA_LOCK_DOWN = ( 'SECALERT_ALERTS_ID'=>0, 'SECALERT_NUM'=>0, 'TECH_SPEC_ID'=>0, 'OS_APP_NAME'=>0, ... );
Re: What causes HASH to print?
by EvanK (Chaplain) on Feb 27, 2007 at 15:43 UTC
    Read the docs on the subject of references. whats happening is you're using curly braces, thus assigning a (scalar) anonymous hash. likely what you MEANT to do was assign an actual hash:
    # note the parentheses for a list, rather than curly braces for an ano +nymous structure %SA_DATA_LOCK_DOWN = ( 'SECALERT_ALERTS_ID'=>0, 'SECALERT_NUM'=>0, 'TECH_SPEC_ID'=>0, 'OS_APP_NAME'=>0, )
    Update: Fletch makes a good point. references and scalar/list context are both complex and difficult to fully explain, as I perpetually prove :o

    __________
    Systems development is like banging your head against a wall...
    It's usually very painful, but if you're persistent, you'll get through it.

      That's not assigning "an actual hash", that's assigning a LIST to a hash which is taken as a set of key/value pairs.</pedant>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found