Re: When do you use chop instead of comp?
by steves (Curate) on Mar 02, 2002 at 18:45 UTC
|
chop always takes off the last character of a string
and returns that character.
chomp takes off any and all trailing newlines, but
leaves the string alone if there are no newlines. It returns the number of characters removed. $/ determines what chomp thinks is a newline. So you can local that to remove newlines from files from other systems that have different newlines than your native OS.
| [reply] |
|
|
my $str = "some string\n\n\n\n";
chomp $str;
print "[$str]"; # still three trailing newlines
$/ = ""; # 'paragraph mode'
chomp $str;
print "[$str]"; # all gone
Note also that while chomp does return the number of
characters removed, that result may not be what you think
if you are in Windows or Mac OS where \n gets written out
as two characters. During the time that
Perl still has the string
in memory, the \n only counts as one character and the
second chomp in the example above returns 3 (not 6) on my
Windows machine -- just as it would on Linux.
On the other hand, if you assign a value to $/ that
truly is more than one character (internally),
that count gets
returned as you would expect.
$/ = 'ggl'; # giggle mode
my $str2 = "a string ggl";
my $cnt = chomp $str2;
print "$cnt"; # prints: 3
------------------------------------------------------------
"Perl is a mess
and that's good because the
problem space is also a mess." - Larry Wall
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: When do you use chop insetad of comp?
by data64 (Chaplain) on Mar 02, 2002 at 18:48 UTC
|
chomp will only remove any trailing string that corresponds to the current value of $/ usually the "newline" character whereas chop will remove the last character regardless of what it is. The benifit of using chop is that it is much faster since it does not try to scan the string and match the value of $/
See also chop() and chomp()
<cite>Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd</cite>
| [reply] [d/l] |
Re: When do you use chop insetad of comp?
by Mandor (Pilgrim) on Mar 02, 2002 at 18:46 UTC
|
You could use it in a case where you are absolutely sure that you want to remove the last character of a string. | [reply] |
Re: When do you use chop insetad of comp?
by webadept (Pilgrim) on Mar 02, 2002 at 20:09 UTC
|
Chop removes the last character, no matter what it is, EOL, or what ever. Chomp only removes EOL marks if there is one. If not, it does nothing.
The EOL mark Chomp removes corresponds to the current value of $/, so you may want to check that as well on your system.
Chomp also returns the total number of characters removed so you get a 1 or a 0 responce. Chop returns nothing.
webadetp.net | [reply] |
|
|
minor correction. chop returns the character removed.
$string = "abc";
print chop $string; # prints 'c'
print $string; # prints 'ab'
/\/\averick
perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"
| [reply] [d/l] |
|
|
Ah.. and so it does.. Most usful that. Thanks
| [reply] |
Re: When do you use chop insetad of comp?
by Juerd (Abbot) on Mar 02, 2002 at 19:10 UTC
|
| [reply] [d/l] |
Re: When do you use chop insetad of comp?
by shotgunefx (Parson) on Mar 02, 2002 at 20:55 UTC
|
I guess an appropriate place to use it would be when reading from STDIN when you are reasonably sure it is line terminated.
print "Enter a url!:\n";
my $url = <STDIN>;
chop($url);
In practice I NEVER use chop. Even though chomp is slower, I feel that with chop it's a lot easier to introduce subtle bugs. (Let's say you cut and paste a block of code.)
-Lee
"To be civilized is to deny one's nature." | [reply] [d/l] |
|
|
my $val = 123;
$val = chop($val) . $val;
print $val; # prints '312'
my $val = 'abc';
$val = chop($val) . $val;
print $val; # prints 'cab'
~Particle ;Þ
| [reply] [d/l] |
|
|
Pretty cool. Never would have ocurred to me. One of the things I love about Perl (besides making my life so much easier) is that even though I've used it every day for a few years there is still always something more to learn and one more way to do something..
-Lee
"To be civilized is to deny one's nature."
| [reply] |
Re: When do you use chop instead of chomp?
by dragonchild (Archbishop) on Mar 04, 2002 at 14:35 UTC
|
chomp is used for stripping newlines. chop is used when you want to destructively work with the last character of a string. Pretty simple. :-) (In other words, if you're using chop and discarding the return value, maybe you shouldn't be using chop ...)
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. | [reply] |
| A reply falls below the community's threshold of quality. You may see it by logging in. |