Fist off, that doesn't compile.
Secondly, how did you solve it?
Thirdly, what if you have A.B.C=3&A.B=3, what happens then?
Your strategy is flawed, I can't fathom why you'd wanna do something like this.

Also, how could a user possibly cause "not a HASH ref" error? He's not writing any code.

#!/usr/bin/perl use strict; use CGI; my $cgi = CGI->new( 'a.b.c=3&a.b.c=4&x.y=4' ); # convert somehow to: my $args = { 'a' => { b => { c => [3,4] }, }, 'x' => { 'y' => 4, }, }; use Data::Dumper; warn Dumper $args; die Dumper Shaz( $cgi ); #sub Shaz { "goes here -- you show me yours, i'll show you mine" } sub Shaz { my( $cgi ) = @_; my $shazbah = {}; for my $i ( $cgi->param() ) { my @bits = split /\./, $i; my $shazbot = $shazbah; for my $bit( 0..$#bits ) { if( $bit == $#bits ) { if( exists $shazbot->{$bits[$bit]} ) { if( ref $shazbot->{$bits[$bit]} ne 'ARRAY'){ $shazbot = $shazbot->{$bits[$bit]} = []; } else { $shazbot = $shazbot->{$bits[$bit]}; } } else { $shazbot = $shazbot->{$bits[$bit]} = []; } last; } if( exists $shazbot->{$bits[$bit]} ) { $shazbot = $shazbot->{$bits[$bit]}; } else { $shazbot = $shazbot->{$bits[$bit]} = {}; } } push @$shazbot, $cgi->param($i); } return $shazbah; } __END__ # I'd say this is pretty close $VAR1 = { 'x' => { 'y' => 4 }, 'a' => { 'b' => { 'c' => [ 3, 4 ] } } }; $VAR1 = { 'x' => { 'y' => [ '4' ] }, 'a' => { 'b' => { 'c' => [ '3', '4' ] } } };
update: Updated Shaz with actual code ;)(please note I did this in a single pass)


MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
** The Third rule of perl club is a statement of fact: pod is sexy.


In reply to Re: Initializing Hashes of Hashes (of Hashes) by PodMaster
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.