Trouble passing arguments to subroutine

Discussion in 'Perl Misc' started by laredotornado, Nov 5, 2010.

  1. Hi,

    I want to create a function that takes a hash (by reference) and a
    scalar as arguments. I'm trying ...

    sub add_to_block_hash
    {
    my (%blocks, $curBlock) = @_;
    print "curblock: $curBlock\n"; # line 33
    ....


    add_to_block_hash(\%blocks, $curBlock);


    but I'm getting the error, "Use of uninitialized value in
    concatenation (.) or string at test.pl line 33" in which line 33 is
    marked above. How do I correct this? THanks, - Dave
     
    laredotornado, Nov 5, 2010
    #1
    1. Advertising

  2. laredotornado

    J. Gleixner Guest

    laredotornado wrote:
    > Hi,
    >
    > I want to create a function that takes a hash (by reference) and a
    > scalar as arguments. I'm trying ...
    >
    > sub add_to_block_hash
    > {
    > my (%blocks, $curBlock) = @_;

    ^^^^^^^ hash, not a reference
    > print "curblock: $curBlock\n"; # line 33
    > ...
    >
    >
    > add_to_block_hash(\%blocks, $curBlock);

    ^^^^^^^ reference to a hash
    >
    >
    > but I'm getting the error, "Use of uninitialized value in
    > concatenation (.) or string at test.pl line 33" in which line 33 is
    > marked above. How do I correct this? THanks, - Dave


    sub add_to_block_hash {
    my ($blocks, $curBlock) = @_;

    Then, dereference $blocks.

    e.g.
    for my $k ( keys %$blocks ) { ...}
     
    J. Gleixner, Nov 5, 2010
    #2
    1. Advertising

  3. laredotornado <> wrote:
    >I want to create a function that takes a hash (by reference) and a
    >scalar as arguments. I'm trying ...
    >
    >sub add_to_block_hash
    >{
    > my (%blocks, $curBlock) = @_;

    ^^^^^^^
    This hash will suck up all the arguments provided, leaving nothing for
    $curBlock. And because you are passing an even number of arguments you
    are not even getting a warning that you are trying to create a hash with
    an odd number of elements.
    If you are passing a hash ref, then you need to treat it as hash ref:

    my ($blockRef, $curBlock) = @_;

    jue
     
    Jürgen Exner, Nov 6, 2010
    #3
    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. Scott
    Replies:
    10
    Views:
    214
    Tad McClellan
    Dec 7, 2004
  2. TheOrangeRemix
    Replies:
    4
    Views:
    103
    -berlin.de
    Jun 29, 2006
  3. Dean
    Replies:
    3
    Views:
    182
  4. king
    Replies:
    5
    Views:
    188
  5. oldyork90
    Replies:
    10
    Views:
    348
    Jorge
    Sep 27, 2008
Loading...

Share This Page