Need a little help creating hashrefs of hashrefs, with the last key as a reference to an array.
use Data::Dumper;
my $foo = "a:b:c:d:a";
my $bar = "a:b:c:d:z";
my $hoh = {};
sub createHash {
my ($hoh,$orig,$rest,$last) = @_;
$rest = $rest || $orig;
$_ = $rest;
if (/^(.*?):(.*)$/) {
$hoh->{$1} = $hoh->{$1} || {};
createHash($hoh->{$1},$orig,$2,$1);
}
elsif (defined($last)) {
push (@{$hoh->{value}} , [$rest,$orig]);
}
return $hoh;
}
$hoh = createHash($hoh,$foo,undef);
$hoh = createHash($hoh,$bar,undef);
print Dumper($hoh);
What's Wanted:
$VAR1 = {
'a' => {
'b' => {
'c' => {
'd' => [
[
'a',
'a:b:c:d:a'
],
[
'z',
'a:b:c:d:z'
]
]
}
}
}
};
You can compare this with the output from codepad. Notice the subtle difference; instead of 'd' being a hashref that has an arrayref value
, 'd' is the arrayref and there is no value
.