Danny Fang am Montag, 21. November 2005 13.59:
> Hi,

Hello Danny

>   I'm new to PERL and would like to seek help for the task mentioned below:
>
>   I'm attempting to read the contents of a file containing rows with the
> format shown below:
> version|exchange|...
> E1440000100TT|006030766|0521|...
>
>   I'm interested in modifying the values at the 3rd and 26th column of 1
> particular row in this file and duplicating that row values to populate it
> to 4000 rows. There are 2123 rows in the this file currently.
>
>   Below is the script which I've written in order to modify the values at
> the column mentioned.
>
>   However, I'm not sure how I could rewrite the newly modified column
> values of that particular row back into the file - I want to use the
> particular row which had its columns modified to be duplicated and appended
> to the end of the current file for a specific number of time (adding more
> rows with the duplicated rows).
>
>  
> version|exchange|area|...
> E1440000100TT|006030766|...
> E1440000100TT|006030766|...
>
>   Could anyone help me out?
>
>   Thanks
>   Danny

Don't forget to check user input to the script.

>   ##open file for reading
> open(INPUTFILE, $inputFile) || die "Cannot open $inputFile \n";
>   @fileRecs = <INPUTFILE>;

close the file here...

perldoc -f close

> $totalRecs = scalar(@fileRecs)-1;

This will return -1 if there are no lines.

>   print "Total records in $inputFile is $totalRecs  \n";
>   ##open file for writting output
>
>   open(OUTFILE, ">$inputFile.tmp") || die "Cannot open $inputFile.tmp \n";

... and reopen it in append mode instead of above.

   open (OUTFILE, '>>', $inputFile.tmp)  
      or die "Cannot open $inputFile.tmp: $! \n";

(note the usage of $! to get the system error).

perldoc -f open
perldoc perlvar

>   $secondRow = $fileRecs[2];
> print "BEGINNING -- secondRow = $secondRow \n";

$secondRow (scalar) does not exist, but @secondRow (array) does.

You are warned if such thins occur if you put the following lines at the 
beginning of every script oder module:

   use strict;
   use warnings;

> @secondRowRec = split("|", $secondRow);
>
>    $secondRowRec[2]="AAAA";
>    $secondRowRec[3]="BBBBB";
>
>   print "\$secondRowRec[2] = $secondRowRec[2] and
> \$secondRowRec[3]=$secondRowRec[3] \n";
>
> print "\$secondRow is now $secondRow \n";
>
>   $diffOfNewRec = 4000 - $totalRecs;
>
>   print "Need to produce additional $diffOfNewRec \n";
>
>   #making a backup copy
>   `cp $inputFile $inputFile.tmp`;

You have this file already opened; and no error checking is done for the copy 
operation.

perldoc -f system
perldoc perlop ("Quote and Quote-like Operators")
perldoc File::Copy

>   ## I'm need help here !! Not sure how I could re-join the elements
> modified back into the array containing that particular row and append it
> to the end of the file ##
>   print OUTFILE for ($i=0;$i<$diffOfNewRec; $i++){
>    print OUTFILE "$secondRow\n";
> }

You could pack the row changements and the appending to the file in a loop:

for (1..$diffOfNewRec) {
   # change the row in some way here
   my $line=join '|', @secondRow; # concat values
   print OUTFILE $line, "\n"; # and append it to the file
}
close OUTFILE or die $!;

perldoc perlsyn (loops)
perldoc -f join

>   close OUTFILE;


hoping to help

joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to