in reply to Re: reference packing?
in thread reference packing?

Just to let you know what I was thinking ...

I wanted to pack a reference with a sort key, in the style of a Guttman-Rosler transform. If I have to pack in the whole data structure, that might make the sort a bit fat.

--
Brother Marvell

¤

Replies are listed 'Best First'.
(tye)Re: reference packing?
by tye (Sage) on Jul 23, 2001 at 20:37 UTC

    Sorting on a reference?? That doesn't make much sense. (:

    But however you encode the references into strings, you'll need to keep the real references external to the strings and use a lookup to restore them.

    To minimize space, you could stringify and cache via:

    my %refs; $str= pack( "N", 0+$ref ); $refs{0+$ref}= $ref;
    then restore via:     $ref= $refs{unpack("N",$str)};

    How to incorporate the packed value into the sort string and then extract it out again depends on how the rest of the string is built and so is left as an exercise. :)

            - tye (but my friends call me "Tye")

      I don't want to actually sort the references, I want to keep the references close to the sort key.

      Based on your splendid advice, I have created this small example to show what I meant. Imagine, that the structures contain tons of data, not just ip and host. One would not want to encode the whole thing using Dumper, etc.

      #!/usr/bin/perl -w use Data::Dumper; use strict; my @data = ( { 'ip' => '10.0.0.10', 'host' => 'laptop' }, { 'ip' => '10.0.0.1', 'host' => 'router' }, { 'ip' => '10.0.0.3', 'host' => 'desktop' } ); my %refs; @refs{map { pack('N',$_) } @data} = @data; # cache references my @sorted = map { $refs{substr($_,4)} } sort map { pack('C4N',split('\.',$_->{'ip'}), $_) } @data; print Dumper(\@sorted);

      Output:

      $VAR1 = [ { 'ip' => '10.0.0.1', 'host' => 'router' }, { 'ip' => '10.0.0.3', 'host' => 'desktop' }, { 'ip' => '10.0.0.10', 'host' => 'laptop' } ];

      Comments would be appreciated.

      Update: changed regexp for split

      --
      Brother Marvell

      ¤