in reply to List Values As Multidimensional Hash Keys
Please. Do this without eval and without arbitrary restrictions on numbers of lists.
use List::Util 'reduce'; use Data::Dumper; $_ = "a1:a2:a3=foo"; my ($key,$val) = split(/=/); my @keys = split /:/, $key; my $last = pop @keys; my %hash; ( @keys ? reduce( sub { $a->{$b} ||= {} }, \%hash, @keys ) : \%hash )->{ $last } = $val; print Dumper( \%hash );
|
|---|