in reply to Pushing w/ an associative array?

The answer which no one is willing to give you is this:
my %pairs = split(/&|=/, $buffer);
A hash can accept an ordered list of pairs which it will assign using a KEY VAL KEY VAL scheme.

NOTE: I am only providing this answer because your method can be improved if you insist on ignoring the sound advice to use the CGI module for handling HTTP transactions. This sort of paired-list-to-hash assignment is very useful in creating arguments for subroutines, see here for more information.

Update:This post was made in the spirit of solving the problem as stated, and is a great example of why using the CGI module is better than recoding parts of it. While this solution will work for the stated problem involving key/value pairs, it will have serious problems (as pointed out below) when you have checkboxes or empty (no default values) form fields.

It will also likely break as soon as you change your form, unless you are overly restrictive in your form construction (and prohibit check boxes and enforce default values for all fields). All of this is an inefficient use of your time as a coder. Especially when working with CGI and web development, you open up a Pandora's box of problems for yourself or your clients if you do not take advantage of every well-tested solution (like CGI.pm) and get a solid understanding of the programmming you are doing before you build something that is so easily exposed for attack (that is, the web site).

Replies are listed 'Best First'.
Re: Re: Pushing w/ an associative array?
by merlyn (Sage) on Dec 27, 2000 at 22:09 UTC
    And this breaks if someone hands you the perfectly legitimate string of
    key1=val1&key2=val2&key3&key4&key5&key6=val6
    Please stop open-coding this. Cargo cult crap that breaks. And CGI is a pre-installed module (even if out of date)!

    -- Randal L. Schwartz, Perl hacker

      Good point.

      But look, it's great to point out the module (which I did), but is that a learning experience? Certainly CGI should be used for handling CGI, but no one answered this poor anon_monk's more general question, which was about handling lists and assigning values to hashes (and sadly, my answer assumed that we were feeding valid pairs in, something I would now know to change). Thus, this poor monk still has no idea how to write Perl to do this when there isn't a module involved. And so we'll have this really great use of modules strung together with the Perl equivalent of baby talk. This is good if the monk is going to immediately put something into production... but is this good for the personal growth of the monk?
Re: Re: Pushing w/ an associative array?
by davorg (Chancellor) on Dec 27, 2000 at 22:14 UTC

    "The answer which no one is willing to give you is this"

    And the reason that no-one is willing to give this answer is that it has fundamental flaws.

    As well as the example that merlyn gives, your code also breaks on a query string like this:

    key1=val1&key2=val2a&key2=val2b

    Update: D'oh! Corrected example so the values are actually different! Thanks merlyn.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Um, even if the values are different, it doesn't really break. It just overwrites the first value for key2 with the second listed value for this. Shouldn't keys be unique if we expect them to actually be keys? How does CGI handle this exception?
        It's quite common (and useful) to have multiple values associated with one name. These aren't keys in the same sense that unique identifiers are in a database. Considering the following:

        You're creating a Web page where people can sign up for mailing lists on their favorite sports. You have the following HTML:

        <input type="checkbox" name="football" value="football"> <input type="checkbox" name="soccer" value="soccer"> <input type="checkbox" name="tiddly winks" value="tiddly winks">
        In your code, you check for each of those values and subscribe the person to the mailing list appropriately. However, as time goes on, your site becomes wildly popular and you are looking at your IPO. You include more and more sports, but your scripts are becoming difficult to maintain because you have to test for each and every sport individually. Instead, change your HTML to the following:
        <input type="checkbox" name="sports" value="football"> <input type="checkbox" name="sports" value="soccer"> <input type="checkbox" name="sports" value="tiddly winks">
        Your query string will resemble the following (if they check both football and tiddly winks):
        sports=football&sports=tiddly%20winks
        In your script, you'd have something like the following:
        my @sports = $q->param('sports'); foreach my $sport ( @sports ) { # subscribe them to the list }
        That is much simpler than having separate names for each sports. Programmatically, it's the way to go.

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.