Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: How to sort hash tables alpha-numeric

by juo (Curate)
on Oct 19, 2003 at 02:31 UTC ( #300348=note: print w/replies, xml ) Need Help??


in reply to Re: How to sort hash tables alpha-numeric
in thread How to sort hash tables alpha-numeric

This works perfect although I don't understand yet 100% how the Schwartzian transform works. Their is one small problem that I have with the code is that it should look for all leading characters to be split because now it substr only one but it could be more. (CR30-1 for example) So I tried to modify the code but no success.
sub sorted { return map { $_->[0] } sort { $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] } map { [ $_, split("-", s/^[A-Z]+//) ] } @_; } foreach my $key (sorted(keys %keys)) { print "'$key' => ", $keys{$key}, "\n"; foreach my $subkey (sorted(keys %{$keys{$key}})) { print " '$subkey' => $keys{$key}->{$subkey}\n"; } }

Replies are listed 'Best First'.
Re: Re: Re: How to sort hash tables alpha-numeric
by Aragorn (Curate) on Oct 19, 2003 at 10:46 UTC
    The Schwartzian Transform is explained here by the person after who this sorting technique is named.

    A modified version of the sorted routine which also takes the leading letters into account:

    sub sorted { return map { $_->[0] } sort { $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2] || $a->[3] <=> $b->[3] } map { [ $_, (/([A-Z]+)(\d+)(?:-(\d+)){1,2}/) ] } @_; }
    The split function is replaced by a regex which returns a list of letters and the numbers.

    Arjen

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://300348]
help
Chatterbox?
[marto]: usemodperl I guess that depends on what you mean by a safe space, since many people seem to have the impression a safe space allows them to do/say whatever they feel like, without question or critque
[marto]: 'typos'->'typo'
[usemodperl]: it's like you guys are retarded or something, no sense of humor? autism?
[usemodperl]: take things too literally, nothing is funny, everyhting must be perfect, or else, SCOLD SCOLD SCOLD, haha
[Veltro]: usemodperl I think you are offensive right now.
[marto]: people are very defnsive about their bad ideas behaviour' :P
[usemodperl]: sorry veltro, venting...
[usemodperl]: (is that wrong marto?)
[aitap]: usemodperl: maybe it's you who has changed
[marto]: demonstrably yes, since you claim to want a safe space, your definition for which seems to be a place where you can name call, make things up or otherwise post without being challenged

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2018-06-24 15:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?