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!

Removing all spaces at the end of a variable 1

Status
Not open for further replies.

sulfericacid

Programmer
Aug 15, 2001
244
US
I have a form which asks for a real name and username and it checks against the DB to prevent duplicates from the same username. Problem is I can type in 'webmaster' and 'webmaster ' and 'webmaster ' and they will each be unique and still get saved to DB. I tried using a substitution but that didn't work. Can anyone help?

if(param()){
my $username = param('username');
$username =~ s/\s+\z//;
my $askname = param('askname');
$askname =~ s/\s+\z//;
my $update = param('update');

if($username){
if($askname){
if($update eq "add"){
if(exists $list{$username}){
print "Username already exists in database.\n";
}
else{
$list{$username} = $askname;
print &quot;Username was added to our system!<p>\n&quot;;

}
}
elsif($update eq &quot;rem&quot;){
if(exists $list{$username}){
delete $list{$username};
print &quot;Account information was removed from the system.\n&quot;;
}
else{
print &quot;Oops, it doesn't appear that username is in our database.\n&quot;;
}
}
}
else{
print &quot;For this to work please add your username.\n&quot;;
}
}else{
print &quot;Unless your name is null, please go back and fill it in.\n&quot;;
}
}

&quot;Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
You need to trim your whitespace, you could use a regex, but I prefer to call a function because I'm not too good at regexes
Code:
$username=trim($username);

...

sub trim {
    my @out= @_;
    for (@out) {
        s/^\s+//;
        s/\s+$//;
    }
    return wantarray ? @out : $out[0];
}

HTH
Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Sorry for the delay, thanks for your help!! That works great!!

&quot;Age is nothing more than an inaccurate number bestowed upon each of us at birth as just another means for others to judge and classify us- sulfericacid
 
Code:
sub trim {
    s/^\s*(.*?)\s*$/$1/ foreach @_;
    return @_;
}

lets you also use [tt]trim[/tt] as you would [tt]chop[/tt], ie:

Code:
   my $a = &quot;   hi&quot;;
   my $b = &quot;hi   &quot;;
   my $c = &quot;   hi   &quot;;
   trim($_) foreach ( $a, $b, $c );
   print &quot;[$a] [$b] [$c]\n&quot;;

as well as the more conventional

Code:
   my $a = &quot;   hi&quot;;
   my $b = &quot;hi   &quot;;
   my $c = &quot;   hi   &quot;;
   foreach ( $a, $b, $c ) {
       $_ = trim($_);
   }
   print &quot;[$a] [$b] [$c]\n&quot;;

HTH,


fish

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Here's a something a little more simple.

my $a = &quot; a b c &quot;;

$a =~ s/\s+//g;

print $a;

it prints &quot;abc&quot;;


or in a sub

my $a = &quot; a b c &quot;;

&trim($a);

print $a;

sub trim {
$_[0] =~ s/\s+//g;
}
 
Fine: but it takes spaces out of the middle of the string, which was not in the spec.

Yrs,

fish.



&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
just chop instances of whitespace from front and back ignoring anything in the middle. Also, if you wish, the commented regex removes duplicate spaces. Feel free to just copy and paste the subroutine out of the code below...
[tt]
#!/usr/bin/perl -w
##########################################

# just a string variable
my $erg = &quot; Hello there &quot;;

# print the variable before modifying
print $erg.&quot;<EOL>\n&quot;;

# print the return from the subroutine
print &removewhitespace($erg).&quot;<EOL>\n&quot;;

#==========End Of Main===================#

sub removewhitespace(){
#========================================#
# takes a single arguement and returns #
# the arguement with whitespace stripped #
#========================================#
my $value=shift();
$value =~ s/^\s+//;
$value =~ s/\s+$//;

# # uncomment me to remove duplicate spaces
# $value =~ s/\s+/ /g;

return $value;
}
[/tt]

-MikeyG
 
Thanks mg,

I didn't need the whole function. Just the part about stripping the space from the end.

It seems these books talk a lot about the ^ for replacing from the front. And they even have a lot of examples.

But when it comes to using the $ they mention it a few times, but they never give any good examples.

I keep putting it in the wrong places.

Now I know where it goes.

Thanks! I gave you a ~ S T A R ~ for that!

tgus

____________________________
Families can be together forever...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top