# SPUG: spug: Odd number of elements in hash assignment at YYYY line XX

Andrew Sweger andrew at sweger.net
Wed Dec 19 20:19:11 CST 2001

```To answer the question specifically, because @array_x contains an odd
number of elements.

It will help to know what you are attempting to accomplish. When "casting"
an array into a hash, the alternating elements of the array become the
keys and values of the hash. For example,

@in = qw(Oz Squint Tux Grommit);
%hash = @in;

---

%hash = ( "Oz"  => "Squint",
"Tux" => "Grommit",  # These "associations" aren't meant
);                             # to make sense

Also, how are you intending to use the RE match here? The way I read this,
@array_x will be populated with the list of non-zero counts of matches (RE
in scalar context). I'm assuming that your RE is a non-capturing pattern
(no parens).

@in = qw(Oz Squint Tux Grommit);
@out = map /i/, @in;
%hash = @out;

---

@out = (1, 1);
%hash = (1 => 1);

Or...

my @in = qw(Oz Squint Tux Grommit);
my @out = map /t/i, @in;             # Note different pattern
%hash = @out;

---

@out = (1, 1, 1);
Odd number of elements in hash assignment...

On Wed, 19 Dec 2001, Nord, Chris wrote:

> Question on the below code.  I get a PERL message, "Odd number of elements
> in hash assignment at nlerg line XX".  The results of the map get populated
> as keys in %hash.  Why the "Odd number..." error?
>
>  @array_x = map m|some_pattern|, at array_y;
>  %hash = @array_x;
>
>  foreach (keys %hash){
>   print "\$_\n";
>  }
>
> Odd number of elements in hash assignment at nlerg line XX
> line XX is %hash = @array_x;
>
> Also tried \$hash{map m|some_pattern|, at array_y} = ''; but the results of the
> map do not generate a key list in the hash.

--
Andrew B. Sweger -- The great thing about multitasking is that several
things can go wrong at once.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL