Opening a text file and editing its contents

T

toomanyjoes

I'm still having problems opening a file. I looked at the docs and this
is what I came up with.


open (FILE, "> C:\notes\gen.txt") || die ("error $!\n"); #See note 1


while (<FILE>) { #See note 2


s/\<[NR][a\d]\>/ /g #note 3


if (^\w\D\D\s\d+:\d+) #note 4


Note1: Although in all the examples I've looked at the actual file
reference is always very vague and I have never seen a full path with
the files extension in the quotes like I have done here.


Note2: Here I want to loop through the file, is this correct?


Note3: Here I want to find every instance (In the File) of a "<Ra> or a
<N1>(2,3,etc.)" and replace it with nothing (basically remove it)


Note4: Here I'm looking for a specific arrangement of characters at the
beginning of the line it can be any character followed by 2 letters
followed by a space followed by any number of digits separated by a
colon. (ex. 1RA 12321:123214 would match as well as ADD 4:2) if a match
occurs I want to remove the colon and separate these items with a
delimiter. For Example (ex. 1RA/ 12321/123214)


And thats as far as I've been able to get because I get an error
opening the file. It looks to me like I've done it by the book. But I
must be missing something. I'd appreciate any comments about my code.
Thanks,
Joe
 
S

Sherm Pendley

I'm still having problems opening a file. I looked at the docs and this
is what I came up with.

Posting the same message multiple times is rude. Please don't do that.

sherm--
 
A

A. Sinan Unur

I'm still having problems opening a file. I looked at the docs and this
is what I came up with.


open (FILE, "> C:\notes\gen.txt") || die ("error $!\n"); #See note 1

Note that \ is special here. In this case, \n translates to 'newline' and
and \g (I think) translates to the ASCII bell character. You would have
noticed this had you tried to print the file name.

Also, I do prefer using lexical filehandles and the 3 argument form of
open:

my $fn = 'c:/notes/gen.txt';

open my $file, '>', $fn
or die "Cannot open $fn: $!";
while (<FILE>) { #See note 2

Your posting style makes it very cumbersome to answer your other questions.

Sinan.
 
T

thundergnat

I'm still having problems opening a file. I looked at the docs and this
is what I came up with.

First of all, every program except the most trivial should start with

use warnings;
use strict;

Those two things will go a long way towards helping fix problems.
open (FILE, "> C:\notes\gen.txt") || die ("error $!\n"); #See note 1

I suspect your main problem is the newline character in the filename.
Double quotes interpolate, so the \n is read as newline. Use single
quotes instead. The fact that you are opening the file for output is
likely going to be a problem too. You would likely be better off with
3 argument open and lexical file handles also. Actually, it would
probably be better not to hard code the file name. Feed the file name
as a command line parameter instead.
Also, if you leave the newline off the die message, more useful
information will be supplied.


open my $file, '<', $ARGV[0] or die "Could not open file. $!";
while (<FILE>) { #See note 2

while ( said:
s/\<[NR][a\d]\>/ /g #note 3
Will also catch <Na> and <R0>,<R1>, etc. Ok if that's what you want, but
you may be better tightening up the assertion. It's not necessary to
escape the angle brackets either.

s/ said:
if (^\w\D\D\s\d+:\d+) #note 4

You description suggests something more like the following. Also, if you
are just checking if a pattern exists so you can change it if it does,
you are better off just trying to change it. It will if it exists and
won't if it doesn't

s!^(.\w\w)( \d+):(\d+)!$1/$2/$3!;


All together...

use warnings;
use strict;

open my $file, '<', $ARGV[0] or die "Could not open file. $!";
while (<$file>){
s/<(Ra|N\d)>//;
s!^(.\w\w)( \d+):(\d+)!$1/$2/$3!;
print;
}
 
T

toomanyjoes

What you have written is EXACTLY what I am looking for! I'm assuming
the print command on the last line is the command that actually
executes the changes above? My question is by using $ARGV[0] (What
exactly is this doing in this case) how does the compiler know what
file $file is? Should I set ARGC[0] equal to the filename in a line
above? When I run the code as you have shown it I get no output and no
changes are made to the text file.
Thanks so much for taking the time to help me out.
Joe
 
A

A. Sinan Unur

(e-mail address removed) wrote in @c13g2000cwb.googlegroups.com:
What you have written is EXACTLY what I am looking for!

Who is 'you'? What did he/she write? What are you responding to?
I'm assuming the print command on the last line is the command
that actually executes the changes above?

This statement is a tautology as the antecedent is false.
My question is by using $ARGV[0] (What exactly is this doing in
this case)

See

perldoc perlvar
how does the compiler know what file $file is?

That is a really philosophical question. I do not see any variable called
$file in the nonexistent code your are referring to.
Should I set ARGC[0] equal to the filename in a line above?

How would changing a line in your post accomplish anything?
When I run the code as you have shown it I get no output and no
changes are made to the text file.

That is to be expected when one runs non-existent code.

Sinan.
 
J

Joe Smith

Previous post:
open my $file, '<', $ARGV[0] or die "Could not open file. $!";
while (<$file>){
s/<(Ra|N\d)>//;
s!^(.\w\w)( \d+):(\d+)!$1/$2/$3!;
print;
}

I'm assuming the print command on the last line is the command
that actually executes the changes above?

No, it does not. The changes are performed by the s/// operators.
My question is by using $ARGV[0] (What exactly is this doing in this case)

@ARGV is a way of passing file names as command-line arguments.
As in `perl myprog.pl infile.txt`.
how does the compiler know what file $file is?

In the above code, $file is not a file and it is not a file name.
It is a file handle. The file handle is activated by open()
and utilized by said:
Should I set ARGC[0] equal to the filename in a line above?

No, you set it by specifying the filename on the command line.
If wish to hard-code the file name, use

my $filename = 'inputfile.txt';
open my $file, '<', $filename or die "Can't open $filename: $!\n";
When I run the code as you have shown it I get no output and no
changes are made to the text file.

Since you did not specify a filename on the command line, your
script is waiting for you to manually type in some lines of text.
It will continue to read from STDIN (your keyboard) until you
type the end-of-file character(s), such as Control-D or
Control-Z + Enter.

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top