This returns the same thing:
use strict; use warnings; use Data::Dumper (); my %shash; my $line; while (defined($line = <DATA>)) { # Get rid of end of line. chomp($line); # Build hash from line. my $p = undef; $p = { $_ => $p } foreach (reverse(split(/--/, $line))); # Merge hashes. my $key; my $base = \%shash; for (;;) { ($key, $p) = each(%$p); last unless ($base->{$key}); $base = $base->{$key}; } $base->{$key} = $p; } print(Data::Dumper::Dumper(\%shash)); __DATA__ Item1--Item2--Item3 ItemX--Item2--ItemA Item1--ItemV--Item3--Item4
After the fix to my code, it ended up being bigger than your code. ah well. Do note the use of chomp. It's much better than those regexps of yours.
In reply to Re: Generating Hashes from arrays of arbitrary size
by ikegami
in thread Generating Hashes from arrays of arbitrary size
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |