Hi Monks!

I have to check if this value XXy in the hashref is true in the array, if it is
get the value of "expdate" and use it to update the hashref YYK "expdate" with it. Do a need a second loop, is that why $newdate has no value?

I am trying to get this at the end.

{ 'code' => 'YYK', 'expdate' => '2017-01-01', # <<< noticed that this value was found +in the hashref XXY 'date' => '2014-02-20', 'product' => 'buc', 'city' => 'MARS' }

My test case:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data = [ { 'code' => 'XXY', 'expdate' => '2017-01-01', 'date' => '2017-01-30', 'product' => 'cell', 'city' => 'NYK' }, { 'code' => 'YYK', 'expdate' => '2015-05-05', 'date' => '2014-02-20', 'product' => 'buc', 'city' => 'MARS' }, { 'code' => 'RX Issued', 'expdate' => '2019-11-09', 'date' => '2011-03-11', 'product' => 'idx', 'city' => 'BXO' }, ]; my $results = process( $data ); print "\n\n"; print Dumper $results; print "\n\n"; sub process { my $all = shift || return; my $setdate; my $newdate; foreach my $all_item (reverse @{ $all }) { # If XXY is found use the value in the "expdate" to replace the "e +xpdate" value in YYK # First check if XXY is true and store it if ( $all_item->{ code } eq 'XXY' ) { $newdate = $all_item->{expdate}; } if( $all_item->{ code } eq 'YYK') { # Now, set the new date $all_item->{expdate} = $newdate if $newdate; $setdate = $all_item; } } return $setdate; }

Thanks for looking!

In reply to Replace a value with another value from the same array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.