2

Below I have a list of data I am trying to manipulate. I want to split the columns and rejoin them in a different arrangement.

I would like to switch the last element of the array with the third one but I'm running into a problem. Since the last element of the array contains a line character at the end, when I switch it to be a thrid, it kicks everything a line down.

CODE

while (<>) {
    my @flds = split /,/;
    DO STUFF HERE;
    ETC;
    print join ",", @flds[ 0, 1, 3, 2 ];    # switches 3rd element with last
}

SAMPLE DATA

1,josh,Hello,Company_name
1,josh,Hello,Company_name
1,josh,Hello,Company_name
1,josh,Hello,Company_name
1,josh,Hello,Company_name
1,josh,Hello,Company_name

MY RESULTS - Kicked down a line.

1,josh,Company_name
,Hello1,josh,Company_name
,Hello1,josh,Company_name
,Hello1,josh,Company_name
,Hello1,josh,Company_name
,Hello1,josh,Company_name,Hello

*Desired REsults**

1,josh,Company_name,Hello
1,josh,Company_name,Hello
1,josh,Company_name,Hello
1,josh,Company_name,Hello
1,josh,Company_name,Hello
1,josh,Company_name,Hello

I know it has something to do with chomp but when I chomp the first or last element, all \n are removed. When I use chomp on anything in between, nothing happens. Can anyone help?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JDE876
  • 407
  • 1
  • 5
  • 16
  • 1
    If your system has `perldoc`, I'd recommend using it first when you have a question about a particular function. It's quite convenient to just run from your shell and the documentation is really good. Run like this: `perldoc -f ` or in this case, [`perldoc -f chomp`](http://perldoc.perl.org/functions/chomp.html) (I've also added a link to the online version). Note that the documentation for `chomp` contains a code example that looks very much like your code. – ThisSuitIsBlackNot Oct 31 '14 at 15:12
  • Very cool! Thanks much for the tip, I find myself needing hard examples for functions a lot of the time! @ThisSuitIsBlackNot – JDE876 Oct 31 '14 at 15:31

3 Answers3

4

chomp removes the trailing newline from the argument. Since none of your four fields should actually contain a newline, this is probably something you want to do for the purposes of data processing. You can remove the newline with chomp before you even split the line into fields, and then add a newline after each record with your final print statement:

while (<>) {
    chomp; # Automatically operates on $_
    my @flds = split /,/;
    DO STUFF HERE;
    ETC;
    print join(",", @flds[0,1,3,2]) . "\n"; # switches 3rd element with last
}
Dan
  • 10,531
  • 2
  • 36
  • 55
0
while ( <> ) {
    chomp; 
    my @flds = split /,/;
       ... rest of your stuff
 }

In the while loop, as each line is processed, $_ is set to the contents of the line. chomp by default, acts on $_ and removes trailing line feeds. split also defaults to using $_, so that works fine.

Technically what will be happening is the last element in @flds includes the trailing \n from the line - e.g. $flds[3].

Sobrique
  • 52,974
  • 7
  • 60
  • 101
0

The chomp() function will remove (usually) any newline character from the end of a string. The reason we say usually is that it actually removes any character that matches the current value of $/ (the input record separator), and $/ defaults to a newline.

Example 1. Chomping a string Most often you will use chomp() when reading data from a file or from a user. When reading user input from the standard input stream (STDIN) for instance, you get a newline character with each line of data. chomp() is really useful in this case because you do not need to write a regular expression and you do not need to worry about it removing needed characters.

while (my $text = <STDIN>) {
chomp($text);
print "You entered '$text'\n";
last if ($text eq '');

}

Example usage and output of this program is:

a word You entered 'a word' some text You entered 'some text'

You entered ''