Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Initializing Hashes of Hashes (of Hashes)

by robartes (Priest)
on Apr 28, 2003 at 09:01 UTC ( [id://253624]=note: print w/replies, xml ) Need Help??


in reply to Initializing Hashes of Hashes (of Hashes)

Right, first off the bat - heed PodMaster's advice. What you are doing here does not make much sense: leaf nodes with the same path leading to them as earlier nodes will overwrite those nodes. Also, I don't know how CGI is supposed to handle a.b.c=1&a.b.c=2, but CGI.pm concatenates the values, which does not seem to be what you want.

That said, here's some code that does what you ask, although it does not make much sense :) :

use warnings; use strict; use CGI; use Data::Dumper; my $cgi = CGI->new( 'a.b.c=3&d.e.f.g=5&x.y=4' ); my %params=$cgi->Vars(); my %hash; while (my ($param,$value) = each %params ) { my $hashref=fill($param,$value); my $key=(keys %$hashref )[0]; $hash{$key}=$hashref->{$key}; } print Dumper(\%hash); sub fill { my $node=shift; my $value=shift; my @nodetree=split /\./, $node; if (scalar @nodetree == 1) { return { $nodetree[0] => $value } }; my $trunk=shift @nodetree; $node=join ".", @nodetree; return { $trunk => fill($node,$value) }; } __END__ $VAR1 = { 'a' => { 'b' => { 'c' => '3' } }, 'd' => { 'e' => { 'f' => { 'g' => '5' } } }, 'x' => { 'y' => '4' } };

Update: Scratch that remark wrg multiple values for the same param. My conclusion was based on an incorrect test of mine.

CU
Robartes-

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://253624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found