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
¤ |