in reply to Sorting binary


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.

Replies are listed 'Best First'.
To John Re: Sorting binary
by Anonymous Monk on Mar 13, 2002 at 16:20 UTC
    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