in reply to Re^2: Replace a value with another value from the same array
in thread Replace a value with another value from the same array

I think you are going to need two passes since you imply you cannot insure the order of the array.

  • Comment on Re^3: Replace a value with another value from the same array

Replies are listed 'Best First'.
Re^4: Replace a value with another value from the same array
by tobyink (Canon) on May 09, 2017 at 20:58 UTC

    No, it can be done in one pass quite easily — you might not even need to pass through the whole array once.

    my ($xxy, $yyk); for (@$data) { $xxy = $_ if $_->{code} eq 'XXY'; $yyk = $_ if $_->{code} eq 'YYK'; if ($xxy && $yyk) { $yyk->{expdate} = $xxy->{expdate}; last; } }

      It was not specified that there would only be one YYK entry, and if there were multiples and they all came before the XXY was found only the last found would be replaced, or only the first if any came after the XXY entry.

        If there are multiple YYK entries, my code still works. It'll just use the first one it finds. If you'd rather use the last YYK entry then just comment out:

        last;

        And you still only have a single pass.