algorithm - How can I sort a Perl list in an arbitrary order? -


I have a list of wires, whose value comes from a fixed set. I need to sort this list arbitrarily.

The order of the set is specified by a list of all the possible strings, which is sorted into an array.

Here's an example:

  my @all_possible_strings_in_order = ('name', 'road', 'city', 'state', 'postcode'); My @list_that_needs_to_be_sorted = ('city', 'state', 'name');  

I am working in Perl. I understand my best condition that is automatically creating a hash that connects the stars with sequence numbers, and then the sequential numbers Sorts according to context.

There are approximately 300 possible strings in the set. Specific lists will have 30 strings that need to be sorted. It is not being called in a tight loop, but it can not be slowed too. Due to the structure of the program, the gradual hash can not be created automatically before time.

I'm open to suggestions for doing this in a better way. Thanks!

Edit: You are terrible. I can not keep my head up tonight, but tomorrow morning I am going to take time to understand your suggestions. This time I became skilled with the map () and grep ().

Set the relationship between the strings and their respective posts

  # Define your custom order of all possible wires in my @ custom_order = qw / name road city state / postalcode; My% order = map + ($ custom_order [$ _] = & gt; $ _), 0 ... $ # custom_order;  

You can now create a comparison function to use with Perl's sort operator:

  by-by-order { $ Order {$ a} & lt; = & Gt; $ Command {$ b}}  

For example:

  My sorted = sort by_order qw / city state name /; Print "@ named \ n"; # Print: Name city state  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

jquery - SimpleModal Confirm fails to submit form -