Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi monks, I wondered whether someone could shed some light on a problem i'm having?! I have two arrays of floating-point numbers
@numbers @data
I have some code that deciphers a desired value from @numbers which is stored in  $best. I now need to find the corresponding value (in the same position in the array to) to $best in the @data array. I have no idea how to do this! To make the problem clearer;
my @numbers = ('0.001','0.34','0.456','0.521'); my @data = ('70.1','75.3','76.5','87.4');
say that for example $best is 76.5, i need away of returning 0.456, without simply using $numbers2. Hope someone can help?! thanks

Replies are listed 'Best First'.
Re: relating arrays
by japhy (Canon) on Dec 05, 2002 at 15:03 UTC
    You want to keep track of the indices, not the actual element. If, for example, your "best" routine were simply the max-function:
    sub best { my $d = shift; my ($best_idx) = sort { $d->[$b] <=> $d->[$a] } 0 .. $#$d; return $best_idx; }
    Then you would do:
    my $idx = best(\@numbers); my $other = $data[$idx];

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: relating arrays
by LTjake (Prior) on Dec 05, 2002 at 15:12 UTC
    You should really keep track of the index, not the value. But, if you really need to, this thread has some ways (although, most of them are golfed) to get an array index based on the value. So, for example, using jmcnamara's lovely sub:
    use strict; my @numbers = ('0.001','0.34','0.456','0.521'); my @data = ('70.1','75.3','76.5','87.4'); print $numbers[array_index('76.5', @data)]; sub array_index { @_{@_}=-1..@_; $_{$_[0]} }
    Prints 0.456

    HTH

    --
    "I don't intend for this to take on a political tone. I'm just here for the drugs." --Nancy Reagan
Re: relating arrays
by diotalevi (Canon) on Dec 05, 2002 at 15:07 UTC

    Or just use a hash if the actual order isn't important.

    my %data = ('0.001' => '70.1', '0.34' => '75.3', '0.456' => '76.5', '0.521' => '87.4');
    __SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;
      Note that this will not work unless the elements of the first array ( keys in the hash ) are unique.
(jeffa) Re: relating arrays
by jeffa (Bishop) on Dec 05, 2002 at 15:09 UTC
    You need to store the index that $best was found and use that to find the corresponding index from your other array. One way to do this is to use a hash as a lookup table:
    my @numbers = ('0.001','0.34','0.456','0.521'); my @data = ('70.1','75.3','76.5','87.4'); my %data; @data{@data} = (0..$#data); my $best = $data{'76.5'}; print $numbers[$best], $/;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: relating arrays
by dvergin (Monsignor) on Dec 05, 2002 at 20:27 UTC
    Assuming (as we must -- else your problem has no clean solution) that the elements in @data are unique.

    Given:

    my @data = ('70.1', '75.3','76.5', '87.4'); my @numbers = ('0.001','0.34','0.456','0.521');
    ...then just do:
    my %look_up; @look_up{@data} = @numbers;
    ...and then you can retrieve the values as needed. E.g.: print $look_up{76.5}, "\n";   # prints: 0.456

    ------------------------------------------------------------
    "Perl is a mess and that's good because the
    problem space is also a mess.
    " - Larry Wall