You are not understanding how hashes work. Your code creates a hash, but assigns the value 'ALEX' to the key 'ABBIGAIL'. When you move ALEX earlier in the hash, you also put him in the 'odd' posiiton, so ALEX bacame a key, assigned the value 'ABBE',
Data::Dumper is very useful in this regaerd:
use Data::Dumper; my %WORD = ('AALIYAH', 'AARON', 'ABBE' , 'ABBEY', 'ABBI', 'ABBIE', 'ABBIGAIL', 'A +LEX'); print "FIRST HASH:\n"; print Dumper(\%WORD); my %WORD = ('AALIYAH', 'AARON', 'ALEX', 'ABBE', 'ABBEY', 'ABBI', 'ABBI +E', 'ABBIGAIL'); print "SECOND HASH:\n"; print Dumper(\%WORD);
produces:
FIRST HASH: $VAR1 = { 'ABBI' => 'ABBIE', 'ABBIGAIL' => 'ALEX', 'AALIYAH' => 'AARON', 'ABBE' => 'ABBEY' }; SECOND HASH: $VAR1 = { 'ALEX' => 'ABBE', 'AALIYAH' => 'AARON', 'ABBEY' => 'ABBI', 'ABBIE' => 'ABBIGAIL' };
Also, note that the keys to the hash are not in alphabetical order. They are in an order picked by perl, which you cannot count on.
If you need to operate on has keys sorted alphabeitcally, you need to sort them yourself, for example:
for ( sort keys %WORD ) { print "$_: $WORD{$_}\n" }
some or all code untested.
--Bob Niederman, http://bob-n.comIn reply to Re: Exists in HASH issue
by bobn
in thread Exists in HASH issue
by wsee
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |