John,

Thanks for the mail. Yes you are right, I need to modify the contents of the
file. But need some clarification with some of the statements you have used

my @data = map [ /(\S+)(\s*)/ ], /\S+\s*/g; - what exactly is happening over
here?

$_->[0] += 5 for @data; ????? and

my $lead = $1 if /^(\s+)/; ?

Output xyz.dat after using your program. you will notice thatinstead of 7.00
it is showing 7, how to preserve the decimal point and trailing zeros??

____________________________________

106 7 7 7 7

106 7 7 7 7

___________________________________

I will try to explain the problem I am facing in detail:

File to be processed (Input File)

________________________________________________________

10111 2.00 2.00 2.00 2.00

10111 2.00 2.00 2.00 2.00

_________________________________________________

Restriction First field to remain same.

Parameter File

_______________________________________________

1,0.3,0.5,1

_________________________________________

Each filed in the parameter file corresponds to a field in the input file.

Logic -

>From 2nd field to eof in input file

If value in parameter file < 1

split that filed into two fields

i.e for the value in the 3rd field (2.00), corresponding parameter value is
0.3, so it should be splitted into two fields (0.60 and 1.40)

I am using the following program

______________________________________________________________

$FILE1 = $ARGV[0];

$FILE2 = $ARGV[1];

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

@array = <INFILE>;

close(INFILE);

open SPLITFILE, $FILE2 or die "Cannot open $FILE2: $!";

$ratio = <SPLITFILE>;

@sratio = split(/,/, $ratio);

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



foreach $line (@array) {

@out = split(/ */, $line);

print OUTFILE (@out[0]);

$count = @out;

for ($i=1;$i<$count;$i++) {

if (@sratio[$i-1] < 1) {

print OUTFILE (@out[$i] * @sratio[$i-1],@out[$i]*([EMAIL PROTECTED]));

} else {

print OUTFILE (@out[$i]);

}

}

}

____________________________________________________________________________
______

However the output file is,

101112.000.61.4112.00

101112.000.61.4112.00

I can get acoss the spaces problem once I am able to understand how to use
the map function, but how to preserve the decimal point and two zeros after
a field is modified.

Thanks,

Gufran

----- Original Message -----
From: "John W. Krahn" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, March 18, 2003 5:27 PM
Subject: Re: Need Help with File Processing


> 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]
>
>

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

Reply via email to