Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Building data structures from CGI params

by fullermd (Priest)
on Nov 21, 2010 at 11:38 UTC ( [id://872779]=note: print w/replies, xml ) Need Help??


in reply to Re: Building data structures from CGI params
in thread Building data structures from CGI params

that is not the way to manage session data

Quite. Luckily for me, I'm not trying to manage session data, I'm trying to manage form data :p

A flat namespace is great when you're dealing with 3 or 4 bits of info conceptually at the same level. When you're dealing with 20 or 50 or 200, that are conceptually at varying levels and associations, not so much.

Replies are listed 'Best First'.
Re^3: Building data structures from CGI params
by Anonymous Monk on Nov 21, 2010 at 12:11 UTC
    But what do those other guys call this feature? Link?

    It vaguely reminds me of JSON::Path

      I don't think they have any particular name for it. Just referred to as 'arrays from forms' or the like.

      PHP also understands arrays in the context of form variables (see the related faq). You may, for example, group related variables together, or use this feature to retrieve values from a multiple select input. For example, let's post a form to itself and upon submission display the data:

      (and following "Example #3" on http://us2.php.net/manual/en/language.variables.external.php)

      Related FAQ is How do I create arrays in a HTML <form>?

      The AnotherArray array will now contain the keys 0, 1, email and phone.

      (for those lucky enough not to have PHP on the brain, PHP's "array" serves as both a perl 'array' and 'hash')

        Well, that doesn't exactly match your syntaxt, but I found Ok, here is a start (with help from Corion)
        #!/usr/bin/perl -- use strict; use warnings; use CGI; use Data::Dump::Streamer; Main(@ARGV); exit(0); BEGIN { my %queries = ( # PHP::HTTPBuildQuery # URL decoded: "foo[bar]=baz", "foo[quick][quack]=schmack" "foo%5Bbar%5D=baz&foo%5Bquick%5D%5Bquack%5D=schmack" => { foo => { bar => "baz", quick => { "quack" => "schmack" }, }, }, ); sub Main { for my $query ( keys %queries ) { my $q = CGI->new($query); print $query, "\n", Dump( $queries{$query}, parse_str($q) +), "\n"; } } ## end sub Main } ## end BEGIN sub parse_str { my ($q) = @_; my %params; for my $k ( $q->param ) { my @v = $q->param($k); my $level = \%params; #~ my @k = grep length, split /\[([^\[]+)\]/, $k; #~ use DDS; warn Dump( { " $k => ", [ $k =~ /([^\[\]]+)/g ] } ); my @items = $k =~ /([^\[\]]+)/g; my $key = pop @items; for (@items) { $level->{$_} ||= {}; $level = $level->{$_}; } $level->{$key} = \@v; } ## end for my $k ( $q->param ) return \%params; } ## end sub parse_str __END__ foo%5Bbar%5D=baz&foo%5Bquick%5D%5Bquack%5D=schmack $HASH1 = { foo => { bar => 'baz', quick => { quack => 'schmack' } } }; $HASH2 = { foo => { bar => [ 'baz' ], quick => { quack => [ 'schmack' ] } } };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-03-19 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found