in reply to Initializing Hashes of Hashes (of Hashes)
Also, how could a user possibly cause "not a HASH ref" error? He's not writing any code.
update: Updated Shaz with actual code ;)(please note I did this in a single pass)#!/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' ] } } };
|
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. |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Initializing Hashes of Hashes (of Hashes)
by bsb (Priest) on Apr 29, 2003 at 01:03 UTC | |
by PodMaster (Abbot) on Apr 29, 2003 at 05:23 UTC |