Skip to content

Instantly share code, notes, and snippets.

@rlb3
Created September 8, 2012 14:41
Show Gist options
  • Select an option

  • Save rlb3/3675529 to your computer and use it in GitHub Desktop.

Select an option

Save rlb3/3675529 to your computer and use it in GitHub Desktop.
hash of hashes
#!/usr/bin/env perl
my $array = [qw/one two/];
my $hash = {};
my $value = [qw/done and done/];
sub head {
my (@list) = @_;
return shift @list;
}
sub tail {
my (@list) = @_;
shift @list;
return \@list;
}
sub last_e {
my (@list) = @_;
return pop @list;
}
sub build_hash {
my ($lst, $a) = @_;
return if !@$lst;
my $head = head(@$lst);
$a->{$head} = (last_e(@$array) eq $head) ? $value : {};
build_hash(tail(@$lst), $a->{$head});
}
build_hash($array, $hash);
__END__
OUTPUT:
$VAR1 = {
'one' => {
'two' => [
'done',
'and',
'done'
]
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment