Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
First of all, my congratulations for discovering Data::Dumper, you will never go anywhere without it ;)

This is a common problem with CGI's param method. It's being called in list context in this case. If the CGI form did not include a value for rs$i, then param("rs$i") in list context will return an empty list. The empty list just gets flattened away in a hash construction:

{ number => 1, name => "test", id => "test", rollSizzle => (), roll => undef, sizzle => undef } ## is the same as { number => 1, name => "test", id => "test", rollSizzle => "roll", undef => "sizzle", undef }
This explains the output you're getting. You can fix it by forcing scalar context:
{ number => $number, name => scalar param("n$i"), id => scalar param("id$i"), rollSizzle => scalar param("rs$i"), roll => $temp, sizzle => $temp };
At some point, this problem has bitten everyone who's ever done CGI programming with CGI.pm, and I wish there were an option to pass to CGI.pm to force scalar context behavior for all calls to param. I rarely use forms where multiple fields are named the same, so param's list context is rarely useful for me...

Now that I think of it, I will probably start changing the tops of my CGI scripts from now on:

# use CGI 'param'; use CGI; sub param { scalar CGI::param(@_) }

blokhead


In reply to Re: Problem assigning values to Hash by blokhead
in thread Problem assigning values to Hash by Steny

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 goofing around in the Monastery: (5)
As of 2024-04-23 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found