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

Re: [MacPerl] going backwards



On 03 Jan 2000 13:25:23 PST, Nicholas G. Thornton wrote:

>    @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.

Too bad. Too big?

Anyway, all I wanted to say now, is that you can automatically invert
the indices. Correctly, and away with the redundancy.

    my %weekdays;
    @weekdays{@weekdays}  = (0 .. $#weekdays);

If that's a bit too obscure for your taste, you can also do:

   %weekdays = map { $weekdays[$_] => $_ } 0 .. $#weekdays;

-- 
	Bart.

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