[tpm] grabbing 2 input params at a time?

Henry Baragar Henry.Baragar at instantiated.ca
Wed Apr 16 19:25:00 PDT 2008


On Wednesday, April 16 2008 07:12 pm, Fulko Hew wrote:
> Sorry, my brain has gone on vacation...
>
> Whats the easiest way to grab two parameters at a time, that were passed
> into a subroutine?
>
> ie.
>
> mysub('a', 1, 'b', 2, 'c', 3);
>
> sub mysub {
>    while (@_) { push @letters, shift; push @nums, shift; }
> }

If order does not matter (and letters are unique):

sub mysub {
    my %map = @_;
    my @letters = keys %map;
    my @nums = values %map;
}

If order does matter:

sub mysub {
   my @letter_indices = map {2 * $_} 0..$#_/2;
   my @number_indices = map {$_ + 1} @letter_indices;
   my @letter = @_[@letter_indices];
   my @number = @_[@number_indices];
}

This is longer, but the intention is more clear (IMHO).

Henry


More information about the toronto-pm mailing list