Using Perl, how can I sort an array using the value of a number inside each array element?
Date : March 29 2020, 07:55 AM
should help you out Looks like you need a Schwartzian Transform: #!/usr/bin/perl
use strict;
use warnings;
my @a = <DATA>;
print
map { $_->[1] } #get the original value back
sort { $a->[0] <=> $b->[0] } #sort arrayrefs numerically on the sort value
map { /sj\.(.*?)p/; [$1, $_] } #build arrayref of the sort value and orig
@a;
__DATA__
12 16 sj.1012804p1012831.93.gz
12 16 sj.1012832p1012859.94.gz
12 16 sj.1012860p1012887.95.gz
12 16 sj.1012888p1012915.96.gz
12 16 sj.1012916p1012943.97.gz
12 16 sj.875352p875407.01.gz
12 16 sj.875408p875435.02.gz
12 16 sj.875436p875535.03.gz
12 16 sj.875536p875575.04.gz
12 16 sj.875576p875603.05.gz
12 16 sj.875604p875631.06.gz
12 16 sj.875632p875659.07.gz
12 16 sj.875660p875687.08.gz
12 16 sj.875688p875715.09.gz
12 16 sj.875716p875743.10.gz
|
How do I sort an array in Perl if not all of the elements in the array are defined?
Date : March 29 2020, 07:55 AM
like below fixes the issue How do I sort an array if not all of the elements in the array are defined? use strict;
use warnings;
use autodie;
use feature qw(say);
my @names;
$names[0] = "andrei";
$names[1] = "serghei";
$names[7] = "valerii";
$names[10] = "alexandr";
@names = sort grep { defined } @names;
for my $name ( @names ) {
say $name;
}
#! /usr/bin/env perl
#
use strict;
use warnings;
use autodie;
use feature qw(say);
my @names;
$names[0] = "andrei";
$names[1] = "serghei";
$names[7] = "valerii";
$names[10] = "alexandr";
no warnings qw(uninitialized);
@names = sort @names;
use warnings qw(uninitialized);
for my $name ( @names ) {
say $name if defined $name;;
}
|
Sort an array in perl
Date : March 29 2020, 07:55 AM
I wish did fix the issue. This is another use case where the nsort function from the CPAN module Sort::Naturally works as humans expect it: @a =(mem_64x128,mem_8x64,mem_8x128,mem_128x64); # no strict!
use Sort::Naturally qw(nsort);
print join(',', nsort @a), "\n";
mem_8x64,mem_8x128,mem_64x128,mem_128x64
|
How to Sort 2D array Perl?
Tag : perl , By : greggerz
Date : March 29 2020, 07:55 AM
should help you out May be I am missing something. First It seemed too easy to me.I thought I can easily achieve it using map{}sort{}map{} ,but now it became complicated to me. , Yes, you can use it, use strict;
use warnings;
my @array =( [qw(b e d)], [qw(s a f)], [qw(g i h)] );
my @sorted_array =
map { $_->[0] }
sort {
$a->[1] cmp $b->[1]
}
map {
my $r = [ sort @$_ ];
[$r, "@$r"];
}
@array;
use Data::Dumper;
print Dumper \@sorted_array;
$VAR1 = [
[
'a',
'f',
's'
],
[
'b',
'd',
'e'
],
[
'g',
'h',
'i'
]
];
|
Sort a row of a 2D array in perl
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , OK, the thing you need to know is - that an array of arrays in perl, is implemented as an array of array references. So - $array[$i] - is a reference to an array. @{$array[$i]} = sort @{$array[$i]};
@{$array[$i]} = sort { $a <=> $b } @{$array[$i]};
@$_ = sort {$a<=>$b} @$_ for @array;
|