There is no such thing as an "Array of variables". This

my @Variables = ( $Map_Request_Date,$Map_Due_Date,$Map_Cutover_Date,$M +ap_Complete_Date,$Map_Approved_Date);

does not do what you think it does. You are assigning the contents of those variables to a new array, not the variables themselves. In the loop you are modifying the values in the array, not the original variables:

my($foo, $bar) = (1,2); my @vars = ($foo, $bar); foreach my $var (@vars) { $var += 4; } print "\@vars: @vars\n"; print "\$foo: $foo\n"; print "\$bar: $bar\n"; __END__ @vars: 5 6 $foo: 1 $bar: 2

Is that what you want to do? Or do you want to modify the values in the variables? In that case, you have two possibilities:

  1. use the list of variables as argument to the foreach loop:
    my($foo, $bar) = (1,2); foreach my $var ($foo, $bar) { $var += 4; } print "\$foo: $foo\n"; print "\$bar: $bar\n"; __END__ $foo: 5 $bar: 6
    The loop variable $var is an alias for each item in the list. The same happens if you iterate over an array: the loop variable is then an alias to each slot in the array.
     
  2. take references to the original variables:
    my($foo, $bar) = (1,2); my @vars = \($foo, $bar); # note the backslash before the list # this would do the same: my @vars = (\$foo, \$bar); foreach my $var (@vars) { $$var += 4; # note the de-referencing $ before $var } print "\@vars: @vars\n"; print "\$foo: $foo\n"; print "\$bar: $bar\n"; __END__ @vars: SCALAR(0x15ae7a0) SCALAR(0x15ae830) $foo: 5 $bar: 6
    In this case, the @vars array is populated with references to the original variables, which are values. To modify what they point at, they must be de-referenced. Doing so gives access to the body of the variable of which the reference has been taken.

Almost always the first way is used.

Then, your %Months hash only holds constants, but you are setting it up and tearing it down everytime through the loop. Move that outside the loop. You can limit the scope of that hash by wrapping all into a bare block:

{ my %Months = (...); ... foreach $var (...) { ... } } # %Months not defined here anymore

Lastly, it is easier to reformat your date strings using split and sprintf:

my %Months = (Dec => 12); my $date = 'Dec 1 2016 18:15'; my @pcs = split " ", $date; $date = sprintf "%02d %02d %d %s",$Months{$pcs[0]}, @pcs[1..3]; print "date: '$date'\n"; __END__ date: '12 01 2016 18:15'

The construct @pcs[1..3] is an array slice of @pcs from index 1 to 3.

update: add/fix links

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: Array of variables by shmem
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.