Absolut Newbie wrote:
>
> Hi,
Hello,
> Newbie here. I am writing a program that takes a file that has two columns
> of words, extracts the second column, then saves the original file with only
> the data in the first column.
>
> Question #1 - would it be better to do this w/ the split or s/// functions ?
> Question #1 - how do I write back the results to the original file ? I tried
> using +< or > but all these do is add to the original file and not re-write
> it.
> if I open it like this >> then it erases the file first and I get no data.
>
> my source file (searchandreplace.txt) looks like this:
> XXXXX YYYYY
> XXXXX YYYYY
> XXXXX YYYYY
>
> my code looks like this:
>
> my $source_file = "searchandreplace.txt";
> open(SOURCE, "+<$source_file") || die "can't open file: $1";
> flock(SOURCE,2);
> foreach ( <SOURCE> ) {
> s/\s+\w+//;
> print SOURCE ;
> }
>
> after I run the program it looks like this:
> XXXXX YYYYY
> XXXXX YYYYY
> XXXXX YYYYY
> XXXXX
> XXXXX
> XXXXX
>
> instead of what I want
> XXXXX
> XXXXX
> XXXXX
The simplest way I could think of doing that would be:
use warnings;
use strict;
use Tie::File;
use Fcntl ':flock';
my $source_file = 'searchandreplace.txt';
my $t = tie my @data, 'Tie::File', $source_file or die "Cannot open $source_file: $!";
$t->flock( LOCK_EX );
s/\s+\w+// for @data;
untie @data;
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]