Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey there, Can anyone direct me towards a Perl program, function or module, commercial or home grown, which sorts binary data??

Replies are listed 'Best First'.
Re: Sorting binary
by jmcnamara (Monsignor) on Mar 13, 2002 at 12:00 UTC

    You can use the pack function to pack binary data as a string. The packed data can then be sorted lexically:
    #!/usr/bin/perl -wl use strict; my @ints = (34, 0, 6, 9999, 1); print "@ints"; @ints = map {pack "n", $_ } @ints; # Pack the data @ints = sort @ints; # Sort lexically @ints = map {unpack "n", $_ } @ints; # Unpack it print "@ints";

    However, there are quite a few caveats, such as dealing with negative numbers and hardware endianness. But, perhaps you could give a bit more detail about what you are trying to do before we go down those paths.

    --
    John.

      There is a sequential binary file, with 150 lines and 100 fields in each row. There are two fields required one (a) is eight bytes and appears four bytes in, the other one (b) is four bytes on from (a) and is four bytes long. I am not scared of it, I just don't know how to make it do what I want it to do
Re: Sorting binary
by cjf (Parson) on Mar 13, 2002 at 11:18 UTC