Angharad has asked for the wisdom of the Perl Monks concerning the following question:

I have a text file that I'm feeding into a perl script and need a look up table to check the text file and see whether a particular value is something 'new'. If 'new' the value is added to a hash table and a counter goes up by one.
if(!$data{$match}) + { $data{$match}++; $count = $count + 1; }
It works fine for the most part but i sometimes get an error:
Odd number of elements in hash assignment at ...
What does this mean and how might i take steps to correct it? Also, is it necessarily something 'bad' or can I - in an emergency - safely ignore it? Thanks

Replies are listed 'Best First'.
Re: Odd number of elements error
by shmem (Chancellor) on Jul 15, 2007 at 12:13 UTC
    Odd number of elements in hash assignment at ...

    at where? The code snippet you present cannot be the cause of the error shown 1). I can't get a clue of what's going wrong if you omit the line number of the error, don't provide the code surrounding that very line, but an entirely unrelated snippet. See How (Not) To Ask A Question.

    Are you doing some method call? Remember that the object (or class) is passed in as the first argument calling $obj->method(). Consider:

    use strict; use warnings; my $obj = bless do { \my $x }, __PACKAGE__; # current package, e.g. 'm +ain' print "wrong:\n"; $obj->wrong(name =>'John', age => 28); print "-" x 50, "\n"; print "right:\n"; $obj->right(name =>'Paul', age => 30); sub wrong { warn +(caller(0))[3],'(',join(',',map "'$_'", @_),")\n"; my %h = @_; print "$_ => $h{$_}\n" for keys %h; } sub right { warn +(caller(0))[3],'(',join(',',map "'$_'", @_),")\n"; my $self = shift; my %h = @_; print "$_ => $h{$_}\n" for keys %h; } __END__ wrong: main::wrong('main=SCALAR(0x8167870)','name','John','age','28') Odd number of elements in hash assignment at - line 17. Use of uninitialized value in concatenation (.) or string at - line 18 +. 28 => John => age main=SCALAR(0x8167870) => name -------------------------------------------------- right: main::right('main=SCALAR(0x8167870)','name','Paul','age','30') name => Paul age => 30

    As for this...

    if(!$data{$match}) { $data{$match}++; $count = $count + 1; }

    why don't you just $count++ ?

    1): There is no assignment to a hash in that snippet, but a post-increment of a hash value keyed with the content of the variable $match

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      why don't you just $count++ ?
      And put it along with the the checking as well...
      $data{$match}++ || $count++; # or, $count++ unless $data{$match}++;

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

        That's not equivalent; the original code only increments $data{$match} if it's false. But there are good odds that what you give is what the OP actually wanted.
Re: Odd number of elements error
by FunkyMonk (Bishop) on Jul 15, 2007 at 11:55 UTC
    A hash is initialised with a list of key-values pairs, so there must be an even number of items in the list. This error means you're trying to initialise a hash with a list that has an odd number of elements (and doesn't come from the snippet of code you posted).

    my %h1 = ( 1, 2 ); # OK - same as ( 1 => 2) my %h2 = ( 1 ); # Not OK - make no sense

    Is it important? I'd say it probably is. Something is happening in your data that you haven't anticipated. Go back to your data and find out what's causing the error.

Re: Odd number of elements error
by syphilis (Archbishop) on Jul 15, 2007 at 15:49 UTC
    Odd number of elements in hash assignment at ...

    Ever mindful of the ambiguity of "odd", I always laugh whenever I see this error. My mind drifts back to (circa) 1969 ... Deniliquin High School, Remedial Maths (Donald Firth, the presiding teacher):

    Mr. Firth: Here's the problem. We have 18 horses, and 5 stalls ... we have to house those 18 horses in the 5 stalls in such a way that there is at least one horse in each stall, and there is an odd number of horses in each stall.

    Young syphilis: Can't be done, Sir. We put one horse in each stall (to satisfy the first requirement) ... that leaves 13 horses to be accommodated. They have to be accommodated in multiples of 2 (to preserve the oddness). No matter how we do that there will always be an odd number of horses left over which, when assigned to a particular stall, will mean there's an even number of horses in that stall.

    Mr. Firth: Not so, young syphilis, we merely put one horse in each of the first 4 stalls and 14 horses in the 5th stall.

    Young syphilis: But Sir ... 14 is not an odd number !!

    Mr. Firth: Au contraire, young syphilis ... "14" is a very odd number of horses to be housed in the one stall !!

    Never argue with a Maths Teacher.

    Cheers,
    Rob
      Along similar lines:

      $ fortune -m "Alexander the Great"
      (science)
      %
      (1) Alexander the Great was a great general.
      (2) Great generals are forewarned.
      (3) Forewarned is forearmed.
      (4) Four is an even number.
      (5) Four is certainly an odd number of arms for a man to have.
      (6) The only number that is both even and odd is infinity.

      Therefore, Alexander the Great had an infinite number of arms.
      %

      Young syphilis, despite off-topic this is an excellent story :)
      Young syphilis, despite it's off-topic this is an excellent story :)
Re: Odd number of elements error
by GrandFather (Saint) on Jul 15, 2007 at 20:42 UTC

    Could it be you are doing something the equivalent of:

    use strict; use warnings; my $valuesLine = <DATA>; my @values = split /\s+/, $valuesLine; my %data = @values; __DATA__ one two three four five

    where instead you should initialize %data using something like:

    my %data = map {$_ => 0} @values;

    DWIM is Perl's answer to Gödel
Re: Odd number of elements error
by apl (Monsignor) on Jul 15, 2007 at 12:47 UTC
    I assume you're getting the error when you initialize $data. This has happened to me. Add one more definition with a key-value you'll never use (e.g. "____") and a value of undef.

    This doesn't explain the cause of the problem, and I regret not being able to help you there. But the work-around prevents the error.

    (I ++ you for asking the question, and look forward to wiser Monks than I answering it...)