[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Dimensional question



On Thu, Aug 05, 1999 at 11:26:44AM -0400, Chris Nandor wrote:
> Here is something.
> 
> #!perl -w
> use strict;
> use Data::Dumper;
> 
> my %s;
> while (<DATA>){
>   my($ref, @tmp) = (\%s, split);
>   for my $i (0 .. $#tmp) {
>     $ref->{$tmp[$i]} = $i == $#tmp ? 1 : {};
>     $ref = $ref->{$tmp[$i]};
>   }
> }
> print Dumper \%s;
> __DATA__
> a 1 2 3
> b 4 5
> c 6 7 8 9 10 11 12
> 

This code will clobber previous entries when the input coincides along in
the first column.  Try running it with the following data:

a 0 1
a 1 2


There must also be a separation between the final value and the next level
in the structure, because the value and the hash reference cannot be stored
together.

#!/usr/local/bin/perl -w

use strict;
use Data::Dumper;

my %s;
while (<DATA>){
    my($ref, @tmp) = (\%s, split);
    for my $i (@tmp) {
        $ref->{$i} ||= {};
        $ref = $ref->{$i};
    }
    $ref->{__VALUE__} = 1;
}
print Dumper \%s;

__END__
a 0 1
a 1 2
b 1
b 1 2 3


Obviously, you can't use __VALUE__ as a dimension label with this code.
But you shouldn't need to.  :)


Ronald

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org