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

I cannot help but feel that the following code ought to work.
my %messages = ( 'both' => 'Please enter both a Screen Name and a password', 'badchars' => 'Your Screen Name and password must contain onl +y letters, numbers, -, or _', 'notfound' => 'The Screen Name or password is incorrect, plea +se try again', 'emailnf' => 'Sorry! Our system doesn\'t recognize that emai +l address. Please enter the same email address you used to register +with the site', 'emailf' => 'Your Screen Name and password are on their way t +o you!', 'noemail' => 'You forgot to enter your email address', 'beenDel' => $beenDelErrMsg ); while (($k=$v) = each (%messages)) { print "$k=$v\n"; } foreach $i (values (%messages)) { print $i,"\n"; }
But the output it produces is this:
C:\MYPERL>perl stoopid.pl badchars= emailnf= noemail= notfound= both= emailf= beenDel= Your Screen Name and password must contain only letters, numbers, -, o +r _ Sorry! Our system doesn't recognize that email address. Please enter + the same email address you used to register with the site You forgot to enter your email address The Screen Name or password is incorrect, please try again Please enter both a Screen Name and a password Your Screen Name and password are on their way to you!
It is as if the values are not being associated with the keys, but the syntax looks right on to me. Any thoughts?

Replies are listed 'Best First'.
Re: Weird hash behavior
by kilinrax (Deacon) on Oct 11, 2000 at 17:14 UTC

    Try changing the equals sign to a comma in this line:

    while (($k,$v) = each (%messages))

    Then it should work.

    The each function returns a 2-element list in a list context, but just the key otherwise.
    What you were doing there was assinging $v to $k (when $v was undefined), and then assinging the key to $k.
    As a result, $v was never assigned a value.

      D'oh! Stupid mistake. Thanks for catching it.