Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Opening a text file and editing its contents (http://www.velocityreviews.com/forums/t890188-opening-a-text-file-and-editing-its-contents.html)

toomanyjoes@mail.utexas.edu 01-14-2005 07:28 PM

Opening a text file and editing its contents
 
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


Sherm Pendley 01-14-2005 07:46 PM

Re: Opening a text file and editing its contents
 
toomanyjoes@mail.utexas.edu 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

A. Sinan Unur 01-14-2005 07:51 PM

Re: Opening a text file and editing its contents
 
In article <1105730903.248958.95440@z14g2000cwz.googlegroups. 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
usa1@llenroc.ude.invalid -- remove invalid and
reverse each component for email address.


thundergnat 01-14-2005 08:22 PM

Re: Opening a text file and editing its contents
 
toomanyjoes@mail.utexas.edu 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;
}


toomanyjoes@mail.utexas.edu 01-14-2005 10:02 PM

Re: Opening a text file and editing its contents
 
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. Sinan Unur 01-14-2005 11:47 PM

Re: Opening a text file and editing its contents
 
toomanyjoes@mail.utexas.edu 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.

Joe Smith 01-16-2005 12:00 PM

Re: Opening a text file and editing its contents
 
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;
>> }


toomanyjoes@mail.utexas.edu 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


All times are GMT. The time now is 11:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.