[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] going backwards



On Mon, Jan 03, 2000 at 01:25:23PM -0800, Nicholas G. Thornton wrote:
> say we have an array like... 
>      @weekdays = (mon, tues, wed, thurs, fri, sat, sun); 
> ...and we do $weekdays[0] we get "mon", no problems. But is there a 
> simple way to go backwards from "mon" to get "0" without testing every 
> value in the array? The best I've come up with so far is to have a 
> hash... 
>      %weekdays = (mon => 0, tues => 1, wed => 2, thurs => 3, fri => 4, 
> sat => 5, sun => 6); 
> ... and do $weekdays{mon} to get "0". But for what I'm doing I'd like 
> not to have an inverse hash for all the arrays. 
> 

You want a way to do a hash lookup without having a hash...
Do you also tighten screws with a hammer?  :D

If you're simply concerned about defining the list in two places, do
something like this:

@weekdays = qw(mon tues wed thurs fri sat sun);
$i = 0;
%weekdays = map {$_, $i++} @weekdays;


The best way to get hash lookups is simply to use a hash.


Ronald

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org