【发布时间】:2011-10-21 11:40:25
【问题描述】:
我正在寻求帮助对数组进行排序,其中每个元素都由“一个数字,然后是字符串,然后是数字”组成。我想对数组元素的第一个数字部分进行排序,降序(以便我首先列出较高的数字),同时还列出文本等。
我还是个初学者,所以也欢迎以下替代品
use strict;
use warnings;
my @arr = map {int( rand(49) + 1) } ( 1..100 ); # build an array of 100 random numbers between 1 and 49
my @count2;
foreach my $i (1..49) {
my @count = join(',', @arr) =~ m/$i,/g; # maybe try to make a string only once then search trough it... ???
my $count1 = scalar(@count); # I want this $count1 to be the number of times each of the numbers($i) was found within the string/array.
push(@count2, $count1 ." times for ". $i); # pushing a "number then text and a number / scalar, string, scalar" to an array.
}
#for (@count2) {print "$_\n";}
# try to add up all numbers in the first coloum to make sure they == 100
#sort @count2 and print the top 7
@count2 = sort {$b <=> $a} @count2; # try to stop printout of this, or sort on =~ m/^anumber/ ??? or just on the first one or two \d
foreach my $i (0..6) {
print $count2[$i] ."\n"; # seems to be sorted right anyway
}
【问题讨论】:
-
恭喜您作为初学者使用
use strict和use warnings! -
请说明您真正想要做什么。你写了关于排序的文章,在代码中有一些关于计数的东西。如果您最终想要计算不需要排序的数字......但是请提供您要计算的字符串的示例。
-
计数/总和对我来说只是一个评论晕,我还没有看到它...如果我不能解决它可能会在这里问...谢谢跨度>