Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 根据每个元素中找到的第一个数字对数组进行排序_Perl_Sorting - Fatal编程技术网

Perl 根据每个元素中找到的第一个数字对数组进行排序

Perl 根据每个元素中找到的第一个数字对数组进行排序,perl,sorting,Perl,Sorting,我正在寻找帮助排序数组,其中每个元素由“一个数字、一个字符串、一个数字”组成。我想对数组元素的第一个数字部分进行排序,降序(以便我首先列出较高的数字),同时列出文本等 我仍然是一个初学者,所以下面的选择也是受欢迎的 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

我正在寻找帮助排序数组,其中每个元素由“一个数字、一个字符串、一个数字”组成。我想对数组元素的第一个数字部分进行排序,降序(以便我首先列出较高的数字),同时列出文本等

我仍然是一个初学者,所以下面的选择也是受欢迎的

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
}
使用严格;
使用警告;
my@arr=map{int(rand(49)+1)}(1..100);#构建一个由100个介于1和49之间的随机数组成的数组
我的@count2;
每个我的$i(1..49){
我的@count=join(“,”,@arr)=~m/$i,/g;#可能尝试只生成一次字符串,然后通过它进行搜索???
my$count1=标量(@count);#我希望这个$count1是在字符串/数组中找到每个数字($I)的次数。
push(@count2,$count1.“$i”的次数);#将“数字、文本和数字/标量、字符串、标量”推送到数组中。
}
#对于(@count2){print“$\n”;}
#尝试将第一个冒号中的所有数字相加,以确保它们==100
#排序@count2并打印前7名
@count2=排序{$b$a}@count2;#请尝试停止此文件的打印输出,或按=~m/^a成员/?排序???或仅在第一个或第二个\d
foreach我的$i(0..6){
打印$count2[$i]。“\n”#似乎排序正确
}

首先,将数据存储在数组中,而不是字符串中:

# inside the first loop, replace your line with the push() with this one:
push(@count2, [$count1, $i]; 
然后,您可以轻松地按每个子阵列的第一个元素进行排序:

my @sorted = sort { $b->[0] <=> $a->[0] } @count2;

另请参见:,

首先,将数据存储在数组中,而不是字符串中:

# inside the first loop, replace your line with the push() with this one:
push(@count2, [$count1, $i]; 
然后,您可以轻松地按每个子阵列的第一个元素进行排序:

my @sorted = sort { $b->[0] <=> $a->[0] } @count2;

另请参见:,

按原样考虑您的需求。最好不要在字符串中嵌入计数信息。不过,我会把它当作一个学习练习

注意,我使用散列进行计数,以节省内存和可能的速度

但是,可以通过使用

编辑:仅使用绘制的数字创建结果数组
#/usr/bin/perl
严格使用;使用警告;
my@arr=map{int(rand(49)+1)}(1..100);
我的百分比计算;
++@arr的$counts{$};
my@result=map sprintf(“%d次用于%d”,$counts{$\u},$\ uz),
排序{$counts{$a}$counts{$b}}键%counts;
为@result打印“$\un”;
然而,我可能会这样做:

#!/usr/bin/perl

use strict; use warnings;
use YAML;

my @arr;
$#arr = 99; #initialize @arr capacity to 100 elements 

my %counts;

for my $i (0 .. 99) {
    my $n = int(rand(49) + 1); # pick a number
    $arr[ $i ] = $n;           # store it
    ++$counts{ $n };           # update count
}

# sort keys according to counts, keys of %counts has only the numbers drawn
# for each number drawn, create an anonymous array ref where the first element
# is the number drawn, and the second element is the number of times it was drawn
# and put it in the @result array

my @result = map  [$_, $counts{$_}],
             sort {$counts{$a} <=> $counts{$b} }
             keys %counts;

print Dump \@result;
#/usr/bin/perl
严格使用;使用警告;
使用YAML;
我的@arr;
$#arr=99#将@arr容量初始化为100个元素
我的百分比计算;
对于我的$i(0..99){
我的$n=int(兰特(49)+1)#选一个数字
$arr[$i]=$n;#存储它
++$counts{$n};#更新计数
}
#根据计数对键进行排序,%计数的键只绘制数字
#对于绘制的每个数字,创建一个匿名数组ref,其中第一个元素
#是绘制的数字,第二个元素是绘制的次数
#并将其放入@result数组中
my@result=map[$\$counts{$\}],
排序{$counts{$a}$counts{$b}
键的百分比计数;
打印转储\@结果;

按原样满足您的需求。最好不要在字符串中嵌入计数信息。不过,我会把它当作一个学习练习

注意,我使用散列进行计数,以节省内存和可能的速度

但是,可以通过使用

编辑:仅使用绘制的数字创建结果数组
#/usr/bin/perl
严格使用;使用警告;
my@arr=map{int(rand(49)+1)}(1..100);
我的百分比计算;
++@arr的$counts{$};
my@result=map sprintf(“%d次用于%d”,$counts{$\u},$\ uz),
排序{$counts{$a}$counts{$b}}键%counts;
为@result打印“$\un”;
然而,我可能会这样做:

#!/usr/bin/perl

use strict; use warnings;
use YAML;

my @arr;
$#arr = 99; #initialize @arr capacity to 100 elements 

my %counts;

for my $i (0 .. 99) {
    my $n = int(rand(49) + 1); # pick a number
    $arr[ $i ] = $n;           # store it
    ++$counts{ $n };           # update count
}

# sort keys according to counts, keys of %counts has only the numbers drawn
# for each number drawn, create an anonymous array ref where the first element
# is the number drawn, and the second element is the number of times it was drawn
# and put it in the @result array

my @result = map  [$_, $counts{$_}],
             sort {$counts{$a} <=> $counts{$b} }
             keys %counts;

print Dump \@result;
#/usr/bin/perl
严格使用;使用警告;
使用YAML;
我的@arr;
$#arr=99#将@arr容量初始化为100个元素
我的百分比计算;
对于我的$i(0..99){
我的$n=int(兰特(49)+1)#选一个数字
$arr[$i]=$n;#存储它
++$counts{$n};#更新计数
}
#根据计数对键进行排序,%计数的键只绘制数字
#对于绘制的每个数字,创建一个匿名数组ref,其中第一个元素
#是绘制的数字,第二个元素是绘制的次数
#并将其放入@result数组中
my@result=map[$\$counts{$\}],
排序{$counts{$a}$counts{$b}
键的百分比计数;
打印转储\@结果;

祝贺您作为初学者使用了
使用严格
使用警告
!请澄清你到底想做什么。你写的是排序,代码里有一些关于计数的东西。如果你最终想数数你不需要排序的数字。。。但是,请提供您想要计算的字符串的示例。计数/总和对我来说只是一个注释halo,我还没有看到它。。。我可能会在这里问,所以如果我不能解决它。。。感谢初学者使用
使用严格
使用警告
!请澄清你到底想做什么。你写的是排序,代码里有一些关于计数的东西。如果你最终想数数你不需要排序的数字。。。但是,请提供您想要计算的字符串的示例。计数/总和对我来说只是一个注释halo,我还没有看到它。。。我可能会在这里问,所以如果我不能解决它。。。感谢汉克斯·莫里茨,似乎正在打印出来。但有一个输入错误是:\npush(@count2,[$count1,$i])???谢谢moritz,好像要打印出来了。但有一个输入错误是:\npush(@count2,[$count1,$i])???感谢西南大学,这一个看起来很难为我现在,但我会努力的。我在它的输出中也得到了一些重复的数字…啊,我忘了创建一个辅助数组。我会的。谢谢西南大学,这个看起来对我来说很难,但我会努力的。我在它的输出中也得到了一些重复的数字…啊,我忘了创建一个辅助a