From the documentation of Foreach Loops: "... the foreach loop index variable is an implicit alias for each item in the list that you're looping over." Meaning you can simply assign back to the $Date_Ref variable in order to assign to the element in the array. You can even modify the original variables by writing for my $Date_Ref ($Map_Request_Date, ...) { ... (Update: It seems choroba has made the same point while I was typing.)

In the following, I've taken the liberty of writing the code the way I might have (see also Use strict and warnings), although I prefer using modules like DateTime (plus DateTime::Format::Strptime) for all my date/time handling needs, even if it seems like overkill sometimes. (Update: Note that I kept your technique of adding a leading zero to $Day instead of switching to sprintf because I wanted to demonstrate using if as a statement modifier, but if I were writing this from scratch I'd have used sprintf instead.)

use warnings; use strict; use Data::Dumper; my %Months = ( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => + '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12' ); my ($Date1,$Date2) = ('Jul 3 2016 10:45 UTC','Aug 12 2017 11:56 EST') +; my @Variables = ($Date1,$Date2); for my $Date_Ref (@Variables) { my ($Month,$Day,$Year,$Time) = split ' ', $Date_Ref, 4; $Day = "0$Day" if length($Day) == 1; $Date_Ref = $Year."-".$Months{$Month}."-".$Day; } print Dumper \@Variables; __END__ $VAR1 = [ '2016-07-03', '2017-08-12' ];

Note: split ' ', ..., as opposed to split / /, ... has a special behavior, see split: "any leading whitespace in EXPR is removed before splitting occurs, and the PATTERN is instead treated as if it were /\s+/; in particular, this means that any contiguous whitespace (not just a single space character) is used as a separator.".


In reply to Re: Array of variables by haukex
in thread Array of variables by Michael W

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.