hash mystery

Discussion in 'Perl Misc' started by monkeys paw, Jan 18, 2006.

  1. monkeys paw

    monkeys paw Guest

    For some reason i'm forced to use a scalar for
    ONE hash element and not the rest. Here is how the
    code behaves:

    This works:

    $qry = new CGI;

    foreach (@names) {
    if ($_ =~ /^newsummary(.*)$/ && $qry->param($_)) {
    # I have no clue why 'id' must be a scalar to work. [sf]
    my $id = $qry->param('id' . $1);
    %hash = (
    file_name => $qry->param('file_name' . $1),
    id => $id,
    comment_name => $qry->param('newsum_name' . $1),
    id_type => 'bill',
    commentary => $qry->param($_),
    );
    }
    }

    At his point the hash looks like:

    file_name => 'file',
    id => 'CA2005',
    comment_name => 'cname',
    id_type => 'bill',
    commentary => 'some comment',

    However, if i use $qry for 'id' the hash gets whacked like
    so:

    %hash = (
    file_name => $qry->param('file_name' . $1),
    id => $qry->param('id' . $1),
    comment_name => $qry->param('newsum_name' . $1),
    id_type => 'bill',
    commentary => $qry->param($_),
    );


    CA2005 => undef,
    file_name => 'file',
    id => 'CA2005',
    comment_name => 'cname',
    id_type => 'bill',
    commentary => 'some comment',
     
    monkeys paw, Jan 18, 2006
    #1
    1. Advertisements

  2. Gunnar Hjalmarsson, Jan 18, 2006
    #2
    1. Advertisements

  3. monkeys paw <> wrote:
    > For some reason i'm forced to use a scalar for
    > ONE hash element and not the rest. Here is how the
    > code behaves:


    > my $id = $qry->param('id' . $1);


    perhaps param('id' . $1) is multivalued?

    --
    Darren Dunham
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
     
    Darren Dunham, Jan 18, 2006
    #3
  4. monkeys paw

    Guest

    Darren Dunham wrote:
    > perhaps param('id' . $1) is multivalued?


    If the OP would like to investigate Darren's question, s/he may use the
    Dump function of CGI.pm, which is a good debugging tool. Simply put:

    print Dump();

    somewhere and it will show you all the param()eters, with their value
    and structure (the dump is HTML formatted for display in a browser).

    You will probably need to include Dump in your "use" statement, ie:

    use CGI qw/:standard Dump/;

    (note that "Dump" begins with an upper-case D).
     
    , Jan 18, 2006
    #4
  5. monkeys paw

    Anno Siegel Guest

    Darren Dunham <> wrote in comp.lang.perl.misc:
    > monkeys paw <> wrote:
    > > For some reason i'm forced to use a scalar for
    > > ONE hash element and not the rest. Here is how the
    > > code behaves:

    >
    > > my $id = $qry->param('id' . $1);

    >
    > perhaps param('id' . $1) is multivalued?


    That's certainly what it looks like. If so,

    %hash = (
    # ...
    id => scalar $qry->param('file_name' . $1),
    # ...
    );

    should solve it.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Jan 19, 2006
    #5
  6. monkeys paw

    Guest

    monkeys paw <> wrote:
    > For some reason i'm forced to use a scalar for
    > ONE hash element and not the rest. Here is how the
    > code behaves:
    >
    > This works:
    >
    > $qry = new CGI;
    >
    > foreach (@names) {
    > if ($_ =~ /^newsummary(.*)$/ && $qry->param($_)) {
    > # I have no clue why 'id' must be a scalar to work. [sf]
    > my $id = $qry->param('id' . $1);


    CGI's param can return a list if called in a list context. The hash
    assignment is a list context. If you call it directly in the hash
    assignment, and it happens to return a list of size other than 1, than your
    hash gets mis-aligned.

    If you want to avoid the temp variable you could put "scalar()" around the
    call to param.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jan 19, 2006
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Red Orchid
    Replies:
    3
    Views:
    1,460
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,346
    CBFalconer
    Aug 4, 2004
  3. Bo Peng
    Replies:
    4
    Views:
    1,004
  4. navS
    Replies:
    3
    Views:
    684
    Ismo Salonen
    May 9, 2008
  5. rp
    Replies:
    1
    Views:
    875
    red floyd
    Nov 10, 2011
  6. Gavin Kistner

    Hash like JS Hash (code)

    Gavin Kistner, Feb 14, 2004, in forum: Ruby
    Replies:
    8
    Views:
    476
    Gavin Kistner
    Feb 17, 2004
  7. Joel VanderWerf

    Hash#eql? and Hash key testing

    Joel VanderWerf, Dec 4, 2004, in forum: Ruby
    Replies:
    4
    Views:
    323
    Christoph
    Dec 4, 2004
  8. Srijayanth Sridhar
    Replies:
    19
    Views:
    1,017
    David A. Black
    Jul 2, 2008
Loading...