unfortunately it was not working as i intended.
 
so i have new code with some help now:
using $column variable, i can control which column (from 1 to 4) i want to be 
printed with column 5.
 
open(IN_FH,"<","infile") or die "Cannot open infile because: $!";
open(OUT_FH,">","outfile") or die "Cannot open outfile because: $!";
foreach $line (<IN_FH>){
    @tmpAR=split(/ /,$line,5);                                   #/ +/ will 
split at any number of white spaces...
    next if $tmpAR[$column] == 0;
    push(@tmpAR2,"$tmpAR[$column] $tmpAR[4]");      # we can use -1 here as 
last element of array
 }
print OUT_FH sort { (split(/ /,$a))[0] <=> (split(/ /,$b))[0] } @tmpAR2;
close IN_FH;
close OUT_FH;
 
 
sample data in infile:
1 0 0 0 some text
0 2 0 3 text....
3 1 4 2 text....
4 3 1 1 text......

From: John W. Krahn <jwkr...@shaw.ca>
To: Perl Beginners <beginners@perl.org>
Sent: Friday, September 2, 2011 2:12 PM
Subject: Re: how to do this in perl (from unix)

Rajeev Prasad wrote:
> Thank you John,
> 
> that is almost perfect. only two issues:
> 
> 1.  getting error on sort for each line of input, column 5 onwards is text...:
>    Argument "154 Overload Status\n" isn't numeric in sort at ./test.pl line 
>30,<IN_FH>  line 208.
> 2.  by chaniging the column number in join (...[0,-1]) to  (...[2,-1]) I am 
> not getting column 2 and 5 in output, it still send column 1 and 5 in output 
> file.
> 
> again, that was cool.
> -Rajeev
> 
> 
> from Linux:
> cut -f1,5- -d" " file |grep -v "^0" | sort -n>  to_file
> to perl:
> open IN_FH,  '<', 'file'    or die "Cannot open 'file' because: $!";
> open OUT_FH, '>', 'to_file' or die "Cannot open 'to_file' because: $!";
> print OUT_FH sort { $a<=>  $b } map /^0/ ? () : join( ' ', ( split / +/, $_, 
> 5 )[ 0, -1 ] ),<IN_FH>;
> 
> I tried to put my understanding in words:
>     1. 0. operations starts from right most part of statement (statement ends 
> with ;)
>     2. split = breaks the line (contained in $_)
>  / +/ = any number of white space
>  $_ = the current line in in-built perl string variable
>  5 = number of fields string will be split into
>     3. join  = joins the new resulting string from eariler split
>  ' ' = uses single space as seperator
>  [0,-1] = element 0 and -1 of array repersented by (split...)
>     4. map  =<=========================================not able to understand 
> map (map expr, list )
>  /^0/ = expression to check wether line starts with 0
>  ? = if line starts with zero then ??? not sure....
>  () = not sure...
>  : = not sure...
>     5. sort  = sorts the final array on first column (which is by the way 
> numerical)
>     6. OUT_FH = writes the outcome to the file_handle.

The flow goes from <IN_FH> to map to sort to print.

<IN_FH> reads the file lines and passes the list of lines to map.

map has each line in $_.  This is matched with /^0/ and if it does match then 
return the empty list and if it doesn't match remove columns 2, 3 and 4 and 
then pass the resulting list to sort

sort does a numerical sort and then passes the sorted list to print.

print prints out the list to the filehandle OUT_FH.


To remove the error message you could do this:

{  no warnings;
    print OUT_FH sort { $a <=> $b } map /^0/ ? () : join( ' ', ( split / +/, 
$_, 5 )[ 0, -1 ] ), <IN_FH>;
    }



John
-- Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                  -- Albert Einstein

-- To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/

Reply via email to