> 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
I assume you meant (note braces and quotes):
$categories{1094} = '100049-0220-100004'
otherwise the value will be 100049-0220-100004 = -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;
There are two ways I can think of: (1) you can copy and reverse the hash or
(2) you can loop through the values until you find a match.
------------------------------------------
# remember, hashes are not in key order
# note () NOT {}
%categories = ( 1094 => '100049-0220-100004',
1095 => '100059-5220-500004',
1096 => '100669-6620-660004',
1097 => '100779-7720-770004',
);
$value = '100779-7720-770004';
# (1)
%reversed = reverse %categories;
print "$reversed{$value}\n";
# (2)
foreach $key(keys %categories){
print "Loop ", ++$x, "\n";
if($categories{$key} eq $value){
print "$key\n";
print "Exiting within loop $x\n";
last;
}
}
------------------------------------------
> #until ($value == $ccid)
Data are text not numeric ($value eq $ccid). See above.
> #} ## Need to break out of the while loop here once find it.
last;
> So, here's the question:
> Where can I find information on how to use variables as the keys in a
> hash?
I hope this answers your question. If %categories is not that large OR if
you have a fast computer OR if you have a lotta memory, I would opt for (1)
above because there is no looping--ie, less code to maintain, that is always
good. If you no longer need the original %categories, you can also do
%categories = reverse %categories;
Otherwise, opt for (2).
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]