开发者

Perl defining a pattern for a variable to follow

开发者 https://www.devze.com 2023-03-21 22:46 出处:网络
If you define my $top = (0 .. 100) you g开发者_开发百科et each number 1,2,3,4...100 but how can I define $top to only produce 0, 2.5, 5, 7.5, ... 100? Thanks@top = map { 2.5 * $_ } 0 .. 40;

If you define my $top = (0 .. 100) you g开发者_开发百科et each number 1,2,3,4...100 but how can I define $top to only produce 0, 2.5, 5, 7.5, ... 100? Thanks


@top = map { 2.5 * $_ } 0 .. 40;


#!/usr/bin/perl -w

use strict;
use warnings;
use Data::Dumper;

my @numbers = ( 0 .. 40 );
my @top = map { $_ * 5 / 2 } @numbers;

print Dumper \@top;

Output:

$ ./test.pl 
$VAR1 = [
          '0',
          '2.5',
          '5',
          '7.5',
          ...
          '95',
          '97.5',
          '100'
        ];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号