Think about Loose Coupling | |
PerlMonks |
[ Natural Sort ]: Sorting a string with numbersby zzspectrez (Hermit) |
on Aug 12, 2005 at 23:52 UTC ( [id://483462]=perlquestion: print w/replies, xml ) | Need Help?? |
zzspectrez has asked for the wisdom of the Perl Monks concerning the following question: So I need to handle sorting some strings that contain numbers. Instead of rolling my own, I did a super search to see what others have done and found this example by tye. Just a few lines of code and does exactly what I need. Just could not understand what the heck it was doing at first glance. So I decided to deconstruct it and ask you all if I got it right. Lets analyze the code:
my @list = <DATA>;
Read in the data to the unsorted list. Ok, this part is easy. :)
Ok this is not as simple to follow.. Lets break it down. my @sorted = @list[ ... ];Ok, we are assigning an array slice to the @sorted array. So if the code between the brackets returned "1,5,2,3" then we the sorted array would be assigned $list[1], $list[5], $list[2], $list[3] in that order. The code inside the brackets returns the order which is assigned using an array slice.
What the??? Ok.. lets see.. First lets look at what the map is doing: map { ... } 0..$#listOk, The map is just itterating over the number 0 .. to the subscript of the last element in the array @list. So if there are 5 elements of in the array map will be getting 0 .. 4. So why are we not just doing for $a = ( 0 .. $#list)? That is because we want map to return a modified list to be sorted.
Now what is going on inside the map? Here we are setting $key to a value from the @list. We then use a greedy regex to globaly replace all numbers with the number packed as a 32 bit Big Endian Number. So the string "Happy4" would be converted to "Happy" . chr(00) . chr(00). chr(00) . chr(04). The modifier to the regex "e" evaluates the right side of the substitution as perl code. $key . pack "N", $_ Now we are taking the converted string and adding that elements position in the unsorted array as a packed 32 bit Big Endian Number and returning this value from the map. This results in a list of the unsorted array with all number converted to 32 bit Big Endian Format and the position tacked on to the last 4 bytes. This list is returned to the builtin sort function which can then sort the resulting array asciibetically. This sorts how we think it should. Given the strings "apple1", "apple2", "apple3","apple20". A normal sort would return the results in the order:
With the conversion from this routine, the strings are converted to:
Because of this conversion, the strings sort properly. Ok now for the last bit of code. map { unpack "N", substr($_,-4) }What this does is takes the last 4 chars of each string which is the the position in the unsorted array packed in Big Endian Format and unpacks it and returns it to be used as an array slice. So given the following @list = ( 'apple01', 'apple10', 'apple20', 'apple3', 'apple4');all the following sorts and maps result in a statement that would simplify as my @sorted = @list[ 0, 3, 4, 1, 2];Pretty cool. ++tye!! So only limitations I can see, would it could only sort a list up to 4,294,967,295 strings.. Not to bad. Anything you think Im missing.. Or any reason not to use this for sorting these types of strings? Performance issues? Bugs? etc.. You still using it tye?? :) Update
Back to
Seekers of Perl Wisdom
|
|