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

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")

Replies are listed 'Best First'.
Re: packed refernce transform
by marvell (Pilgrim) on Jul 24, 2001 at 18:15 UTC

    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

    ¤