[英]Perl program for sorting an array not working
I am new to Perl programming.... I wrote a simple script for sorting of numbers in an array but the script is not working as expected. 我是Perl编程的新手。...我编写了一个简单的脚本,用于对数组中的数字进行排序,但是该脚本无法正常工作。 Please help me with this.
请帮我解决一下这个。
#!/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");
I get: 我得到:
Enter the numbers...... Please enter a blank space at the end
2
4
3
2 4 3
2 4 3
http://codepad.org/LtVxt8zG 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
The perl debugger is very useful for seeing why your sort algorithm is not working. Perl调试器对于查看为什么排序算法不起作用非常有用。 I started your program in the debugger and entered the following:
我在调试器中启动了程序,然后输入以下内容:
a 10 print "count -> $count ; y -> $y ; array -> @array\n"
The output is: 输出为:
2 4 3
count -> 1 ; y -> 1 ; array -> 2 4 3
count -> 2 ; y -> 1 ; array -> 2 4 3
2 4 3
$y
never gets past 1. The 4 and the 3 are never compared. $y
永远不会超过1。永远不会比较4和3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.