in reply to Assign values by position to strings in two arrays
For example:
my @array1 = qw(a b c d e); my @array2 = qw(a c d e b); # assign numbers and create lookup table my $i; my %pos1 = map { $_ => ++$i } @array1; # lookup items of array2 my @pos2 = map $pos1{$_}, @array2; print "@pos2\n"; # 1 3 4 5 2
The hash %pos1 maps strings to numbers, i.e. you can lookup the number associated with for example "a" by writing $pos1{a}, which would give 1 here.
(If items can occur more than once in array1 (i.e. not unique), you'd have to define how to deal with that case...)
|
|---|