--- Matt Simonsen <[EMAIL PROTECTED]> wrote: > I need to get all the words in a query (q=___) out of a (URL, I think) > encoded string. > > Example line and my current plan: > > $queryString = > 'pp=20&q=Finance/Banking/Insurance&qField=All&qMatch=any&qSort=smart&view=1' > > my %querys = $queryString =~ /(\S+)=(\S+)&?/g ; > > #Here I could use a tip on how to split counting the encoding > @words = split ($querys{q}) ; > > I have to do this for millions of lines per month so any tips to help me > optimize this would be appreciated.
There are a variety of ways to do this. If you are receiving the query string from a Web form: 1. If all names occur only once in the query string (e.g., no 'color=red&color=blue') use CGI qw(:standard); my %data; foreach my $name ( param() ) { $data{$name} = param($name); } 2. If you know the names might occur more than once in a query string and you are comfortable with array references: use CGI qw(:standard); my %data; foreach my $name ( param() ) { $data{$name} = [param($name)]; } 3. If some people prefer string not to use array references if you only have a single value and use array references for multiple values: use CGI qw(:standard); my %data; foreach my $name ( param() ) { my @values = param($name); $data{$name} = 1 == @values ? param($name); : [param($name)]; } However, if you already have a query string in the program (read from a parsed HTML doc, for example), the you switch to the OO form of CGI.pm. Using the first example: use CGI; my $cgi = CGI->new( $query_string ); my %data; foreach my $name ( $cgi->param() ) { $data{$name} = $cgi->param($name); } You should be able to extend that to the other examples. If you use the last example, don't use that with a Web submission because that implies that you are grabbing $ENV{ QUERY_STRING } yourself. Don't do that. Let CGI.pm handle it for you because if you ever need to change that in the future (using POST and reading from standard input), CGI.pm will handle that transparently for you and you won't have to update your code. Cheers, Ovid Cheers, Ovid ===== "Ovid" on http://www.perlmonks.org/ Web Programming with Perl: http://users.easystreet.com/ovid/cgi_course/ Silence Is Evil: http://users.easystreet.com/ovid/philosophy/decency.txt __________________________________________________ Do you Yahoo!? U2 on LAUNCH - Exclusive greatest hits videos http://launch.yahoo.com/u2 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]