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-
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.