1 2 3 4 5
my @sorted = sort { my ($chr_a, $num_a) = $a =~ m/^([a-z]+)(\d+)/; my ($chr_b, $num_b) = $b =~ m/^([a-z]+)(\d+)/; $chr_a cmp $chr_b || $num_a <=> $num_b } @array;
1 2 3 4 5 6 7
my @sorted = map { $_->[2] } sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } map { my ($chr, $num) =~ m/^([a-z]+)(\d+)/; [$chr, $num, $_] } @array;
2012-11-23T16:25:57 pqdu kannst den string im sort aufsplitten:
Code (perl): (dl )1 2 3 4 5my @sorted = sort { my ($chr_a, $num_a) = $a =~ m/^([a-z]+)(\d+)/; my ($chr_b, $num_b) = $b =~ m/^([a-z]+)(\d+)/; $chr_a cmp $chr_b || $num_a <=> $num_b } @array;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#! /usr/bin/perl use strict; use warnings; # use Getopt::Long; # use CGI; my @list = qw( chr10 chr10 chr10 chr10 chrY chrY chrY chr1 chr1 chr1 chrX chrX chrX chrX chr2 chr2 chr2 ); my @sorted = sort { my ( $strA, $numA ) = $a =~ m/(\D+)(\d+)?/; my ( $strB, $numB ) = $b =~ m/(\D+)(\d+)?/; if ( defined $numA && defined $numB ) { $strA cmp $strB || $numA <=> $numB } else { $strA cmp $strB } } @list; print join "\n", @sorted; __END__
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
chr1
chr1
chr1
chr2
chr2
chr2
chr10
chr10
chr10
chr10
chrX
chrX
chrX
chrX
chrY
chrY
chrY
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#! /usr/bin/perl use strict; #use warnings; my @list = qw( chr10 chr10 chr10 chr10 chrY chrX chrY chr1 chr1 chr1 chrX chrY chrX chrX chr2 chr2 chr2 ); my @sorted = sort { my ($chr_a, $num_a) = $a =~ m/^([a-z]+)(\d+)/; my ($chr_b, $num_b) = $b =~ m/^([a-z]+)(\d+)/; $chr_a cmp $chr_b || $num_a <=> $num_b } @list; print join "\n", @sorted;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
chrY
chrX
chrY
chrX
chrY
chrX
chrX
chr1
chr1
chr1
chr2
chr2
chr2
chr10
chr10
chr10
chr10
my ($chr_a, $num_a) = $a =~ m/^([a-z]+)(\d+)/;
perl -e 'my $a = "fdsgdf"; my($b,$c) = $a =~ m/(fd)(4)/;print "$b $c\n"'
2012-11-29T11:56:06 LinuxerCode (perl): (dl )1 2 3 4 5my @sorted = sort { my ($chr_a, $num_a) = $a =~ m/^([a-z]+)(\d+)/; my ($chr_b, $num_b) = $b =~ m/^([a-z]+)(\d+)/; $chr_a cmp $chr_b || $num_a <=> $num_b } @list;
1 2 3 4 5
my @sorted = sort { my ($chr_a, $num_a) = $a =~ m/^([a-z]+)(\d*)/i; my ($chr_b, $num_b) = $b =~ m/^([a-z]+)(\d*)/i; $chr_a cmp $chr_b || $num_a <=> $num_b } @list;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
chr1
chr1
chr1
chr2
chr2
chr2
chr10
chr10
chr10
chr10
chrX
chrX
chrX
chrX
chrY
chrY
chrY
2012-11-30T10:16:32 Student87Warum das Hütchen ^ am Anfang der match-expression? Damit quasi nur nach Spalte 1 gesortet wird? Ist das quasi der schnelle Weg, Spalte 1 auszuwählen? Oder gibts noch nen anderen Grund für das Hütchen?
^([a-z]+)(\d+)
([a-z]+)(\d+)