Danny Fang wrote:
  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).

It is considered bad form to write back into a text file since a change of length of one byte in a record could screw-up the rest of the file.


#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

##open file for reading
open(INPUTFILE, $inputFile) || die "Cannot open $inputFile \n";

# Is $inputFile tainted? That is, is it hard-coded
# or a user given value. To protect yourself from
# malicious users, use:

open INPUTFILE, "< $inputFile" or die "...";

# If you don't see why, ask yourself what if the
# user gives ">~/.profile" as the $inputFile?
# What if he gives "rm -fr * |"?

  @fileRecs = <INPUTFILE>;
$totalRecs = scalar(@fileRecs)-1;
  print "Total records in $inputFile is $totalRecs  \n";
  ##open file for writting output

  open(OUTFILE, ">$inputFile.tmp") || die "Cannot open $inputFile.tmp \n";
  $secondRow = $fileRecs[2];
print "BEGINNING -- secondRow = $secondRow \n";
@secondRowRec = split("|", $secondRow); $secondRowRec[2]="AAAA";
   $secondRowRec[3]="BBBBB";

# Now put the record back together.
$secondRow = join( "|", @secondRowRec );
$fileRecs[2] = $secondRow;


  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`;

# Bad idea. You have just opened $inputFile.tmp for writing. Try:
use File::Copy qw( copy move );
copy( $inputFile, "$inputFile.bak" );

## 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"; } close OUTFILE;

for my $rec ( @fileRecs ){
  print OUTFILE, $rec or die "cannot write to output: $!";
}

# To speed up processing, the computer stores
# up file writes in a buffer and only
# writes when it is full. The close command
# will write the last little piece in the buffer
# and should be tested for error.
close OUTFILE or die "cannot write to output: $!";

# Now move the new file to the old.
move( "$inputFile.tmp", $inputFile );

__END__

You mentioned that you want to add more records to the end but you haven't indicated what they are.



--

Just my 0.00000002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

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