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 (Patriarch) on Oct 19, 2011 at 19:32 UTC | |
by sy (Initiate) on Oct 19, 2011 at 19:50 UTC | |
by onegative (Scribe) on Oct 19, 2011 at 20:14 UTC | |
by ikegami (Patriarch) on Oct 19, 2011 at 23:02 UTC | |
by sy (Initiate) on Oct 19, 2011 at 21:05 UTC | |
|
Re: print last array element without a new line
by Lotus1 (Vicar) on Oct 19, 2011 at 20:16 UTC |