in reply to Array to nested hash

amw1,
No need to use evil eval, this should do it:
#!/usr/bin/perl use strict; use warnings; my @foo = 1..5; my %hash; my $ref = \%hash; for ( 0 .. $#foo ) { $ref->{ $foo[$_] } = $_ == $#foo ? undef : {}; $ref = $ref->{ $foo[$_] }; }
The question is - why?

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Array to nested hash
by tinita (Parson) on Nov 08, 2004 at 17:18 UTC
    The question is - why?
    I needed this once, too. Dynamically building data structures, usually if the data (here the array) comes from outside the program.

    I also once maintained a script which used the eval approach. The script was twice as fast after I replaced it with my code.

Re^2: Array to nested hash
by Mutant (Priest) on Nov 08, 2004 at 16:53 UTC

    The question is - why?

    Homework?

      No, not homework. Trying to generate a nested data structure from a path statement that looks like
      level1::level2::level3::leaf
      for a menuing system.