Lara J. Fabans wrote:
> Hi, perl-friends,
>
> Here's the setup:
> I have set up a hash %categories where the key is one number and the
> value is another
> i.e. $categories('1094') = 100049-0220-100004
You need quotation marks here, like this:
$categories('1094') = '100049-0220-100004';
otherwise you're setting the hash value to -99!
>
> So, further on down, I'm trying to do a lookup in %categories to see
> if a value is there. (I can
> swap the key and the value in the setup if it will make this next step
> easier....)
> and I had a while loop to search through each of the keys & values,
> but the problem is
> that it doesn't break out of the while loop, and the wrong value gets
> set as $myID;
>
> $ccid = 1094;
>
> ## Attempt #2
> ### 1.22.03 change to try to get it to stop once it finds
> ### the correct value.
> #until ($value == $ccid)
> # {
> # ($key, $value) = each %categories;
> # }
>
> ## Attempt #1
> #while(my ($key, $value) = each %categories )
> #{
> #print "CC ID: $ccid: Key : $key : Value: $value\n";
> # if ($value == $ccid)
> # {
> # $myID = $key;
> #} ## Need to break out of the while loop here once find it.
> #}
>
> print "My ID is $categories{$ccid}\n"; ## Returns a null
Hi Lara.
I think you're misunderstanding hashes. They work this way round:
my (%hash, $key, $value);
$key = 1094;
$value = '100049-0220-100004'
$hash{$key} = $value;
so it looks like you're searching the hash for an entry
where $value == 1094, which doesn't exist. Also, this is
exactly what hashes were meant for so you don't need the
loop to find an entry with the given key. Just write:
$value = $hash{$ccid};
and it's done.
I hope I've understood what you're trying to do.
Cheers,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]