在 Perl 中,如何使用第一个冒号 (:) 之前出现的数字对记录进行排序?

发布于 2024-12-15 17:42:37 字数 304 浏览 1 评论 0原文

我在 Perl 中有以下数据序列:

143:0.0209090909090909 
270:0.0909090909090909 
32:0.0779090909090909 
326:0.3009090909090909

请问,如何根据冒号之前的数字对它们进行排序,以将其作为我的输出?

32:0.0779090909090909
143:0.0209090909090909
270:0.0909090909090909  
326:0.3009090909090909

I have the following sequence of data in Perl:

143:0.0209090909090909 
270:0.0909090909090909 
32:0.0779090909090909 
326:0.3009090909090909

Please, how can I sort them based on the numbers before the colon, to get this as my output?

32:0.0779090909090909
143:0.0209090909090909
270:0.0909090909090909  
326:0.3009090909090909

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

落在眉间の轻吻 2024-12-22 17:42:37

那里有冒号并不重要。

Perl 将字符串转换为数字的规则
只会做正确的事:

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

my @nums = qw(
    143:0.0209090909090909 
    270:0.0909090909090909 
    32:0.0779090909090909 
    326:0.3009090909090909
);

{ no warnings 'numeric';
    @nums = sort {$a <=> $b} @nums;
}

print "$_\n" for @nums;

It does not matter that there are colons there.

Perl's rules for converting strings to numbers
will just do The Right Thing:

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

my @nums = qw(
    143:0.0209090909090909 
    270:0.0909090909090909 
    32:0.0779090909090909 
    326:0.3009090909090909
);

{ no warnings 'numeric';
    @nums = sort {$a <=> $b} @nums;
}

print "$_\n" for @nums;
悲凉≈ 2024-12-22 17:42:37

可以使用内置的 sort 函数:

程序

#!/usr/bin/env perl

use strict;
use warnings;

my @data = qw(
  143:0.0209090909090909
  270:0.0909090909090909
  32:0.0779090909090909
  326:0.3009090909090909
);

my $match = qr/^(\d+):/;
@data = sort { ( $a =~ $match )[0] <=> ( $b =~ $match )[0] } @data;

print join( "\n", @data ), "\n";

输出

32:0.0779090909090909
143:0.0209090909090909
270:0.0909090909090909
326:0.3009090909090909

The built-in sort function can be used:

Program

#!/usr/bin/env perl

use strict;
use warnings;

my @data = qw(
  143:0.0209090909090909
  270:0.0909090909090909
  32:0.0779090909090909
  326:0.3009090909090909
);

my $match = qr/^(\d+):/;
@data = sort { ( $a =~ $match )[0] <=> ( $b =~ $match )[0] } @data;

print join( "\n", @data ), "\n";

Output

32:0.0779090909090909
143:0.0209090909090909
270:0.0909090909090909
326:0.3009090909090909
蘸点软妹酱 2024-12-22 17:42:37

类似于:

my @sorted = sort { my ($a1) = split(/:/,$a);
                    my ($b1) = split(/:/,$b);
                    $a1 <=> $b1 } @data ;

$a1 和 $b1 将是每个排序输入的第一个元素,在冒号字符上分割。

Something along the lines of:

my @sorted = sort { my ($a1) = split(/:/,$a);
                    my ($b1) = split(/:/,$b);
                    $a1 <=> $b1 } @data ;

$a1 and $b1 will be the first element of each of the sorting inputs, split on the colon character.

玩心态 2024-12-22 17:42:37

我会简单地使用

sort -n < input.txt

否则:

use strict;
use warnings;

my @lines = (<>); 
print for sort { 
    my @aa = split(/:/, $a); 
    my @bb = split(/:/, $b); 
    1*$aa[0] <=> 1*$bb[0] 
} @lines;

I'd simply use

sort -n < input.txt

Otherwise:

use strict;
use warnings;

my @lines = (<>); 
print for sort { 
    my @aa = split(/:/, $a); 
    my @bb = split(/:/, $b); 
    1*$aa[0] <=> 1*$bb[0] 
} @lines;
江湖彼岸 2024-12-22 17:42:37

什么,还没有施瓦茨变换吗?

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my @data = qw(143:0.0209090909090909
            270:0.0909090909090909 
            32:0.0779090909090909 
            326:0.3009090909090909);

my @sorted = map $_->[0], sort {$a->[1] <=> $b->[1]} map {[$_, m/^(.+):/]} @data;

print Dumper \@sorted;

输出:

$VAR1 = [
          '32:0.0779090909090909',
          '143:0.0209090909090909',
          '270:0.0909090909090909',
          '326:0.3009090909090909'
        ];

What, no Schwhartzian transform yet?

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my @data = qw(143:0.0209090909090909
            270:0.0909090909090909 
            32:0.0779090909090909 
            326:0.3009090909090909);

my @sorted = map $_->[0], sort {$a->[1] <=> $b->[1]} map {[$_, m/^(.+):/]} @data;

print Dumper \@sorted;

Output:

$VAR1 = [
          '32:0.0779090909090909',
          '143:0.0209090909090909',
          '270:0.0909090909090909',
          '326:0.3009090909090909'
        ];
£噩梦荏苒 2024-12-22 17:42:37

考虑到多样性,我认为一些基准可能是合适的。请注意,在相信这些数字之前,请仔细检查基准测试代码:我匆忙编写了脚本。

#!/usr/bin/env perl

use 5.012;
use strict;
use warnings;

use Benchmark qw( cmpthese );

use constant DATA_SIZE => 1000;

cmpthese( -1, {
    right_thing   => sub { do_the_right_thing ( make_data(rt => DATA_SIZE) ) },
    re_extract    => sub { re_extract         ( make_data(re => DATA_SIZE) ) },
    split_extract => sub { split_extract      ( make_data(se => DATA_SIZE) ) },
    schxfrom_re   => sub { schxform_re        ( make_data(sx => DATA_SIZE) ) },
    nop           => sub { nop                ( make_data(nl => DATA_SIZE) ) },
});

sub do_the_right_thing {
    my ($DATA) = @_;
    no warnings 'numeric';
    [ sort { $a <=> $b } @$DATA ];
}

sub re_extract {
    my ($DATA) = @_;
    my $re = qr/^([0-9]+):/;
    [ sort { ($a =~ $re)[0] <=> ($b =~ $re)[0] } @$DATA ];
}

sub split_extract {
    my ($DATA) = @_;
    [
        sort {
            my ($x, $y) = map split(/:/, $_, 2), $a, $b;
            $x <=> $y
        } @$DATA
    ];
}

sub schxform_re {
    my ($DATA) = @_;
    [
        map    $_->[0],
        sort { $a->[1] <=> $b->[1] }
        map  { [ $_, m/^([0-9]+):/ ] } @$DATA
    ];
}

sub nop {
    my ($DATA) = @_;
    [ @$DATA ];
}

sub make_data {
    state %cache;
    my ($k, $n) = @_;

    unless (exists $cache{$k}) {
        $cache{ $k } =  [
            map
            sprintf('%d:%f', int(rand 10_000), rand),
            1 .. $n
        ];
    }

    return $cache{ $k };
}

结果

                Rate re_extract schxfrom_re split_extract right_thing        nop
re_extract    32.1/s         --        -85%          -92%        -98%       -99%
schxfrom_re    213/s       565%          --          -46%        -87%       -94%
split_extract  392/s      1121%         84%            --        -76%       -89%
right_thing   1614/s      4933%        657%          312%          --       -53%
nop           3459/s     10685%       1522%          783%        114%         --

Given the variety, I figured some benchmarks might be appropriate. Note, please double-check the benchmarking code before trusting these numbers: I whipped the script up in a hurry.

#!/usr/bin/env perl

use 5.012;
use strict;
use warnings;

use Benchmark qw( cmpthese );

use constant DATA_SIZE => 1000;

cmpthese( -1, {
    right_thing   => sub { do_the_right_thing ( make_data(rt => DATA_SIZE) ) },
    re_extract    => sub { re_extract         ( make_data(re => DATA_SIZE) ) },
    split_extract => sub { split_extract      ( make_data(se => DATA_SIZE) ) },
    schxfrom_re   => sub { schxform_re        ( make_data(sx => DATA_SIZE) ) },
    nop           => sub { nop                ( make_data(nl => DATA_SIZE) ) },
});

sub do_the_right_thing {
    my ($DATA) = @_;
    no warnings 'numeric';
    [ sort { $a <=> $b } @$DATA ];
}

sub re_extract {
    my ($DATA) = @_;
    my $re = qr/^([0-9]+):/;
    [ sort { ($a =~ $re)[0] <=> ($b =~ $re)[0] } @$DATA ];
}

sub split_extract {
    my ($DATA) = @_;
    [
        sort {
            my ($x, $y) = map split(/:/, $_, 2), $a, $b;
            $x <=> $y
        } @$DATA
    ];
}

sub schxform_re {
    my ($DATA) = @_;
    [
        map    $_->[0],
        sort { $a->[1] <=> $b->[1] }
        map  { [ $_, m/^([0-9]+):/ ] } @$DATA
    ];
}

sub nop {
    my ($DATA) = @_;
    [ @$DATA ];
}

sub make_data {
    state %cache;
    my ($k, $n) = @_;

    unless (exists $cache{$k}) {
        $cache{ $k } =  [
            map
            sprintf('%d:%f', int(rand 10_000), rand),
            1 .. $n
        ];
    }

    return $cache{ $k };
}

Results

                Rate re_extract schxfrom_re split_extract right_thing        nop
re_extract    32.1/s         --        -85%          -92%        -98%       -99%
schxfrom_re    213/s       565%          --          -46%        -87%       -94%
split_extract  392/s      1121%         84%            --        -76%       -89%
right_thing   1614/s      4933%        657%          312%          --       -53%
nop           3459/s     10685%       1522%          783%        114%         --
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文