Hi Monks!
Question of a better way of doing this.
I need to build dynamically, new file names based on the year-month format, but noticing that the first date has to be the last month of the previous
year or “file_name_201212”, and the last date needs to be the current year till November or “file_name_201311” .
My code works but I am not sure if I am over doing it or if there would be a better and more straight forward way to accomplish the same thing more efficiently.
The results expected is this (also from the code I am posting ).
file_name_201212
file_name_201301
file_name_201302
file_name_201303
file_name_201304
file_name_201305
file_name_201306
file_name_201307
file_name_201308
file_name_201309
file_name_201310
file_name_201311
Here is my code:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw/strftime/;
my $this_year = strftime('%Y',localtime);
my $dates = build_file_names();
foreach my $new_file_names (@$dates) {
print " $new_file_names\n";
}
sub build_file_names {
my $last_year = $this_year-1;
my $stop_date = $this_year."12";
my @db_date;
for my $year ($this_year..$this_year) {
for my $month (1..12) {
my $db_date = sprintf "%d%02d", $year,$month;
last if ($db_date eq "$stop_date");
push @db_date, "file_name_".$db_date;
}
}
unshift (@db_date, "file_name_".$last_year."12");
return (\@db_date);
} # End sub build_file_names
Thanks for looking!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.