Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^4: Building data structures from CGI params

by fullermd (Priest)
on Nov 21, 2010 at 22:23 UTC ( #872822=note: print w/replies, xml ) Need Help??

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

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

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')

  • Comment on Re^4: Building data structures from CGI params

Replies are listed 'Best First'.
Re^5: Building data structures from CGI params
by Anonymous Monk on Nov 22, 2010 at 02:31 UTC
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://872822]
[ELISHEVA]: yes - I set "f_encoding"
[erix]: still doesn't work?
[afoken]: sorry, forget File::BOM. I did not see DBD.
[ELISHEVA]: perldoc - of course
[erix]: hm, where's tux when you need him?
[ELISHEVA]: what's curiouser is that the underlying Text::CSV_XS appears to be BOM-friendly
[afoken]: DBD::CSV has a csv_class attribute. You could subclass Text::CSV_XS to handle BOMs, e.g. through File::BOM.
[ELISHEVA]: Text::CSV_XS already can handle boms - see its detect_bom flag
[ELISHEVA]: but it seems that the DBD::CSV layer on top of it doesn't
[afoken]: Also: All other attributes that start with csv_ and are not described above will be passed to Text::CSV_XS (without the csv_ prefix).

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2017-05-28 20:24 GMT
Find Nodes?
    Voting Booth?