(hmm, talking to myself..)
An updated and safer (I hope) version.
I stole some ideas from CGI::State and a piece of TT2's variable naming.
#!/usr/bin/perl -wl use Data::Dumper; use CGI; sub cgi2args { my $cgi = shift; my $args = {}; for my $name ($cgi->param) { my @vals = $cgi->param($name); my $val = @vals == 1 ? $vals[0] : \@vals; #print "$name $val"; my ($first, @segments) = split /\./, $name; my $a = \$args->{$first}; for (@segments) { if(/^(\d+)$/ && $1 < 50) { $$a = [] unless defined $$a; die "CGI param clash for $name=$_" unless ref $$a eq'A +RRAY'; $a = \($$a->[$1]); } else { $$a = {} unless defined $$a; die "CGI param clash for $name=$_" unless ref $$a eq ' +HASH'; $a = \($$a->{$_}); } #print "end ",Dumper([$args,$a]); } die "CGI param clash for $name value $val" if defined $$a; $$a = $val; } return $args; } $Data::Dumper::Terse = 1; $Data::Dumper::Indent = 0; for $q ( 'a.0=3&a.2=4', 'a.0=3&a.2=4&b.c.0=x&c=2&c=3&d=', # bad ones 'a.0=3&a.c=4', 'a.c=3&a.0=4', 'a.0=3&a=b', 'a.a=3&a=b', 'a=3&a.0=b', 'a=3&a.a=b', ) { print " $q => ", eval { Dumper(cgi2args(CGI->new($q))) } || $@; } __END__
The results (more or less):
$perl -w a a.0=3&a.2=4 => {'a' => ['3',undef,'4']} a.0=3&a.2=4&b.c.0=x&c=2&c=3&d= => {'a' => ['3',undef,'4'], 'b' => {'c' => ['x']}, 'c' => ['2','3'], 'd' => ''} a.0=3&a.c=4 => CGI param clash for a.c=c at a line 22. a.c=3&a.0=4 => CGI param clash for a.0=0 at a line 18. a.0=3&a=b => CGI param clash for a value b at a line 27. a.a=3&a=b => CGI param clash for a value b at a line 27. a=3&a.0=b => CGI param clash for a.0=0 at a line 18. a=3&a.a=b => CGI param clash for a.a=a at a line 22.

In reply to Re: Re: Initializing Hashes of Hashes (of Hashes) by bsb
in thread Initializing Hashes of Hashes (of Hashes) by bsb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.