AN wrote:

>>>Hi,

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


>>>thanx for your patience.

>>>AN



try this:

my $source_file = "searchandreplace.txt";
open(IN,"<$source_file") || die "can't open file: $1";
while(<IN>) {
        $row = $_;
        $row =~ s/\s+\w+//i;    
        push(@arr, $row);
}
close(IN);

open(OUT,">$source_file" ) || die "can't open file: $1";
foreach (@arr) {
        print OUT $_;
}
close(OUT);


E.

LOQUENDO S.p.A. 
 Vocal Technology and Services 
 www.loquendo.it 
 [EMAIL PROTECTED] 







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



====================================================================
CONFIDENTIALITY NOTICE
This message and its attachments are addressed solely to the persons
above and may contain confidential information. If you have received
the message in error, be informed that any use of the content hereof
is prohibited. Please return it immediately to the sender and delete
the message. Should you have any questions, please contact us by
replying to [EMAIL PROTECTED] Thank you
====================================================================

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

Reply via email to