Skip to content

Instantly share code, notes, and snippets.

@xiaocang
Created May 20, 2016 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaocang/9d51abc1f2644e3dfcb46b2cad1fed0b to your computer and use it in GitHub Desktop.
Save xiaocang/9d51abc1f2644e3dfcb46b2cad1fed0b to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl [0/3]
use strict;
use warnings;
use Smart::Comments;
my @array = qw{a b c};
my $result = {};
my $r = $result;
sub walk {
my $v = shift;
my $value = shift;
if ($value) {
$r->{$v} = $value;
} else {
$r->{$v} = {};
}
return $r->{$v};
}
for (0..$#array) {
if ( $_ == $#array ) {
$r = walk($array[$_], [1, 2, 3]);
} else {
$r = walk($array[$_]);
}
}
### $result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment