Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

the chomp does not seem to work

by steph_bow (Pilgrim)
on Mar 11, 2008 at 09:51 UTC ( [id://673438]=perlquestion: print w/replies, xml ) Need Help??

steph_bow has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks

Could you help me fix this bug ? The chomp command does not seem to work. Here is my program. I knlow I have asked about a smilar problem but this time, it seems that chomp does not seem to work, at least on csv files.

#!/usr/bin/perl use strict; my $c_inFile = q{countries_list.csv}; open my $c_INFILE, q{<}, $c_inFile or die "Can't open countries_list.c +sv : $!"; my $c_outFile = q{test_list.csv}; open my $c_OUTFILE, q{>}, $c_outFile or die "Can't open countries_list +.csv : $!"; my $country; while (my $line = <$c_INFILE>){ chomp($line); my @Elements = split(";",$line); $country = $Elements[0]; print $c_OUTFILE "the country is p${country}p\n"; } close $c_INFILE; close $c_OUTFILE;

Here is my input file

Italy USA France

Here is my output file

the country is pItaly p the country is pUSA p the country is pFrance p
But when my input file is
Italy;1 USA;1 France;1

The output file is

the country is pItalyp the country is pUSAp the country is pFrancep

Replies are listed 'Best First'.
Re: the chomp does not seem to work
by moritz (Cardinal) on Mar 11, 2008 at 10:09 UTC
    chomp removes a newline from the end of the line, and what a newline is is defined by the $/ special variable.

    If you're on linux $/ will be a line feed, but the CSV file could have the windows line endings CR LF (carriage return + line feed).

    So you either need to adjust $/, or remove a bit more, for example all trailing spaces:

    $line =~ s/\s+$//;
Re: the chomp does not seem to work
by hipowls (Curate) on Mar 11, 2008 at 10:11 UTC

    I can reproduce the behaviour on my linux box using windows format text files, that is lines end with "\r\n". It works as expected when the line endings are just "\n" on linux

      That's why I usually use $line=~tr/\015\012//d.

      This removes any CR and LF from $line. It works fine as long as you don't care for empty lines.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: the chomp does not seem to work
by halfcountplus (Hermit) on Mar 11, 2008 at 13:52 UTC
    chop removes the final character of a line even if it's not an official newline, so you can use it as many times as you like, eg.
    $x="hello world\n"; chop $x; chop $x; chop $x;
    leaves "hello wor" -- chop chop until you get what you want, if all else fails.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://673438]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found