我正在尝试: 用数字 1 到 10 填充数组的 10 个元素。通过循环数组中包含的值来添加数组中包含的所有数字。
例如,它会从 1 开始,然后第二个数字是 3(1 加 2),然后下一个数字是 6(现有的 3 加新的 3)这是我当前的代码
#!/usr/bin/perl
use warnings;
use strict;
my @b = (1..10);
for(@b){
$_ = $_ *$_ ;
}
print ("The total is: @b\n")
Run Code Online (Sandbox Code Playgroud)
这就是结果
The total is: 1 4 9 16 25 36 49 64 81 100
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是:
The total is: 1 3 6 10 etc..
Run Code Online (Sandbox Code Playgroud)