logo
down
shadow

Perl Sort Array


Perl Sort Array

Content Index :

Perl Sort Array
Tag : perl , By : Mariocki
Date : November 24 2020, 05:44 AM


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Using Perl, how can I sort an array using the value of a number inside each array element?


Tag : regex , By : Josh
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?


Tag : arrays , By : Ian
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


Tag : perl , By : Mark W
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


Tag : arrays , By : cjdavis
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;
Related Posts Related QUESTIONS :
  • Importing Conditionally Compiled Functions From a Perl Module
  • Why is object destructor not called when script terminates?
  • Archive tar files to a different location in Perl
  • Write asynchronously to file in perl
  • What is Perl's secret of getting small code do so much?
  • How to store output of module avail command in perl?
  • Odd "Use of uninitialized value", regular expression error
  • Perl append a substring with in a string
  • How do I collect coverage from child processes when running `cover -test`, and not otherwise? (Devel::Cover)
  • I need help regarding perl hash of array
  • Is there a faster way to get floating point bits than pack/unpack in Perl?
  • Conditions in Perl loops and performance
  • Packing an array causes "Can't locate method" error in perl
  • How do I do a case sensitive rename or move operation in perl?
  • how to convert column into row in perl
  • Comparison of Math::BigInt with scalars in Perl
  • perl mismatched tag, problem = XML::Simple module?
  • verify login+password in Perl via SAML
  • i want to search for a string in a file using perl
  • While running an email program on Windows ActivePerl I'm getting an error
  • How to read a excel file using perl?
  • Which http headers to suppress "This document is no longer available." on browser back button?
  • Is there any way to check whether a file is an empty file using PERL program?
  • Perl Date::Manip detects timezone wrong for EST and EDT
  • Why does the below Perl code show "Use of uninitialized value in concatenation (.) or string" when I try to us
  • How to print part of header and string in another file in Perl
  • Query about how to unpack a packed binary data
  • Can't open perl script "Build.PL": No such file or directory in response to perl Build.PL
  • Is there a way to host a webServer in perl which should in HTTPS
  • How do I concisely check warning categories up to the root of the hierarchy in my package?
  • Use tr with arrays
  • I'm facing problem with the email program in Perl
  • How to install Filter::Crypto module for perl under ubuntu
  • Count tandem repeats in Perl
  • Graph.pm - how to get all paths with particular length?
  • how get the last directory using perl?
  • Perl parsing email body without parts using MIME::Parser
  • Why Perl ignores spaces between a sigil and variable name?
  • Using Perl to replace empty string with space
  • Displaying 12-h time in Perl using DateTime
  • Assign temp file from cgi and read it in .sh
  • why doesn't make -C change $PWD as seen through a scripting language such as Perl?
  • Using Win32::Env perl module, environment variable not setting permanently in windows
  • How to fetch a one table from HTML source file using lwp module?
  • How to process variable dimension array in Perl
  • perl print to a file and STDOUT which is the file
  • Get array in perl from json_encode
  • How to get unicode code points in perl v5.24?
  • using an array as a reference is deprecated in perl
  • Using Perl's File::Basename to rename a file extension
  • Unable to get page via HTTPS with LWP::Simple in Perl
  • Perl DBI case insensitive methods
  • How to resolve @INC related issue in Perl
  • How to do unary and/reduction of bus to a bit in Perl?
  • What does perl -pe "s|$PASSWORD|<PASSWORD>|g"?
  • I want to replace last / by ,
  • Strange regular expression
  • What does "!" mean before a variable in perl
  • Getting the inner hash in Perl
  • perl : can't require $value from other perl file and use it in 3rd perl file
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com