Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Simple enough; I need to repeat a date from an array of dates x number of times and list it to a text file, repeat the next date and list them under the first date, etc. So:
2014-01-01
2014-01-01
2014-01-01
2014-02-02
2014-02-02
2014-02-02
...repeat for more. Le Code:
#!/usr/bin/perl -w use Term::ANSIColor; use Date::Calc qw(Add_Delta_Days); use strict; use warnings; my $START_DATE = &prompt("Please enter the start date (format yyyy-mm- +dd):"); my $END_DATE = &prompt("Please enter the ending date (format yyyy-mm-d +d):"); open LABFILE, '>', "labels.txt"; print LABFILE join("\n", date_list($START_DATE, $END_DATE)); close LABFILE; sub date_list { my ($from, $to) = @_; my @dates = $from; my $intermediate = $from; while ($intermediate ne $to) { $intermediate = sprintf "%04d-%02d-%02d", Add +_Delta_Days(split(/-/, $intermediate), 1); push @dates, $intermediate; } my $first = shift(@dates); my $last = pop(@dates); my $dates = join("\n", @dates); my @multiple = $dates; my @mults = map { "$dates" } @multiple; foreach my $date(@mults) { #return $date; return (($date) x 10); } } sub prompt { my($prompt, $default) = @_; my $defaultValue = $default ? "[$default]" : ""; print color("yellow"), "$prompt $defaultValue", color ("reset"); chomp(my $input = <STDIN>); return $input ? $input : $default; }
...which if you use dates 2014-01-01 to start and 2014-01-10 to end, returning $date (commented out) by itself gives you:
2014-01-02
2014-01-03
..
2014-01-09
..and if you run the uncommented line I get:
02
..through..
09
repeated 10 times. I don't want to repeat the whole array, just repeat each element 10 times in succession. I am prostrate before your light, an empty vessel, yet wide open.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Generate list with array elements repeated x times
by toolic (Bishop) on Oct 16, 2014 at 18:23 UTC | |
by Anonymous Monk on Oct 16, 2014 at 20:55 UTC | |
by toolic (Bishop) on Oct 16, 2014 at 20:59 UTC | |
|
Re: Generate list with array elements repeated x times
by choroba (Cardinal) on Oct 16, 2014 at 18:12 UTC | |
by Anonymous Monk on Oct 16, 2014 at 20:47 UTC | |
by choroba (Cardinal) on Oct 16, 2014 at 21:07 UTC |