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. Advertising

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

  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. Advertising

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. valentin tihomirov

    another array ranges mystery

    valentin tihomirov, Jun 18, 2005, in forum: VHDL
    Replies:
    2
    Views:
    478
    Mike Treseler
    Jun 18, 2005
  2. John K. Humkey

    sed regexp mystery

    John K. Humkey, Jul 8, 2003, in forum: Perl
    Replies:
    0
    Views:
    2,044
    John K. Humkey
    Jul 8, 2003
  3. bjh

    Mystery Repost

    bjh, Jan 13, 2004, in forum: Perl
    Replies:
    0
    Views:
    721
  4. rp
    Replies:
    1
    Views:
    517
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    615
    David A. Black
    Jul 2, 2008
Loading...

Share This Page