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

Parr, Ryan Ryan.Parr at wwireless.com
Wed Dec 19 19:57:47 CST 2001


Assigning an array to a hash doesn't create a list of keys, but rather it
populates the hash.

For example:

@array = qw(one uno two dos three tres four quatro);
%hash = @array;

foreach (keys %hash) {
	print "$_: $hash{$_}\n";
}

Will print:
one: uno
two: dos
three: tres
four: quatro

However, I've personally never gotten the error you mention, and tried
creating a hash out of an array of 5 elements, and just ended up with one
key without a value, as would be expected. Are you using any external
Warnings modules of any type?

Also, with map you don't need to seperate your arguments with a comma unless
the first argument is a string. In other words:

@array = map { m/some_pattern/ } @array_y
-- and --
@array = map "This is $_\n", @array_y

are valid.

-- Ryan

-----Original Message-----
From: Nord, Chris [mailto:chris.nord at attws.com]
Sent: Wednesday, December 19, 2001 5:39 PM
To: 'spug list'
Subject: SPUG: spug: Odd number of elements in hash assignment at YYYY
line XX


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;l

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.

Chris Nord

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/





More information about the spug-list mailing list