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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression within forms

Status
Not open for further replies.

craazycanuck

Technical User
Apr 28, 2001
7
CA
Hello everyone

I have a form with two text inputs....the difficulty I am running into is...I need to parse the spaces between words from the first text input but leave the spaces between words from the second text input.I am sure I need to adjust my regular expression but unclear as to how..Sorry for such a basic question .....just learning.

Ty in advance
Dave

whichmethod = $ENV{'REQUEST_METHOD'};
if($whichmethod eq "GET"){
$forminfo = $ENV{"QUERY_STRING"};
}else
{
$forminfo = <STDIN>;
}

@key_value_pairs = split(/&/,$forminfo);
foreach $pair (@key_value_pairs){
($key,$value) = split(/=/,$pair);
$value =~ s/\+//g;
$value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack(&quot;C&quot;, hex($1))/eg;
$FORM_DATA{$key} = $value;
}

}

 
# The convention is to let the %FORM_DATA hash contain
# the text that was entered by the user. So, replace
# the '+'s in the URI string with spaces.


@key_value_pairs = split(/&/,$forminfo);
foreach $pair (@key_value_pairs)
{
($key,$value) = split(/=/,$pair);
[red]$value =~ s/\+/ /g;[/red]
$value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack(&quot;C&quot;, hex($1))/eg;
$FORM_DATA{$key} = $value;
}

# Then, handle each input key and value according to
# your rules for that value.

# Get a list from the first space delimited input.
@words_from_first_input = split(/ /,$FORM_DATA{'first_input_field_name'});

# Get the second text input as a string
$second_input = $FORM_DATA{'second_input_field_name'};


HTH


keep the rudder amid ship and beware the odd typo
 
I am surprised goBoating didn't say this, cause I know he is a big advocate of CGI.pm, but I think that CGI.pm is a lot better for beginners, and for use in general.

Just my $0.02.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Vic, I agree.....
You can catch and parse the incoming uri string pretty quickly/easily..
Maybe I tried to answer the question to narrowly at first .... thanks for the follow up...

#!perl
use CGI;
my $cgi_object = new CGI;
# There, I just parsed the uri string
# now the fields are available to play with.....
$string = $cgi-object->param('field_name');

HTH

I have heard arguments that the CGI module is a lot to pull in if you don't need to. However, I think that while it uses a few more CPU cycles and more RAM, the ease of use so dramatically reduces the number of my brain cycles that it is well worth the system overhead. I find that carbon based CPU cycles are far more expensive than silicon based CPU cycles. ;-)


keep the rudder amid ship and beware the odd typo
 
There really isn't much system overhead if you only import the functions/sets of functions that you need instead of the entire module. eg.
Code:
#!/usr/bin/perl
use CGI qw/:cgi/;
or
use CGI qw/param header start_html end_html/;

brendanc@icehouse.net
 
Sorry guys ..nothing I try works .Is there any way I can assign a variable to one text fields then use the regular expression on this variable.

Ty for your patience
Dave
 
Actually go boating just showed you to do just that.:)

[tt]
#!perl
use CGI;
my $cgi_object = new CGI;
# There, I just parsed the uri string
# now the fields are available to play with.....
$string = $cgi-object->param('field_name');
[/tt]

The bolded line is assigning the value of the form field &quot;field_name&quot; to the variable named &quot;$string&quot;. You can then manipulate $string to your heart's content. If that was the field that you wish to strip the spaces from you could follow up the assignment with.
[tt]$string =~ s/ //g[/tt];

Derek
 
Thank you for your help guys.... but I know zero about CGI.pm. If I could bother you to check the following code I would be extremely gratefull
Thank you in advance
Dave

$whichmethod = $ENV{'REQUEST_METHOD'};
if($whichmethod eq &quot;GET&quot;){
$forminfo = $ENV{&quot;QUERY_STRING&quot;};
}else
{
$forminfo = <STDIN>;
}
$name =$FORM_DATA{'favoriteurl'};
@key_value_pairs = split(/&/,$forminfo);
foreach $pair (@key_value_pairs){
($key,$value) = split(/=/,$pair);
$value =~ s/\+/ /g;
$value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack(&quot;C&quot;, hex($1))/eg;
if ($name)
{$value =~ s/\+//g;}

$FORM_DATA{$key} = $value;
}

}
 
Dave,

I just noticed one significant error in your code which will cause errors. You left he dollar sign off whichmethod when you set it.
Should read [tt]$whichmethod = $ENV{'REQUEST_METHOD'};[/tt].

To be honest though, I have never bothered using any of the routines for manually parsing the incoming form because CGI.pm does it for me automatically, so I only have to worry about what I need to do with the form field values and not how to put them into a form that the script can use. It is really worth the time to give the CGI.pm documents ( or (assuming that your perl implementation is your path) issue the command [tt]perldoc CGI[/tt] to read the docs specific to your version and get a feel for it, it will more than earn you back the time you save reading it.
Here is an explanation of how to do what I think you want to do with CGI.pm, the code in red is code using CGI.pm and blue is the code from your example that it makes unnecessary. I will assume that your form fields are named text1 and text2 and you want to strip the spaces from text1.
[tt]

#!perl -w
use strict;
use CGI;
my $cgi_object = new CGI; #this one line
# brings in all the form data and sets up a
# hash containing all the form field names and
# values which are accessible through param().
# CGI.pm recognizes whether the form action
# was GET or POST so you don't have to test for it.


whichmethod = $ENV{'REQUEST_METHOD'};
if($whichmethod eq &quot;GET&quot;){
$forminfo = $ENV{&quot;QUERY_STRING&quot;};
}else
{
$forminfo = <STDIN>;
}
@key_value_pairs = split(/&/,$forminfo);
foreach $pair (@key_value_pairs){
    ($key,$value) = split(/=/,$pair);
    $value =~ s/\+//g; 
    $value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack(&quot;C&quot;, hex($1))/eg;
    $FORM_DATA{$key} = $value;
}
# basically all of the above is negated using CGI.pm


# now the fields are available to play with
# so I will set up script variables to hold
# the values of text1 and text2
my $text1 = $cgi_object->param('text1');
my $text2 = $cgi_object->param('text2');
# now, strip the spaces from the value in text1
$text1 =~ s/ //g;
# print the values back out to your web page to see what it looks like

print $cgi_object->header;
print $cgi_object->start_html(-'title'=>&quot;My Form Results&quot;);
print $cgi_object->p(&quot;\$text1 contains the following value: $text1&quot;);
print $cgi_object->p(&quot;\$text2 contains the following value: $text2&quot;);
print $cgi_object->end_html;

[tt]

Here is a simple html page that I made to test this out:
[tt]
<html>
<head>
<title>Test the form response</head>
<body bgcolor=&quot;white&quot;>
<h2>Just want to test out field value processing.</h2>
<form action=&quot;/cgi-bin/strip_space.pl&quot; method=&quot;GET&quot;>
<p>Enter a value in this field: <input type=&quot;text&quot; name=&quot;text1&quot; length=&quot;25&quot;></p>
<p>Enter a value in this field: <input type=&quot;text&quot; name=&quot;text2&quot; length=&quot;25&quot;></p>
<input type=&quot;submit&quot; value=&quot;Do It&quot;><input type=&quot;reset&quot; value=&quot;Clear the form&quot;>
</body>
</html>
[/tt]

The output I get after entering &quot;This is a test&quot; into each field and submitting the form is as follows:
[tt]
$text1 contains the following value: Thisisatest
$text2 contains the following value: This is a test
[/tt]

Derek
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top