Questions tagged [chomp]

a function in Perl/Ruby to remove newline at the end of a string,

60 questions
173
votes
7 answers

How to remove leading and trailing zeros in a string? Python

I have several alphanumeric strings like these listOfNum = ['000231512-n','1209123100000-n00000','alphanumeric0000', '000alphanumeric'] The desired output for removing trailing zeros would be: listOfNum =…
alvas
  • 115,346
  • 109
  • 446
  • 738
11
votes
5 answers

How to strip all blank characters in a string in Erlang?

I know there is string:strip in erlang. But its behaviour is strange for me. A = " \t\n" % two whitespaces, one tab and one newline string:strip(A) % => "\t\n" string:strip(A,both,$\n) % string:strip/3 can only strip one kind of character And…
halfelf
  • 9,737
  • 13
  • 54
  • 63
10
votes
4 answers

How can I chomp every line in an array at once?

In the interest of writing cleaner code... IO.popen("Generate a list of files").readlines.each{ |line| chomped_line = line.chomp # ... }
HandyGandy
  • 660
  • 2
  • 6
  • 15
10
votes
6 answers

How to remove line break when reading files in Ruby

I'm trying to get rid of the brackets [] and the new line \n from being printed. My code looks like: name1 = File.readlines('first.txt').sample(1) name2 = File.readlines('middle.txt').sample(1) name3 = File.readlines('last.txt').sample(1) name =…
marriedjane875
  • 653
  • 2
  • 10
  • 24
8
votes
5 answers

Do we have an autochomp in Perl?

This is what my Perl code looks like for monitoring a Unix folder : #!/usr/bin/perl use strict; use warnings; use File::Spec::Functions; my $date = `date`; chomp $date; my $datef = `date +%Y%m%d%H%M.%S`; chomp $datef; my $pwd = `pwd`;…
Lazer
  • 90,700
  • 113
  • 281
  • 364
7
votes
5 answers

Clarification on chomp

I'm on break from classes right now and decided to spend my time learning Perl. I'm working with Beginning Perl (http://www.perl.org/books/beginning-perl/) and I'm finishing up the exercises at the end of chapter three. One of the exercises asked…
krebshack
  • 992
  • 1
  • 9
  • 19
4
votes
2 answers

Android Swipe View?

I don't know if you guys have ever heard of the app chomp but there is a layout in the app which looks like the image below. I'm wondering how they set this up what would i go about using to make something similar for my own application. The…
user577732
  • 3,956
  • 11
  • 55
  • 76
3
votes
4 answers

chomp in perl not working as expected

I found a strange behavior of chomp in Perl and I am unable to comprehend why is chomp is working like this. The following line does not work as expected if ( chomp($str1) eq chomp($str2) ) But, the following works fine chomp $str1; chomp $str2; if…
Bill
  • 5,263
  • 6
  • 35
  • 50
3
votes
1 answer

Print fails when using chomp()

So I have this problem while looping through a socket with a while loop. When I use this, it totally works fine but I have newlines on every $message, which I don't want. my $socket = new IO::Socket::INET ( LocalHost => "127.0.0.1", …
John Frost
  • 673
  • 1
  • 10
  • 24
3
votes
3 answers

How do I handle/store multiple lines into a single field read from a file in perl?

I am trying to process a text file in perl. I need to store the data from the file into a database. The problem that I'm having is that some fields contain a newline, which throws me off a bit. What would be the best way to contain these…
Highway of Life
  • 22,803
  • 16
  • 52
  • 80
3
votes
1 answer

perl remove trailing line not working

This is workfile.txt NC_001778 NC_005252 NC_004744 NC_003096 NC_005803 I want to read it in array and have only the string without spaces or lines . this code does what I want on my laptop but it's not working on the linux desktop! …
Mariya
  • 847
  • 1
  • 9
  • 25
3
votes
3 answers

When to use chomp?

What is the purpose and advantages of chomp function. What all can it do? Does using chomp creates any problems? or using chomp after file opening is necessary?
VAR121
  • 512
  • 2
  • 5
  • 16
3
votes
2 answers

Why is Perl's chomp affecting the output of my print?

It's been a couple months since I've been Perling, but I'm totally stuck on why this is happening... I'm on OSX, if it matters. I'm trying to transform lines in a file like 08/03/2011 01:00 PDT,1.11 into stdout lines like XXX, 20120803, 0100, KWH,…
user311121
  • 309
  • 1
  • 4
  • 14
2
votes
3 answers

Perl chomp does not remove all the newlines

I have code like: #!/usr/bin/perl use strict; use warnings; open(IO,"}; chomp($variable); print $variable; However when I print it, it still has newlines?
kamaci
  • 72,915
  • 69
  • 228
  • 366
2
votes
2 answers

Grab values between delimiters in Perl from chomped line

I am trying to grab the values between two delimiters in Perl using regex. I am opening a file and using chomp to go through the file line by line. Example of how the file looks: "This is an example of the file that I am…
Natalie
  • 447
  • 1
  • 4
  • 16
1
2 3 4