Perl: the Markov chain saw PerlMonks

### Re: Decimal Array range

by bart (Canon)
 on Dec 21, 2010 at 13:02 UTC ( #878245=note: print w/replies, xml ) Need Help??

in reply to Decimal Array range

Perl's .. operator can do magic increments, but only for strings in the format /^[a-z]*[0-9]*\$/i. But the problem you face here is that your increments must be by 1; not 1/100, not 5 minutes or 1 hour or whatever.

You'll have to somehow produce a range where increments are one, and scale them.

Plenty of people showed you how to do that with increments of 1/100, and here's a way to increment using time (in this exampel: increment in 5 minute intervals):

```use POSIX qw(strftime);
use Time::Local;
my(\$from, \$to, \$incr) = ('13:00', '23:00', '0:05');
foreach(\$from, \$to, \$incr) {
if(/^(\d+):(\d\d)(?::(\d\d))?\$/) {
\$_ = (\$1 * 60  + \$2) * 60 + (\$3 || 0);
}
}
@range = map { strftime "%H:%M:%S", gmtime \$_ }
map \$_*\$incr, \$from/\$incr .. \$to/\$incr;

\$_ = join " ", @range;
s/(.{1,70})(\s|\$)/\$1\n/g;
print ;
Output:
```13:00:00 13:05:00 13:10:00 13:15:00 13:20:00 13:25:00 13:30:00
13:35:00 13:40:00 13:45:00 13:50:00 13:55:00 14:00:00 14:05:00
14:10:00 14:15:00 14:20:00 14:25:00 14:30:00 14:35:00 14:40:00
14:45:00 14:50:00 14:55:00 15:00:00 15:05:00 15:10:00 15:15:00
15:20:00 15:25:00 15:30:00 15:35:00 15:40:00 15:45:00 15:50:00
15:55:00 16:00:00 16:05:00 16:10:00 16:15:00 16:20:00 16:25:00
16:30:00 16:35:00 16:40:00 16:45:00 16:50:00 16:55:00 17:00:00
17:05:00 17:10:00 17:15:00 17:20:00 17:25:00 17:30:00 17:35:00
17:40:00 17:45:00 17:50:00 17:55:00 18:00:00 18:05:00 18:10:00
18:15:00 18:20:00 18:25:00 18:30:00 18:35:00 18:40:00 18:45:00
18:50:00 18:55:00 19:00:00 19:05:00 19:10:00 19:15:00 19:20:00
19:25:00 19:30:00 19:35:00 19:40:00 19:45:00 19:50:00 19:55:00
20:00:00 20:05:00 20:10:00 20:15:00 20:20:00 20:25:00 20:30:00
20:35:00 20:40:00 20:45:00 20:50:00 20:55:00 21:00:00 21:05:00
21:10:00 21:15:00 21:20:00 21:25:00 21:30:00 21:35:00 21:40:00
21:45:00 21:50:00 21:55:00 22:00:00 22:05:00 22:10:00 22:15:00
22:20:00 22:25:00 22:30:00 22:35:00 22:40:00 22:45:00 22:50:00
22:55:00 23:00:00

Tweaking the format spec for strftime you can produce a different time format.

Replies are listed 'Best First'.
Re^2: Decimal Array range
by Gulliver (Monk) on Dec 21, 2010 at 20:50 UTC

I didn't realize you could assign to \$_ inside a foreach loop and it will change the scalars in the list.

In foreach, the loop variable (default \$_) is an alias to the original value. That means that changing the value will change the original.
```my \$x = 100;
my @y = (10, 20, 30);
foreach (\$x, @y) {
\$_ *= 2;
}
local \$" = ", ";
print "\\$x = \$x\n\@y = (@y)\n";
```# or:
foreach my \$n (\$x, @y) {
\$n *= 2;
}

Ditto in the block or expression for map and grep (\$_ only).

```map { \$_ *= 2 } \$x, @y;
```grep { \$_ *= 2 } \$x, @y;

BTW parameters to subs are aliases too, so if you change them in @_, you'll modify the originals too:

```sub double {
foreach(@_) {
\$_ *= 2;
}
}

double(\$x, @y);

Create A New User
Node Status?
node history
Node Type: note [id://878245]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-02-17 21:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (249 votes). Check out past polls.

Notices?