in reply to Manipulating data structure
This puzzles me slightly, because it appears to just be a POST data-stream which any of several CGI-handling packages already know how to handle. But, nevertheless, the so-called “auto-vivification” features of Perl make such things much easier than what you seem to be doing here. Consider the following one-liner:
perl -e 'my $foo; push @{$$foo{'bar'}}, 3; use Data::Dumper; print Data::Dumper->Dump([\$foo],["foo"]);' $foo = \{ 'bar' => [ 3 ] };
As you can see:
You could, therefore, simply loop through the set of keyword=value pairs, and push a value into a bucket for that keyword, trusting auto-vivification to automagically make those elements properly for you. (Perhaps you also use: next unless defined($value); ... in that loop.) Welcome to “The Swiss Army® Knife of Data Processing.”
If duplicates are a possibility and considered to be a problem, auto-vivification can be used just as easily to make a hash of hashes, e.g.
$$foo{$keyword}{$value} = 1;
or something similar. In any case, Perl allows you to achieve the desired results with a paucity of code.
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Manipulating data structure
by SerZKO (Beadle) on Aug 16, 2012 at 18:06 UTC | |
by locked_user sundialsvc4 (Abbot) on Aug 17, 2012 at 10:05 UTC | |
by SerZKO (Beadle) on Aug 17, 2012 at 11:01 UTC | |
by Cristoforo (Curate) on Aug 17, 2012 at 15:14 UTC | |
by SerZKO (Beadle) on Aug 17, 2012 at 18:40 UTC |