a function in Perl/Ruby to remove newline at the end of a string,
Questions tagged [chomp]
60 questions
2
votes
1 answer
Is there a time when 'gets' would be used without 'chomp'?
When collecting user input in Ruby, is there ever a time where using chomp on that input would not be the desired behavior? That is, when would simply using gets and not gets.chompbe appropriate.

scobo
- 197
- 1
- 11
2
votes
1 answer
Perl: After Chomping a string, it does not print the string's value
So I am currently trying to write a perl script that reads to a file and writes to another. Currently, the problem that I have been having is removing new line characters from parsed rows.I feed in a file like this
BetteDavisFilms.txt
1.
Wicked…

MooCow
- 1,397
- 1
- 12
- 30
2
votes
3 answers
How to use chomp
Below I have a list of data I am trying to manipulate. I want to split the columns and rejoin them in a different arrangement.
I would like to switch the last element of the array with the third one but I'm running into a problem.
Since the last…

JDE876
- 407
- 1
- 5
- 16
2
votes
2 answers
"Use of uninitialized value in scalar chomp" in Perl
I get the below error when i run the script: Could someone help me on this
Use of uninitialized value $user in scalar chomp at ./temp.pl line 38, line 558.
Use of uninitialized value $modelName in scalar chomp at ./temp.pl line 39, …

pauler
- 107
- 2
- 4
- 10
1
vote
2 answers
Java How to remove carriage return (HEX 0A) from String?
If a particular String contains a newline character that is invisible (not \n but is 0A in hexadecimal because this value is passed down from the database), how can i able to chomp it away? Will Apache Chomp…

Oh Chin Boon
- 23,028
- 51
- 143
- 215
1
vote
2 answers
check last blank line of HTTP request using perl chomp
I'm pretty new to Perl. My Perl program is getting the HTTP request message from browser, I want to detect the last blank line.
I was trying to use $_ =~ /\S/, but which doesn't work:
while () {
print $_;
if ($_ =~ /\S/) {print…

draw
- 4,696
- 6
- 31
- 37
1
vote
6 answers
Why is my Perl program not reading from the input file?
I'm trying to read in this file:
Oranges
Apples
Bananas
Mangos
using this:
open (FL, "fruits");
@fruits
while(){
chomp($_);
push(@fruits,$_);
}
print @fruits;
But I'm not getting any output. What am I missing here? I'm trying to store all…

Waffles
- 121
- 3
- 7
1
vote
3 answers
chomp() usage in Perl
I have an XML file. There is some blank line in the file. How can I remove only the blank lines? I tried chomp() method and found that it will remove all the new line symbols. I still need to output the standard XML file format.
while(my $line =…

Nano HE
- 9,109
- 31
- 97
- 137
1
vote
1 answer
My code does not work without that specific line. Why?
puts "Enter the first number"
num1 = Float(gets)
puts "Enter the second number"
num2 = Float(gets)
puts "Enter the operation"
op = gets
op = op.chomp # <--- THIS LINE!
case op
when "+" then puts num1 + num2
when "-" then puts num1 - num2 …

Jesus
- 51
- 3
1
vote
1 answer
Read from text file, store in array succesfully but the program will not exit the while loop
So this is what should be of interest for you to be able to help me, the program is written in perl and I'm using both warnings and strict in perl but I get to the end of the text file and then I get stuck with the last output.
my $filename =…

ricksson
- 41
- 6
1
vote
1 answer
Perl chomp doesn't remove the newline
I want to read a string from a the first line in a file, then repeat it n repetitions in the console, where n is specified as the second line in the file.
Simple I think?
#!/usr/bin/perl
open(INPUT, "input.txt");
chomp($text =…

Ashraf Bashir
- 9,686
- 15
- 57
- 82
1
vote
1 answer
Why doesn't chomp() work in this case?
I'm trying to use chomp() to remove all the newline character from a file. Here's the code:
use strict;
use warnings;
open (INPUT, 'input.txt') or die "Couldn't open file, $!";
my @emails = ;
close INPUT;
…

shensw
- 223
- 1
- 3
- 14
1
vote
2 answers
Chomp doesn't work with perl script
I have this script
open (DICT,'file 0 .csv');
my @dictio = ;
chomp @dictio;
print @dictio;
My file 0 .csv is like this :
AAAA , a
AAAT , b
AAAC , c
So using chomp I want to delete the new row character, but when I print it my array…

crocket
- 37
- 4
1
vote
1 answer
Sublime text can't understand gets.chomp
I wrote this simple program in ruby using Sublime Text and for some reason if I build it using Sublime text inbuilt system then I get the following error
`deposit': undefined method `chomp' for nil:NilClass (NoMethodError)
Its running perfectly if…

anonn023432
- 2,940
- 6
- 31
- 63
1
vote
1 answer
Trying to run a simple gets.chomp in Sublime
For some reason when I run:
name = gets.chomp
puts name
I get the error:
gets:1:in ': undefined methodchomp' for nil:NilClass (NoMethodError)
If I remove the .chomp and run:
name = gets
puts name
I get no prompt or error, the console just…

user2109834
- 21
- 2