in reply to sorting a vec
If by "sort a vec" you mean, treat a bitstring as a collection of n-bit values and then sort them using perl's built in sort function without converting it/them to an array, you don't. At least, not easily.
The input to sort is an array as is the return. if you bit-fields are of equal length, which is also a power of 2, then you can use vec (or unpack/pack with a pattern of '(bn)*' to convert them to/from an array.
vec( $vec, $_, 4) = rand(16) for 0 .. 99; print map{ vec( $vec, $_, 4)} 0 .. 99; 6 15 0 10 4 6 10 1 13 9 7 1 1 1 7 12 3 6 14 7 4 7 8 7 0 4 10 0 13 6 6 9 14 7 10 5 12 4 15 11 13 12 7 2 13 14 4 7 0 10 9 4 10 5 3 12 10 9 15 12 5 3 8 0 0 6 7 0 2 14 14 1 3 13 4 15 8 10 2 8 11 2 0 14 10 4 8 6 10 1 10 7 10 13 6 5 12 6 0 15 $p=0; map{ vec( $vec, $p++, 4) = $_ } sort{ $a <=> $b } map{ vec( $vec, $_, 4) } 0 .. 99; print map{ vec( $vec, $_, 4)} 0 .. 99; 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 11 11 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14 15 15 15 15 15
It would be possible supply sort with a list of integers 0 to the length (in bitfields) of your vector and to play games inside the sort block, performing the switching of the bit-strings yourself using vec and returning the approriate value (-1, 0, 1) yourself to indicate what you had done. This is difficult to get right, fragile as heck, and grossly inefficient as the convertion for testing would be done for every comparison of every field, rather than just once at the beginning and once at the end. Nothing more than an intellectual curiosity. I probably shouldn't have even mentioned it:)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: sorting a vec
by Anonymous Monk on Nov 02, 2003 at 16:08 UTC | |
by BrowserUk (Patriarch) on Nov 02, 2003 at 16:49 UTC | |
by Anonymous Monk on Nov 04, 2003 at 05:43 UTC | |
by BrowserUk (Patriarch) on Nov 05, 2003 at 07:36 UTC | |
by Anonymous Monk on Mar 28, 2004 at 02:10 UTC | |
|