On Mon, Jun 30, 2003 at 08:52:32PM +0200, B. Fongo wrote:
> Hi
> 
> I have a file containing arrays of numbers. I 'm trying to get a
> subroutine iterate the file and print ONLY arrays with three elements
> into a second file. None of the elements has more than 2 digits. So I
> tried with a pattern match, it fails to deliver the right result.  

It would be helpful if you told us in what way it failed.  I suspect you
got lines with three or more numbers.

> I want it to find and print array with 3 elements like:
> 
> 1 2 5
> 20 33 45
> 5 17  25
> 
> 
> 
> sub get_arr{
>   
>       open (OUTPUT, "permed.dat") || die "Could not open file. $! \n";
>       open (INPUT, ">cleaned_up.dat") || die "Could not open file. $!\n";

You have a funny idea about input and output ;-)

>       my ($clean, @clean, $cleanprint, $arrlength, $result);
>       $clean = <OUTPUT>;
>       while ( $clean ){
>                       
>                       @clean = split (" ", $clean);
>                         print INPUT ("@clean") if (@clean =~ /\d+\s+\d+\s+\d+/);

I suspect that should be /^\d+\s+\d+\s+\d+$/

possibly allowing spaces at either end.

>                                         $clean = <OUTPUT>;
>                       
>       }
>       
>       die ("Could not close output file.  $! \n") unless close
> (OUTPUT);
>       die ("Could not close input file.  $! \n") unless close (INPUT);
>      }

But I would code that as:

  perl -ane 'print if @F == 3' permed.dat > cleaned_up.dat

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to