in reply to reference packing?

No.

Long answer is you need to squirrel away both the reference and what the reference points to. You will probably be happier with the results of Data::Dumper or Storable.

--
g r i n d e r

Replies are listed 'Best First'.
Re: Re: reference packing?
by marvell (Pilgrim) on Jul 23, 2001 at 19:45 UTC

    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

    ¤

      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

        ¤