Hi,
Howdy.
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 ?
I don't think it matters too much, if you're sure of the format. Use what's easiest for you.
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.
+< is read and write, > is write and will clobber the file first.
if I open it like this >> then it erases the file first and I get no data.
This is append mode and should not have clobbered the file. I suspect you got a couple of these confused.
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> ) {
Don't do that. When you want to read line by line, use:
while (<SOURCE>) { #... }
Your loop reads the entire file into memory, mine handles it line by line, but they work the same on the inside.
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.
Super easy method first, from the command line:
perl -pli.old -e 's/\s\w+$//' searchandreplace.txt
If you prefer to do it in a script though, here's how I would go about it. Open your source file for reading and another file for writing. Copy it over and when you're done, you can rename it to the original name to replace the old file.
Good luck.
James
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]