thdrought has asked for the wisdom of the Perl Monks concerning the following question:

Real Perl newbie question...Searched the site but couldn't find the answer to this simple problem.

I have a script that retrieves a response using HTTP:Request and HTTP:Response. I get back a response in the content as a URLEncoded string, for example 'id=101&results=passed'

I would like to take this response, and parse it into an array where I can call each value by it's key name, for example:

print 'My ID = %myarray['id']'; print 'My Results = %myarray['results']';

How can I do this?

Thanks,
Tom

"With each day passing, what have you been doing?" --Lord Buddha

Replies are listed 'Best First'.
Re: Newbie Array Question
by borisz (Canon) on Oct 13, 2004 at 11:21 UTC
    One way is to use CGI.
    use CGI; my $cgi = CGI->new('id=101&results=passed'); print 'My ID = ', $cgi->param('id'), "\n"; print 'My Results = ', $cgi->param('results'), "\n";
    but read the docs for CGI.
    Boris
Re: Newbie Array Question
by Mutant (Priest) on Oct 13, 2004 at 11:22 UTC
    One way would be to use CGI to get the values out for you. (assuming the response is in $response):
    use CGI; my $q = new CGI($response); print 'My ID = ' . $q->param('id'); print 'My Results = ' . $q->param('results');
    This is probably safer than rolling your own method.

    If you want to access them as a hash, you can call the Vars() method:
    my %params = $q->Vars; print "My ID = $params{'id'}";
    Note the difference in syntax to your example - you have to use double quotes to print variables, and hash elements are accessed with a $ sigil and curly brakets around the key name.
Re: Newbie Array Question
by muba (Priest) on Oct 13, 2004 at 11:28 UTC
    First: When you access a value from a hash, use $, { and }. (that would be $myarray{'id'}. According to the funny character % and to the way you try to read a value from it (by named key, not by index number) you are using a hash.
    However, according to the name of the variable, you're trying to use an array.
    So, second: an array is prepended with a @, and if you try to fetch one single value from it, you use $. Furthermore, you should access the value by their index number, for arrays do not have named keys like hashes do. So that would be $myarray[0] for example.

    So, before we (I?) can answer your question in a helpfull manner, please convince yourself of what you want to do. And I know it is rude to say, but I'd suggest to read something of a manual or a book or anything else to teach you those basics.




    "2b"||!"2b";$$_="the question"
    Besides that, my code is untested unless stated otherwise.
    One more: please review the article about regular expressions (do's and don'ts) I'm working on.
Re: Newbie Array Question
by gothic_mallard (Pilgrim) on Oct 13, 2004 at 12:17 UTC

    As previously mentioned, CGI (or possibly CGI Lite) may be the way to go.

    You are getting confused between arrays and hashes. In some languages (such as Javascript) you can indeed reference elements by named indexes as in your example. So for a quick array vs hash overview.

    In perl, an array is really an ordered list. The elements are indexed by the order in which they appear in the array and, unless there is specific intervention, this order remains. As with other languages, the array indexing begins with 0.

    A hash on the other hand is a set of unordered key-value pairs where both the key is a scalar (numeric or alphanumeric) and the value may be of any type (scalar, array, hash, glob etc).

    Check out the Q&A section for a wealth of information on arrays, hashes and CGI programming.

    --- Jay

    All code is untested unless otherwise stated.