Gufranul Haque wrote:
> 
> Perl Gurus,
> 
> need some help with File Processing. I am trying to process a file where each
> line is treated as a record. The fileds in the record are separated by whitespaces.
> The number of white spaces between two fields can be >=1.
> 
> The idea is to read the input file, and copy it to an output file keeping the
> format of the records same (i.e same number of whitespaces betwwen two fileds).

Do you intend to modify the fields in some way?  It is not clear from
your code
why you want to do this.  If you do not want to modify the fields then
just print
out the input line without modifying it.


> Here is the program that I have written,
> _____________________________________________

You should enable warnings and strict when developing your program to
let perl
help you find mistakes.

use warnings;
use strict;


> $FILE1 = <@ARGV>;

You really do not want to do that.  The proper way to get the first
element from
an array is:

$FILE1 = $ARGV[0];

Or use shift:

$FILE1 = shift @ARGV;

And because @ARGV is the default argument to shift it could be written
as:

$FILE1 = shift;


> print($FILE1);
> 
> open(INFILE, $FILE1);

You should ALWAYS verify that the file was opened correctly.

open INFILE, $FILE1 or die "Cannot open $FILE1: $!";


> @array = <INFILE>;

There is no need to read the entire file into an array.


> close(INFILE);
> open(OUTFILE,">>xyz.dat");

You should ALWAYS verify that the file was opened correctly.

open OUTFILE, '>>xyz.dat' or die "Cannot open xyz.dat: $!";


> foreach $line (@array) {
>  @out = split(/  +/, $line);
>  $count = @out;
>  print($count);
>  print("\n");
>  for ($i=0;$i<$count;$i++)
>  {print OUTFILE (@out[$i]);}
> }
> ______________________________________________
> 
> Input File
> _________________________________________
> 101  2.00  2.00  2.00  2.00
> 101  2.00  2.00  2.00  2.00
> _________________________________________
> 
> However the output file generated is:
> _____________________________________________
> 
> 1012.002.002.002.00
> 1012.002.002.002.00
> _____________________________________________
> 
> The spaces between the fields are missing. Please let me know how to get it working.


If you don't need to modify the fields:

use warnings;
use strict;

open OUTFILE, '>>xyz.dat' or die "Cannot open xyz.dat: $!";

while ( <> ) { # reads the file(s) in @ARGV line by line
    my $count = split;
    print "$count\n";
    print OUTFILE;
    }

__END__


If you do need to modify the fields:

use warnings;
use strict;

open OUTFILE, '>>xyz.dat' or die "Cannot open xyz.dat: $!";

while ( <> ) { # reads the file(s) in @ARGV line by line
    # preserve leading whitespace
    my $lead = $1 if /^(\s+)/;
    my @data = map [ /(\S+)(\s*)/ ], /\S+\s*/g;
    # modify the non-whitespace fields
    $_->[0] += 5 for @data;
    print @data . "\n"; # count - @data in scalar context
    print OUTFILE $lead;
    print OUTFILE @$_ for @data;
    }

__END__



John
-- 
use Perl;
program
fulfillment

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

Reply via email to