in reply to min and mindex
Given your one-to-one mapping between the two arrays (i.e., the same number of elements) and the numbers in @array1 are not repeated, consider the following:
use strict; use warnings; use List::Util 'min'; my %hash; my @array1 = ( 5, 2, 3, 4, 1, 6, 7 ); my @array2 = ( "a", "b", "c", "d", "e", "f", "g" ); my %array1Hash = map { $array1[$_] => $_ } 0 .. $#array1; @hash{@array1} = @array2; my $array1Min = min @array1; print 'The minimum value in @array1 is ', $array1Min, ".\n"; print 'The position of ', $array1Min, ' in @array1 is ', $array1Hash{$array1Min}, ".\n"; print 'The element in @array2 at position ', $array1Min, ' is ', $hash{ min @array1 }, '.';
Output:
The minimum value in @array1 is 1. The position of 1 in @array1 is 4. The element in @array2 at position 1 is e.
Two hashes are created. The first (%array1Hash) pairs each element of @array1 with its position in the array. The second (%hash) pairs the elements of @array1 as the keys with the elements of @array2 as values. When you've found the minimum value in @array1, you can use it as the key to get the associated value from @array2.
Hope this helps!
Update: Added printing the min val of @array1 and that element's position in the array.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: min and mindex
by Anonymous Monk on Nov 06, 2012 at 18:15 UTC | |
by Kenosis (Priest) on Nov 06, 2012 at 19:07 UTC |