in reply to switching two columns under conditions
EDIT: Sorry for the confusion, you will need to split the element if you do it this way, please take this code into consideration:for(@input){ if($_[1] gt $_[2]){ push (@output, "$_[0] $_[2] $_[1] $[3] +"); } print "$_\n" for @output;
The reason I posted the code is because I earlier told you to do it a certain way, without also telling you that you would need to use the split function and create a temporary array to store the results from the split.use strict; use warnings; my @input; my @output; while (<DATA>) { push @input, $_; } for (@input) { my @temp = split( /\s+/, $_ ); # you split on a delimiter, which i +s 1 or more spaces in this case if ( $temp[1] gt $temp[2] ) { push( @output, "$temp[0] $temp[2] $temp[1] $temp[3] +" ); } else { push @output, "$temp[0] $temp[1] $temp[2] $temp[3]"; } } print "$_\n" for @output __DATA__ red 15 2 smith blue 4 10 walter
|
|---|