Database Perl中名称的排序列表

Database Perl中名称的排序列表,database,perl,list,sorting,numbers,Database,Perl,List,Sorting,Numbers,我正在尝试学习Perl,我对Perl非常陌生,可能不到一周 我想对名称列表(在本例中是水果)进行排序并给它们一个ID。我的脚本目前给它们ID,但我也想对它们进行排序 当前代码: use strict; use warnings; my %unique; open(my $inFile, $ARGV[0]) || die "Could not open file '$ARGV[0]' $!"; open(my $outFile, ">$ARGV[1]") || die "Could no

我正在尝试学习Perl,我对Perl非常陌生,可能不到一周

我想对名称列表(在本例中是水果)进行排序并给它们一个ID。我的脚本目前给它们ID,但我也想对它们进行排序

当前代码:

use strict;
use warnings;

my %unique;

open(my $inFile, $ARGV[0]) || die "Could not open file '$ARGV[0]' $!";
open(my $outFile, ">$ARGV[1]") || die "Could not find file '>$ARGV[1]' $!";

while (<$inFile>) {
        my @fields = split;

        my $fruit  = $fields[0];
        my $quantity   = $fields[1];

        my @parts = split(/[_.]/, $fruit);
        my $counter = ++$unique{$parts[1]}{$parts[2]};

        print $outFile join("\t", $fruit, $quantity, "$fruit.$counter"), "\n";
}
Apple   3
Apple   50
Apple   1
Orange  51
Orange  21
Apple   3   Apple.1
Apple   50  Apple.2
Apple   1   Apple.3
Orange  51  Orange.1
Orange  21  Orange.2
Apple   1   Apple.1
Apple   3   Apple.2
Apple   50  Apple.3
Orange  21  Orange.1
Orange  51  Orange.2
Apple   3   1
Apple   50  2
Apple   1   3
Orange  51  3
Orange  21  5
[
    [0] "Apple    1    3",
    [1] "Apple    3    1",
    [2] "Apple    50   2",
    [3] "Orange   21   5",
    [4] "Orange   51   3"
] 
电流输出:

use strict;
use warnings;

my %unique;

open(my $inFile, $ARGV[0]) || die "Could not open file '$ARGV[0]' $!";
open(my $outFile, ">$ARGV[1]") || die "Could not find file '>$ARGV[1]' $!";

while (<$inFile>) {
        my @fields = split;

        my $fruit  = $fields[0];
        my $quantity   = $fields[1];

        my @parts = split(/[_.]/, $fruit);
        my $counter = ++$unique{$parts[1]}{$parts[2]};

        print $outFile join("\t", $fruit, $quantity, "$fruit.$counter"), "\n";
}
Apple   3
Apple   50
Apple   1
Orange  51
Orange  21
Apple   3   Apple.1
Apple   50  Apple.2
Apple   1   Apple.3
Orange  51  Orange.1
Orange  21  Orange.2
Apple   1   Apple.1
Apple   3   Apple.2
Apple   50  Apple.3
Orange  21  Orange.1
Orange  51  Orange.2
Apple   3   1
Apple   50  2
Apple   1   3
Orange  51  3
Orange  21  5
[
    [0] "Apple    1    3",
    [1] "Apple    3    1",
    [2] "Apple    50   2",
    [3] "Orange   21   5",
    [4] "Orange   51   3"
] 
想要的输出:

use strict;
use warnings;

my %unique;

open(my $inFile, $ARGV[0]) || die "Could not open file '$ARGV[0]' $!";
open(my $outFile, ">$ARGV[1]") || die "Could not find file '>$ARGV[1]' $!";

while (<$inFile>) {
        my @fields = split;

        my $fruit  = $fields[0];
        my $quantity   = $fields[1];

        my @parts = split(/[_.]/, $fruit);
        my $counter = ++$unique{$parts[1]}{$parts[2]};

        print $outFile join("\t", $fruit, $quantity, "$fruit.$counter"), "\n";
}
Apple   3
Apple   50
Apple   1
Orange  51
Orange  21
Apple   3   Apple.1
Apple   50  Apple.2
Apple   1   Apple.3
Orange  51  Orange.1
Orange  21  Orange.2
Apple   1   Apple.1
Apple   3   Apple.2
Apple   50  Apple.3
Orange  21  Orange.1
Orange  51  Orange.2
Apple   3   1
Apple   50  2
Apple   1   3
Orange  51  3
Orange  21  5
[
    [0] "Apple    1    3",
    [1] "Apple    3    1",
    [2] "Apple    50   2",
    [3] "Orange   21   5",
    [4] "Orange   51   3"
] 

Apple   3   Apple.2
Apple   50  Apple.3
Apple   1   Apple.1
Orange  51  Orange.2
Orange  21  Orange.1
谢谢

更新:

use strict;
use warnings;

my %unique;

open(my $inFile, $ARGV[0]) || die "Could not open file '$ARGV[0]' $!";
open(my $outFile, ">$ARGV[1]") || die "Could not find file '>$ARGV[1]' $!";

while (<$inFile>) {
        my @fields = split;

        my $fruit  = $fields[0];
        my $quantity   = $fields[1];

        my @parts = split(/[_.]/, $fruit);
        my $counter = ++$unique{$parts[1]}{$parts[2]};

        print $outFile join("\t", $fruit, $quantity, "$fruit.$counter"), "\n";
}
Apple   3
Apple   50
Apple   1
Orange  51
Orange  21
Apple   3   Apple.1
Apple   50  Apple.2
Apple   1   Apple.3
Orange  51  Orange.1
Orange  21  Orange.2
Apple   1   Apple.1
Apple   3   Apple.2
Apple   50  Apple.3
Orange  21  Orange.1
Orange  51  Orange.2
Apple   3   1
Apple   50  2
Apple   1   3
Orange  51  3
Orange  21  5
[
    [0] "Apple    1    3",
    [1] "Apple    3    1",
    [2] "Apple    50   2",
    [3] "Orange   21   5",
    [4] "Orange   51   3"
] 
新输入:

use strict;
use warnings;

my %unique;

open(my $inFile, $ARGV[0]) || die "Could not open file '$ARGV[0]' $!";
open(my $outFile, ">$ARGV[1]") || die "Could not find file '>$ARGV[1]' $!";

while (<$inFile>) {
        my @fields = split;

        my $fruit  = $fields[0];
        my $quantity   = $fields[1];

        my @parts = split(/[_.]/, $fruit);
        my $counter = ++$unique{$parts[1]}{$parts[2]};

        print $outFile join("\t", $fruit, $quantity, "$fruit.$counter"), "\n";
}
Apple   3
Apple   50
Apple   1
Orange  51
Orange  21
Apple   3   Apple.1
Apple   50  Apple.2
Apple   1   Apple.3
Orange  51  Orange.1
Orange  21  Orange.2
Apple   1   Apple.1
Apple   3   Apple.2
Apple   50  Apple.3
Orange  21  Orange.1
Orange  51  Orange.2
Apple   3   1
Apple   50  2
Apple   1   3
Orange  51  3
Orange  21  5
[
    [0] "Apple    1    3",
    [1] "Apple    3    1",
    [2] "Apple    50   2",
    [3] "Orange   21   5",
    [4] "Orange   51   3"
] 
想要的输出

Apple   1   3   Apple.1
Apple   3   1   Apple.2
Apple   50  2   Apple.3
Orange  21  5   Orange.1
Orange  51  3   Orange.2
#读入数据
我的@数据;
而(){
咀嚼;
推送@data,[split(/\t/,$\u,-1)];
}
#分类
@数据=排序{
$a->[0]cmp$b->[0]#按姓名
||
$a->[1]$b->[1]#按数量计
}@数据;
#生成ID并输出数据。
我的百分比计算;
对于我的$行(@data){
my$id=join('.',$row->[0],++$counts{$row->[0]});
推送@$row,$id;
打印(连接(“\t”,@$row),“\n”);
}

这里有一个解决方案,它使用了几个CPAN模块(和)来娱乐

试试这个

# Read in the data
my @data;
while (<>) {
   chomp;
   push @data, [ split(/\t/, $_, -1) ];
}

# Sort it
@data = sort {
   $a->[0] cmp $b->[0]   # By name
      ||
   $a->[1] <=> $b->[1]   # By quantity
} @data;

# Generate the ids and output the data.
my %counts;
for my $row (@data) {
   my $id = join('.', $row->[0], ++$counts{ $row->[0] });
   push @$row, $id;
   print(join("\t", @$row), "\n");
}
#读入数据
我的@数据;
而(){
咀嚼;
推送@data,[split(/\t/,$\u,-1)];
}
#分类
@数据=排序{
$a->[0]cmp$b->[0]#按姓名
||
$a->[1]$b->[1]#按数量计
}@数据;
#生成ID并输出数据。
我的百分比计算;
对于我的$行(@data){
my$id=join('.',$row->[0],++$counts{$row->[0]});
推送@$row,$id;
打印(连接(“\t”,@$row),“\n”);
}

我正在尝试对包含更多列的列表进行排序,似乎我只能对数量和ID进行排序。我还想更改添加列的值。如果您能在这方面帮助我或给我一些提示,我将非常感谢您。您是否向数组中添加了额外的字段?更新。是的,我有,如果我想按第3列而不是第2列对列表进行排序,我是否需要在数据子例程中添加另一个运算符?如果你想让它按不同的顺序排序,你必须更改控制顺序的回调(排序后的卷曲)。好的,非常感谢,我现在明白它的工作原理了!:)