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!

Remove a Character

Status
Not open for further replies.

bnc123

Technical User
Mar 25, 2001
59
AU
I have a database in the form of a text file, where the fields are segregated by a pipe "|" character.

The HTML form that sends the data to my cgi script has many fields. I would like to remove the pipe character from all of the incoming data, if accidentally or intentionally input by the viewer.

So do I have to address each field separately:
Code:
$f_name =~ s/\|//g;
$l_name =~ s/\|//g;
$address =~ s/\|//g;
etc. etc or is there one generic command that will remove the pipe character from each and every one of the incoming fields.

Thanks for some help.

 
it depends on how you are fetching your form data (you should be using the CGI.pm to fetch the form data).

You can do it the way you have posted but I suggest you use tr/// instead of s/// to remove a character or a list of characters because its fatser than using s///:


$f_name =~ tr/\|//d;

the "d" option tells the "tr" operator to delete any character in the match list that does not have a corresponding character in the replacement list.


Post the method you are using to fetch your form data and someone can give you more specific advice.
 
Thanks Kevin,

Whether I use tr or s, your suggestion still restricts the character removal to one field only ($f_name).

What I am looking for is a generic one-line command that will remove the pipe character from ALL the incoming fields without having to address each and every field individually.
 
I repeat:

it depends on how you are fetching your form data (you should be using the CGI.pm to fetch the form data).

Post the method you are using to fetch your form data and someone can give you more specific advice.

Then you will get more specific advice. :)
 
Set up a list with your fieldnames and loop or map over the list, e.g.
Code:
my @fields = ($f_name, $l_name, $address);
s/\|//g for @fields;
Of course, that's 2 lines. If you insist on one:
Code:
($f_name, $l_name, $address) = map {s/\|//g; $_} ($f_name, $l_name, $address);
# [b]or ...[/b]
s/\|//g for ($f_name, $l_name, $address);
but the first with a named array is really better as it's easier to modify.

HTH
 
Many Thanks mikevh, that solves my problem. Being the lazy bloke that I am, I chose the one liner instead of the two liner.

Till the next time.... take care.


 
A little slicker way if you dont want to explicity spell out every variable into an array structure:

Code:
#! C:\Perl\bin\perl
use CGI qw(:standard Vars);
my %form = Vars();
print header;
s/\|//g for (values %form);
 
Sorry tweenerz, your way did not work for me. You see, I use cgi-lib.pl to parse my incoming form fields. I am not an advanced programmer.

Does your method work with cgi-lib.pl? If it does, then it will be great, because as you say, I won't have to spell out each and every field.

Thanks for some more advice.
 
how come you have not answered this suggestion if you are looking for the most elegant answer?

Post the method you are using to fetch your form data and someone can give you more specific advice.

You keep forcing people to guess the answer for you.

cgi-lib.pl is old and should be avoided but since you are using it, it normally uses the convention of %in to store fetched form data. So if you have something like this in your script:

Code:
%in = ReadParse();

then you can do this:

Code:
tr/\|//d for (values %in);

but that will remove the pipe from all form input fields and they may not be what you want. But if it is then the above should work OK.





 
Thanks Kevin, atlast it has worked. I did want the pipe removed from ALL the incoming fields right from the beginning. Maybe I did not understand your directive properly.

I started using cgi-lib.pl some time ago and although I understand that it is outdated, I am sticking with it. I am not a hot-shot programer and all I use it is for mundane purposes.

Anyway, have a nice day.
 
I am not a hot-shot programer and all I use it is for mundane purposes.
You should give it a try. By using the newer version, you may find that you end up having to do less programming yourself, as the module does more of the work for you...
 
OK, atlast I am making the switch from cgi-lib.pl to CGI.pm.

I have written a simple form script and have tried to remove the pipe character from ALL incoming fields as per suggestions above. The script works in principle but the removal of the pipe character does not seem to work.

Can someone please check out what is wrong with the code and let me know how to correct it. I am collecting all the incoming data in an array @data. My code is as follows:
Code:
#!/usr/bin/perl
use CGI qw(:standard Vars);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;

my @data = param();

#Now I am trying to remove the pipe character
#from ALL the incoming data
my %data = Vars();
s/\|//g for (values %data);

print header;

print start_html("Thank You");

my ($name, @data);
$name = param('name');
if ($name eq "")
{
print "Please insert name<br>\n";
}

my ($surname, @data);
$surname = param('surname');
if ($surname eq "")
{
print "Please insert surname<br>\n";
}

my ($email, @data);
$email = param('email');
if ($email eq "")
{
print "Please insert email<br>\n";
}

print h2("Thank You");

my %form;
foreach my $data (param()) {
        $form{$data} = param($data);
        print "$data = $form{$data}<br>\n";
}
print end_html;

#End


 
you removed the pipe from %data:

my %data = Vars();
s/\|//g for (values %data);

but you are not using %data to load the values of your variables, you are using param():

$name = param('name');

do this:

$name = $data{'name'};

for each of your variables and it should work. Personaly I would allow the pipe but convert it to ASCII entity so it doesn't mess up your flat file:

my %data = Vars();
s/\|/&#124;/g for (values %data);
 
I tried it your way, now it is giving me all sorts of errors.

Could you please copy my code, make the necessary changes and re-post it as code.

You have dealt with me before Kevin and you know that I am not that good at understanding things in bits and pieces.

So please do me that favour.
 
OK Kevin, after a little messing around, I did get it to work.

Many thanks.
 
looks like about 9 minutes worth of messing around (21:06 - 21:15). Glad you got it working [2thumbsup]
 
I hate to say it, but patience is not one of my virtues.

I am going to customise this script to add data to a text database file as well as upload a document to the server.

So be prepared for more questions on this one. Many more, if things don't go right. I could not get File::Type to work (reference the other discussion), mainly because I don't know how. So I solved the problem with javascript instead. But all that was with cgi-lib.pl. Now this is a new ball game for me, but this time I am determined to make the switch permanently, and with a little help from good samaritans like yourself, I think I'll get there sooner or later.

I'll be away for a week or so begining tomorrow on work.

Thanks in advance.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top