Created
September 8, 2012 14:41
-
-
Save rlb3/3675529 to your computer and use it in GitHub Desktop.
hash of hashes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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