Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple split questions 1

Status
Not open for further replies.

perlone

Programmer
Joined
May 20, 2001
Messages
438
Location
US
Hi,

I have string like this:

@strings = split(/&/, $ENV{QUERY_STRING});
my($email, $qpass, $query) = @strings;

The first $email is the user email, $qpass is the user password and $query is the query string. So right now, all my links are like this:
login.cgi?$email&$pass&login_string. I want to turn to like this:
login.cgi?email=$email&pass=$pass&mode=login.

Any idea how?
 
Split on the &, and then split each of those strings on the equal sign. This is similar to what the cgi query string code does, but you don't need to worry about decoding, I guess. Here's the code:
Code:
# Split data into list (array) of key=value entries
@FormData = split(/&/, $FormData);

# Process the FormData list
foreach $x (0 .. $#FormData) {
   # Convert plus signs back to spaces
   $FormData[$x] =~ s/\+/ /g;
   # Split into key and value (on the first equal sign found).
   ($key, $val) = split(/=/, $FormData[$x], 2);
   # Convert %XX sequences into characters
   $key =~ s/%(..)/pack("c", hex($1))/ge;
   $val =~ s/%(..)/pack("c", hex($1))/ge;
   # Replace list element with converted values
   $FormData[$x] = $key . "=" . $val;
   # Create associative array member
   $FormData{$key} = $val;
}
You may be able to strip out some parts of this code. The incoming string is in $FormData. The results are in the %FormData hash. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
it's kinda works but thanks again for the code.
 
Why does it only "kinda work"?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
well, i pasted your script, but it's still not accepting email=$email&pass=$pass.
 
Try adding this line at the top of my code:
Code:
$FormData = $ENV{'QUERY_STRING'};
And remember that your results will be $FormData{'email'}, $FormData{'pass'} and $FormData{'mode'}.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Sorry about that. I cut&pasted this code from my CGI data parsing routine, and it's set up to get $FormData from either GET or POST methods. I took that part off, and forgot to add back just the GET method line. :~/ Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top