Perl defining a pattern for a variable to follow

陌路散爱 提交于 2020-01-06 15:24:09

问题


If you define my $top = (0 .. 100) you get each number 1,2,3,4...100 but how can I define $top to only produce 0, 2.5, 5, 7.5, ... 100? Thanks


回答1:


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



回答2:


#!/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'
        ];


来源:https://stackoverflow.com/questions/6783837/perl-defining-a-pattern-for-a-variable-to-follow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!