Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Opening a text file and editing its contents

Reply
Thread Tools

Opening a text file and editing its contents

 
 
toomanyjoes@mail.utexas.edu
Guest
Posts: n/a
 
      01-14-2005
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

 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      01-14-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

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

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-14-2005
In article <(E-Mail Removed). com> posted
on Fri, 14 Jan 2005 11:28:23 -0800, toomanyjoes wrote:

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

--
A. Sinan Unur
(E-Mail Removed)d -- remove invalid and
reverse each component for email address.

 
Reply With Quote
 
thundergnat
Guest
Posts: n/a
 
      01-14-2005
(E-Mail Removed) wrote:
> 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 (<$file>){
>
>
> 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/<(Ra|N\d)>//;

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

 
Reply With Quote
 
toomanyjoes@mail.utexas.edu
Guest
Posts: n/a
 
      01-14-2005
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

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-14-2005
(E-Mail Removed) wrote in news:1105740122.984904.253900
@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.
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      01-16-2005
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;
>> }


(E-Mail Removed) wrote:
> 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 <$file>.

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding contents on yaml file without overwriting actual contents Kamarulnizam Rahim Ruby 4 01-28-2011 09:10 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
Load contents of a text file into a text box Jeremy Chapman ASP .Net 1 08-15-2003 10:10 PM



Advertisments