469,366 Members | 2,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

RE: how do you remove the last character

How do you remove the last chracter( in this case ,) from the end of the line only
Aug 23 '07 #1
5 24655
numberwhun
3,503 Expert Mod 2GB
That will completely depend on whether you want to remove the last character, regardless of what it is, or only if it is a carriage return.

If you want to remove the last character, any character, from the end of a line, then you will need to use chop. If you want to remove just the newline character from the end of every line (ie: from user input or read from a file), then you will need to use chomp.

I highly suggest that you read through the perlfunc page as it will enlighten you to the multitude of standard functions that Perl has available to it, as well as how they work.

Regards,

Jeff
Aug 23 '07 #2
chomp is used for removing the last character.( RETURN)

mean while chop is used for removing the last character irrespective of whatever it is.
Aug 26 '07 #3
Kelicula
176 Expert 100+
Also I believe you can use:
Expand|Select|Wrap|Line Numbers
  1. $var = "This is a string";
  2.  
  3. substr($var, -1);
  4.  
Just for principle...

The other two examples are much more advantagous.
Aug 26 '07 #4
numberwhun
3,503 Expert Mod 2GB
That won't remove the character and leave you the rest of the string, that will just give you the last character of the string and disregard the rest. Plus, to have substr() give you back anything, you would have to have its value assigned to a variable. Here is a full example. Run this and you will see what I mean:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $var = "This is a string";
  5. my $newvar = substr($var, -1);
  6.  
  7. print("$newvar");
  8.  
Regards,

Jeff
Aug 26 '07 #5
KevinADC
4,059 Expert 2GB
The other two examples are much more advantagous.
And besides advantageous, they are correct.

Hint: check the results of your code before posting. ;)
Aug 26 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Donald Canton | last post: by
18 posts views Thread by Toto | last post: by
9 posts views Thread by yawnmoth | last post: by
6 posts views Thread by Daniel Mark | last post: by
34 posts views Thread by Registered User | last post: by
100 posts views Thread by jacob navia | last post: by
9 posts views Thread by ice | last post: by
10 posts views Thread by Mike Copeland | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.