Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

removing \n with for each

by MynameisAchint (Novice)
on Jun 13, 2013 at 13:24 UTC ( #1038736=perlquestion: print w/ replies, xml ) Need Help??
MynameisAchint has asked for the wisdom of the Perl Monks concerning the following question:

hey

I have written this code to extract columns from a csv file , it is working fine , but in last line there is a \n which I need to remove . please tell me where to chomp or any other way to remove this \n

sub column_segregation_Spec_values columns of csv file { $column_number=$_[0]; c my $size; my @array_A1=(); my @array_A2=(); my $column_separator = ","; $column_number--; + my $file="Working_On.csv"; open(FILE,"<","$file"); my @lines=<FILE>; close FILE; foreach my $line (@lines) { my @columns = split(/$column_separator/,"$line"); push @array_A2, $columns[$column_number]; } return (@array_A2);

Comment on removing \n with for each
Download Code
Re: removing \n with for each
by choroba (Abbot) on Jun 13, 2013 at 13:32 UTC
    If you want to remove the newline from all the lines in the array, use
    chomp @lines;

    If you want to delete the newline only from the last line, use

    chomp $lines[-1];
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: removing \n with for each
by Happy-the-monk (Monsignor) on Jun 13, 2013 at 13:32 UTC

    my @lines=<FILE>;

    chomp(my @lines=<FILE>); # should remove your newlines.

    Cheers, Sören

    (hooked on the Perl Programming language)

Re: removing \n with for each
by davido (Archbishop) on Jun 13, 2013 at 15:38 UTC

    I have written this code to extract columns from a csv file ,

    Arrgghhhhhh! Noooooooooo! Text::CSV

    (Whew, that feels better.)

    Unless your goal is to learn how to write a CSV parser, don't write code to parse CSV files; it's been done (correctly) on CPAN.


    Dave

Re: removing \n with for each
by space_monk (Chaplain) on Jun 13, 2013 at 15:42 UTC

    Why bother extracting columns from a CSV file yourself when people have done it before? See Text::CSV_XS and its incredibly long list of relations.

    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2014-08-29 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (281 votes), past polls