homepage Welcome to WebmasterWorld Guest from 54.196.159.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
extracting key-value pair from an url with perl
Warboss Alex




msg:432040
 12:02 am on Mar 31, 2006 (gmt 0)

hey all,

I've got an url of the form

www.server.com?data=a+b+c&other_data=f

and I'm just trying to extract the 'data' values, not the rest. I tried extracting from?data= to the first & it finds like this:

$bits =~ s/(q\=)(.+)(&)/$2/

but it doesn't work .. is the ampersand a special character? what am I missing? :(

thanks in advance!

 

perl_diver




msg:432041
 1:41 am on Mar 31, 2006 (gmt 0)

why aren't you using CGI.pm to do this?

rocknbil




msg:432042
 5:08 am on Mar 31, 2006 (gmt 0)

. . . or any other read/parse. But he may be examining a string. The mechanics could go like

$url = 'www.server.com?data=a+b+c&other_data=f';
($dom,$query_string) = split (/\?/,$url);

foreach $pair (split(/&/, $query_string)) {
($_n, $_v) = split(/=/, $pair);
## A full unpack is not required, all you have to deal with are spaces probably
$_v =~ s/[\+\%20]/ /g;
$qs{$_n} = $_v;
}

## you now have everything stored in a hash, or associative array, named %qs

print "content-type: text/html\n\n";
print "From URL $dom <br>\n";
foreach $k (keys %qs) {
print "key: $k value: $qs{$k} <br>\n";
}

## This should print

#www.server.com <br>
#key: other_data value: f <br>
#key: data value: a b c <br>

## so if you just wanted 'data'
print "$qs{'data'} <br>\n";

perl_diver




msg:432043
 6:39 am on Mar 31, 2006 (gmt 0)

you can still use CGI.pm if it's a string or reading input from a file, etc.


use CGI;
my $uri_string = 'www.server.com?data=a+b+c&other_data=f';
my $data = (split(/\?/,$uri_string))[1];
my $q = new CGI($data);
print $q->header;
my %data = $q->Vars;
print map {"$_ = $data{$_}$/"} (keys %data);

Warboss Alex




msg:432044
 1:26 pm on Mar 31, 2006 (gmt 0)

thanks guys

I was over complicating everything. :/

everything is fine now :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved