in reply to Convert delimited string into nested data structure
The name keys seem redundant to me. If you want to generate a nested hash structure based on the key strings then the following (non-recursive) code may get you started:
use strict; use warnings; use Data::Dump::Streamer; my %hash; while (<DATA>) { chomp; my @elements = split /\s*\/\s*/; next if ! @elements; $hash{$elements[0]} = {} if ! exists $hash{$elements[0]}; my $subHash = $hash{shift @elements}; for (@elements) { $subHash->{$_} = {} unless exists $subHash->{$_}; $subHash = $subHash->{$_}; }; } Dump \%hash; __DATA__ one foo / bar foo / baz foo / qux / two foo / qux / three foo / qux / four five
Prints:
$HASH1 = { five => {}, foo => { bar => {}, baz => {}, qux => { four => {}, three => {}, two => {} } }, one => {} };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Convert delimited string into nested data structure
by Anonymous Monk on Feb 19, 2007 at 20:30 UTC | |
by GrandFather (Saint) on Feb 19, 2007 at 21:30 UTC |