Perl圆阵列函数

时间:2015-09-28 08:18:27

标签: arrays perl

我正在寻找有关如何循环数组的帮助。我有一个源代码,它取一个半径为圆的数字并产生它的周长。这只需要一个数字,我想得到多个圆周。

#!/usr/bin/env perl
use warnings;

$pi = 3.141592654;
print "What's the radius? ";
chomp($radius = <STDIN>);
$circle = 2 * $pi * $radius;
if ($radius < 0 ) {
$circle = 0;
}
print "The circumference of a circle with the radius of $radius is $circle.\n";

现在我希望能够使用while循环为多个半径输入多个数字以将它们放入数组中。一旦我输入0或小于0的数字,while循环将退出,程序将继续循环通过用户输入的数字数组,并使用这些值作为半径计算每个圆的周长。

我有一些代码尝试这样做,但我似乎无法让它工作。

@number = <STDIN>;
while(<>) {
print "whats the radius? ";
my($circle, $radius, $pi);

$radius = &rad(<STDIN>);
$pi = 3.141592654;


$circle = 2 * $pi * $radius;
if ($radius <= 0){

    return $circle; }
}

任何方向,或简单的解决方案将不胜感激。

3 个答案:

答案 0 :(得分:2)

要使用数组执行此操作,您需要首先阅读,直到获得零和push。然后,您可以使用forforeach循环处理数组。这两个是相同的,但通常与foreach相关联的语法更为可靠。

use strict;
use warnings;

# turn on autoflush so stuff gets printed immediately
$|++;

print "Please enter a bunch of numbers, or 0 to stop. ";

my @numbers;
while ( my $n = <> ) {
    chomp $n;

    # stop at zero or less
    last if $n <= 0;

    # store the number in the array
    push @numbers, $n;
}

my $pi = 3.141592654;

foreach my $radius (@numbers) {
    my $circumference = 2 * $pi * $radius;

    # print with format (%s gets replaced by params)
    printf "The circumference of a circle with radius %s is %s\n", $radius, $circumference;
}

这是输入/输出。

Please enter a bunch of numbers, or 0 to stop. 1
2
3
0
The circumference of a circle with radius '1' is '6.283185308'
The circumference of a circle with radius '2' is '12.566370616'
The circumference of a circle with radius '3' is '18.849555924'

运行第一个程序时,我注意到半径是什么?消息显示在输入提示下。这是因为当输出中有换行符\n时(或者在某些其他情况下,让我们忽略它们),只会刷新输出缓冲区。要更改该行为,可以在缓冲区上启用autoflush,或者只需将$|设置为大于零的值。增量习惯用法很常见。

您还可以使用Math :: Trig模块获取π。它带有Perl并且有一个常量,可以为您提供pi。由于Perl中的常量只是subs,因此您可以像模块中的任何其他函数一样导入它。

use strict;
use warnings;
use Math::Trig 'pi'; # load module and import the 'pi' constant

# ...

foreach my $radius (@numbers) {
    my $circumference = 2 * pi * $radius;

    # print with format (%s gets replaced by params)
    printf "The circumference of a circle with radius %s is %s\n", $radius, $circumference;
}

答案 1 :(得分:0)

use strict;
use warnings;

use Data::Dumper;

这会在无限循环中重复您的代码,如果半径<= 0则退出。

它存储数组中圆和半径的所有相应值:

my (@circle, @radius);
while(1){
    print "What's the radius? ";
    chomp($radius = <STDIN>);
    $circle = 2 * $pi * $radius;
    print "The circumference of a circle with the radius of $radius is $circle.\n";
    last if $radius <= 0;
    push @circle, $circle;
    push @radius, $radius;
}

print Dumper \@circle, \@radius;

答案 2 :(得分:0)

比听起来更容易 - 与数组交互的关键方式的音调是通过for(或foreach - 它们是相同的)循环。循环的每次迭代,变量都设置为迭代器 - 您可以指定它(我在下面),但如果不这样做,它默认使用$_

#!/usr/bin/perl
use strict;
use warnings;

my $PI = 3.141592654;

print "Please enter radii, terminate with ^D\n"; 
my @list_of_values = <STDIN>;
chomp ( @list_of_values ); #remove linefeeds

foreach my $radius ( @list_of_values ) { 
    print "Circumference of radius $radius is: ", $PI * $radius * 2,"\n"; 
}

我上面使用的另一个技巧是阅读STDIN。 Perl知道将单个值 - 读入标量​​ - 或将一系列值读入数组之间的区别。

如果你这样做会有不同的表现:

 my $value = <STDIN>; #reads one line
 my @values = <STDIN>; #reads all the lines

你也可以这样做:

#!/usr/bin/perl
use strict;
use warnings;

my $PI = 3.141592654;
my @list_of_values;

print "Enter radii, or \"done\" if complete\n";
while ( my $input = <STDIN> ) {
    chomp($input);
    last if $input eq "done";
    if ( $input =~ m/^[\d\.]+$/ ) {
        push( @list_of_values, $input );
    }
    else {
        print "Input of \"$input\" is invalid, please try again\n";
    }
}

foreach my $radius (@list_of_values) {
    print "Circumference of radius $radius is: ", $PI * $radius * 2, "\n";
}

forforeach做同样的事情,这是你使用的风格问题。