Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The second one should be written with curly brackets not parenthesis.

$test = { test1 => '1', test2 => '2', test3 => '3'};

Otherwise you are trying to assign a list to a scalar. use warnings would have told you this:

Useless use of constants in void context at test.pl line 12.

This is referring to the members of the list that were discarded when it was assigned to a scalar. Everything but the last 3 was discarded.

Further use strict would have told you:

Can't use string ("3") as a HASH ref while "strict refs" in use at tes +t.pl line 19.

$test holds the number 3. When you try to dereference the number 3 (a la %{$test} it converts it to a string "3" and then looks up the hash called "%3" in the symbol table. strict doesn't allow such shenanigans.

The curly brackets in the correct example indicate an anonymous hash reference. References are scalars. So it assigns to $test just fine.

Don't study too hard though - the confusion between references-to-objects and the objects themselves will be going away in Perl 6. (or getting worse, depending how you look at it, :) )

-Andrew.


In reply to Re: Counting keys in a referenced hash by tomazos
in thread Counting keys in a referenced hash by logie17

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found