Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Complex hash sorting

by allolex (Curate)
on Feb 01, 2004 at 03:30 UTC ( [id://325636]=note: print w/replies, xml ) Need Help??


in reply to Complex hash sorting

How can I sort the hash by any of those delimiters? I can sort $key, but that doesn't do the job. I need to be able to print the sorted hash by ANY bit of information.

Then a hash is probably not the data structure you want. Try an array of arrays instead. You know what is supposed to be stored in each field, right?

So the idea is that you put everything into a data structure like this (using the split function):

#!/usr/bin/perl use strict; use warnings; # an array: 0=name, 1=number, 2=city, 3=state, 4=zip my @customers = ( [ 'nye, bill','39','Somehere in Cali','Colombia','12345' ], [ 'simpson, homer','36','Springfield', 'OR', '23456' ], [ 'rubble, barney','31','Bedrock','cartoon location','33456' ] ); # set to the array field you want my $field = '0'; my @sorted = sort { $a->[$field] cmp $b->[$field] } @customers; map { print $_->[$field] . "\n" } @sorted; # OUTPUT nye, bill rubble, barney simpson, homer

Of course you can tweak this to print out what ever you want. This would be best as a subroutine to which you pass the sort field number, IMO.

--
Allolex

Replies are listed 'Best First'.
Re: Re: Complex hash sorting
by parv (Parson) on Feb 01, 2004 at 09:28 UTC

    Also, OP, consider using constants so that you would not have to think about array order more than once. And create a hash that would store mapping from type of elements to perl comparison operators...

    #!/usr/local/bin/perl use warnings; use strict; use constant { NAME => [ 0 , 'cmp' ] , NUMBER => [ 1 , '<=>' ] , CITY => [ 2 , 'cmp' ] , STATE => [ 3 , 'cmp' ] , ZIP => [ 4 , '<=>' ] # Assuming zip is a number } ; my @customers = ( [ 'nye, bill','39','Somehere in Cali','Colombia','12345' ] , [ 'simpson, homer','36','Springfield', 'OR', '23456' ] , [ 'rubble, barney','31','Bedrock','cartoon location','33456' ] ); # Get user input for sort order my @criteria = (NAME , NUMBER , ZIP , STATE); my $sort = make_sort( \@criteria ); my @sorted = sort $sort @customers; #my @control = # sort # { $a->[NAME ->[0]] cmp $b->[NAME ->[0]] # || $a->[NUMBER->[0]] <=> $b->[NUMBER->[0]] # || $a->[ZIP ->[0]] <=> $b->[ZIP ->[0]] # || $a->[STATE ->[0]] cmp $b->[STATE ->[0]] # } @customers; use Data::Dumper; $Data::Dumper::Deepcopy = 1; print Dumper( \@sorted #, \@control ) ; sub make_sort { my @how = @{ $_[0] }; my $sort = join ' || ' , map join( $_->[1] , q/ $a->[/ . $_->[0] . ']' , q/ $b->[/ . $_->[0] . ']' ) , @how ; return eval "sub { $sort ; }"; }

    Above can be retrofitted for other similar structures.

    Update, Feb 1, 2004: Above is another take of Fun with complex sorting... by Dave the davido.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://325636]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-19 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found