On 07/20/2006 07:07 PM, Alan Campbell wrote:
ello folks,
...got a bit carried away with references & deeply nested structures. Net result is I have an array of the form: - $ref <- reference to nested array
      [0]                             <- nothing at this level to key/sort on
          [0]                         <- this level contains the 'meat'
              ...data...
              {typedefName} = xyz;      <- would like to sort on this but its 
nested
          [1]
      [1]
Normally for sorting on an array we can do
      my @funcs = sort {$a->{typedefName} cmp $b->{typedefName}} @func_recs;
...but with a key-less extra level, how would I go about sorting?
   [...]

Get a reference to the array at $ref->[0] and sort that, e.g.
(watch out for wrapping):

use Data::Dumper;

my @data = (
     'parse',
     '_stripHTML',
     'strip',
     '_parsola',
     'new',
     'croak',
     'links',
);

# Construct the aggregate:
my $ref = [];
foreach my $dat (@data) {
    push @{ $ref->[0] }, { typedefName => $dat };
}

# Sort the aggregate:
my $dref = $ref->[0];
@$dref = sort { $a->{typedefName} cmp $b->{typedefName} } @$dref;

print Dumper($ref);

__END__

Or, just for the fun of it, you could use the Alias module to create a local alias to the nested array:

use Alias qw(alias);
local our @array;
alias array => $ref->[0];
@array = sort { $a->{typedefName} cmp $b->{typedefName} } @array;



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to