Home » Faqs » Manipulating text » Chomp |
You have a variable in your program (perhaps containing some text that you've read in from a file), that has a newline at the end of it.
#!/usr/bin/perl use strict; use warnings; while (<>) { my $var = $_; print "I read: " . $var . "from the file\n"; }
...which gives you this output...
I read: 12345 from the file
...but you want to see...
I read: 12345 from the file
The chomp
function removes the input record separator from the end
of a string.
#!/usr/bin/perl use strict; use warnings; while (<>) { my $var = $_; chomp $var; print "I read: " . $var . "from the file\n"; }
perldoc -f chomp perldoc perlvar (see the entry for $/)