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

How Do I remove a specific chracter from the end of a string?

Status
Not open for further replies.

millimeter

Programmer
Mar 30, 2002
12
US
Greetings,

I am replacing spaces in a string with underscores:
$userInput = "newname " (notice space)
$userInput =~ s/( )/\_/g;
However if the user leaves space(s) at the end of the string it then is output as; "newname_"
This posses issues as you likely can imagine particularly when writting file names.

How can I remove it/them (in the case there is more than one) from the end of the string?

Thanks in advance.
 
Something like this:
Code:
my $char = ' ';
my $string = 'newname ';

$string =~ s/[$char]$//gi;

print "\$string = '$string'";
It looks for $char at the end of the string (or before a newline at the end, I think) which is the $ at the end of the regex. If you want to replace more than one, add a + after $char:
Code:
$string =~ s/[$char]+$//gi;
You can get fancier and set $char to more than one character, and it'll chop off any occurances of any combination of the characters at the end.
Code:
$string = 'newname t ';
$char = ' t';
chops down to just 'newname' ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
{

$userInput = "newname " (notice space)
chomp $userInput; #<-remove \n character if exists
local $/=' ';
chomp $userInput; #<-remove trailing space if exists
$userInput =~ s/( )/\_/g;
return $userInput;
}

or

$clean =join('_', split(/ /, $userInput));
 
about the easiest way would be:

#!/usr/bin/perl -w

$userInput = &quot;newname &quot;; #added enough spaces @ end to make point
$userInput =~ s/\s+$//gi;
print &quot;\&quot;$userInput\&quot;&quot;;

this will work for no space, a space, or many spaces @ the end of a string. I do not know why no one else posted this simplistic answer. Learn perl regex folks, it saves lives and code.

 
TIMTOWTDI ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
TIMTOWTDI

In one regex:
Code:
s/\s+(?!\s*$)|(\s+$)/$1?'':'_'/eg;
(condenses one or more spaces to one '_')
nothing was mentioned about leading spaces so it doesn't treat those as special (i.e. they get replaced with '_', too).

jaa
 
While not a solution to the problem, I have to ask. What's wrong with an underscore at the end of a file name?
 
I have a similar problem. I wish to take a file and change the newlines to spaces (spacebar spaces), except for every fifth newline, which is to be replaced by two newlines.

I believe I have to use:

#taken from the Perl cookbook chapter at OReilly

undef $/;
$wholefile = <FILE>;

but replacing the newlines to spaces is throwing me because I am new.

thanks
 
if you loop through the file then you can do something like
Code:
    my $repl = $. % 5 ? '_' : '__';
    s/\n/$repl/;
jaa
 
Thanks jaa. I don't really understand the code though.

i did try this, but it hangs, making a large destination file before I kill the script:
my $counter;

while (<>){
chomp $_;
$counter++;
while ($counter % 5 !=0){
print &quot;$_ &quot;;
}
print &quot;$_\n\n&quot;;
}

I run it at the command line to read the infile and write to the outfile.


 
[tt]while ($counter % 5 !=0){[/tt]

You want an [tt]if[/tt] here, not a [tt]while[/tt]. You don't change [tt]$counter[/tt] in the [tt]while[/tt], so it'll infinitely loop.
 
You'll also want to put [tt]print &quot;$_\n\n&quot;;[/tt] in an [tt]else[/tt], otherwise you'll get 4/5 lines printing twice and two newlines after every single print.
 
Is there a way to do this without a loop? That was my original goal. The justice41 solution may be it; I just can't understnd the code.

I think it would code something like:
(after setting $/ to undef)

s/(*)\n(*)\n(*)\n(*)\n(*)\n/$1_$2_$3_$4_$5\n/g

I don't know how to write the metacharacter for &quot;spacebar&quot;, and the default variables corresponding to the string in parentheses is also not clear to me.
 
to operate on the whole file as a string (i.e. without a loop) change your guess to
Code:
s/(.*?)\n(.*?)\n(.*?)\n(.*?)\n(.*?)\n/$1 $2 $3 $4 $5\n\n/g;
As long as the number of lines in the file is a multiple of 5 then this will work.

Another way which works newline by newline instead of 5 at a time is
Code:
$i = 0;
s/\n/++$i % 5 ? &quot; &quot; : &quot;\n\n&quot;/ge;
The /e evaluates the expression in the replace side of the s/// so that it replaces with a space, &quot; &quot;, or a double newline, &quot;\n\n&quot; depending on the counter.
the '[tt]expr ? x : y[/tt]' is the same as '[tt]if (expr) { x } else { y }[/tt]'.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top