Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

print last array element without a new line

by sy (Initiate)
on Oct 19, 2011 at 19:26 UTC ( #932480=perlquestion: print w/replies, xml ) Need Help??
sy has asked for the wisdom of the Perl Monks concerning the following question:

I'm very new to Perl so please excuse the simple question. I'm processing CSV files and for each record I split the record by comma and then printout a subset of fields in rearranged form. My problem is that I cannot print the LAST element of an array without also getting a newline. This is a problem because I want the last array element to be in the middle of a new record. I've tried using chomp (multiple times) on the input line, the array, or the array element, and I've consulted much documentation to no avail. Can someone offer and explanation and fix that suits a beginning programmer? For example, in the simplified but representative code below I get an unwanted new line after $field10

open(SOURCE, "<", $filename) or die "Can't open $filename"; while(<SOURCE>){ chomp(my $record = $_); my @field = split /\,/, $record; #split record into fields by comm +as print DATAOUT "$field[0],$field[10],$values[0],$field[1],$field[2] +\n"; }

Replies are listed 'Best First'.
Re: print last array element without a new line
by ikegami (Pope) on Oct 19, 2011 at 19:32 UTC

    Unless you're messing with $/ or $\, chomp will remove the newline, and there cannot possibly be two of them.

    Your code does exactly what you say you want it to do.

    while(<DATA>){ chomp(my $record = $_); my @field = split /\,/, $record; print "$field[0],$field[10],$values[0],$field[1],$field[2]\n"; } __DATA__ a,b,c,d,e,f,g,h,i,j,k a,b,c,d,e,f,g,h,i,j,k
    a,k,,b,c a,k,,b,c

    There's an empty field, but that's because I didn't give $values[0] a value. (In fact, I would get a warning with use warnings;.)

      Thanks for the check. And yet the code does NOT work. Against all expectation I continually get a new line after printing the last element of the array created by the split. I cannot find any explanation for why this might be occurring in any of my Perl books. I have read about $/ and $\ and checked through my code to ensure I didn't introduce some such change by a typo. So I'm left wondering how to solve the problem??? Anyone? The broader context may provide some hints: -the CSV files originate from LABVIEW -arrays of hashes are being used as a lookup table to link info from separate files. I'm tearing my hair out.
        Look at
Re: print last array element without a new line
by Lotus1 (Curate) on Oct 19, 2011 at 20:16 UTC

    How about posting a few lines of the csv that are causing the problem. Also post what the program output looks like when it prints the extra newline.

    Another option is to try Text::CSV. Maybe there is some special formatting in the csv file your program isn't handling.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932480]
Approved by ikegami
Front-paged by toolic
NodeReaper power washes the cellar floor

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2018-02-22 11:16 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (291 votes). Check out past polls.