gpt4 book ai didi

linux - 用于对数组进行排序的 Perl 程序不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 04:18:28 24 4
gpt4 key购买 nike

我是 Perl 编程新手......我编写了一个简单的脚本来对数组中的数字进行排序,但该脚本未按预期工作。请帮我解决这个问题。

#!/usr/bin/perl
print ("Enter the numbers...... Please enter a blank space at the end\n");
$input = <STDIN>;
chop ($input);
$a = 1;
until ( $input == "" )
{
@array[$a-1] = $input ;
$a++;
$input = <STDIN>;
chop ($input);
}
print ("@array\n");
$count = 1;
$y = 1;
while ( $count < @array )
{
if ( $array[$y-1] > $array[$y] )
{
@array[$y-1,$y] = @array[$y,$y-1];
$y++;
}
$count++;
}
print ("@array\n");

我得到:

Enter the numbers...... Please enter a blank space at the end
2
4
3

2 4 3
2 4 3

最佳答案

http://codepad.org/LtVxt8zG

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

$"=',';
my @input;
while(<DATA>)
{
s/\s+$//;
push @input, $_;
}
print "Original Input: @input\n";

@input = sort { $a <=> $b } @input;
print "Sorted Input: @input\n";

__END__
3
1
2

关于linux - 用于对数组进行排序的 Perl 程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14907155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com