in reply to Re: sorting according to greek alphabet in roman letters
in thread sorting according to greek alphabet in roman letters
Woke up this morning thinking it could be much simpler. This time I do use a numeric sort:
#!/usr/bin/perl -w use strict; my @unordered = qw( 2HB 3HB C CA CB CG CD1 CD2 CE1 CZ CE2 HE2 HE1 HH HD1 HD2 N O OH ); # change weighting my %main = ( N => 1, CA => 2, C => 3, O => 4, S => 100, P => 200, H => 1000, ); my %greek = ( B => 1, G => 2, D => 3, E => 4, Z => 5, H => 6, ); sub sortable { my $atom = shift; $atom =~ /(\d)?(N|CA|C|O|S|P|H)(B|G|D|E|Z|H)?(\d)?/; # we may have empty groups, so quiet warnings no warnings 'uninitialized'; # make a number that sorts # keep parts at different orders of magnitude so they don't conflict $main{ $2 } + ( $greek{ $3 } * 100 ) + ( $1 * 20 ) + ( $4 * 10 ); } # operator changed to numeric comparison my @ordered = sort { sortable( $a ) <=> sortable( $b ) } @unordered; print join "\n", map { $_ ."\t". sortable( $_ ) } @ordered;
Gives:
N 1 CA 2 C 3 O 4 CB 103 CG 203 CD1 313 CD2 323 CE1 413 CE2 423 CZ 503 OH 604 2HB 1140 3HB 1160 HD1 1310 HD2 1320 HE1 1410 HE2 1420
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: sorting according to greek alphabet in roman letters
by seaver (Pilgrim) on Oct 02, 2003 at 18:01 UTC |