473,395 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Q: What's wrong with this?

Hi,

This is related to my previous post but I made it much simpler...

open (INFILE, "test.txt");
while (<>) {
print $_;
s/one/two/g;
print $_;
}

I really cannot think now - why are the contents of test.txt not being
changed?
print$_ output gives me
one
two
one
two

but cat test.txt is still:
one
one

Thanks
Jul 19 '05 #1
11 2901
"Troll" <ab***@microsoft.com> tried to express:

open (INFILE, "test.txt");
while (<>) {
print $_;
s/one/two/g;
print $_;
}

I really cannot think now - why are the contents of test.txt not being
changed?
print$_ output gives me
one
two
one
two

but cat test.txt is still:
one
one


because you are in no way writing to the file. You are simply reading
it and processing the data that you read. You would have to write the
changed data back out to a new file after it is processed like with:

perl myscript > test.txt.changed
Jul 19 '05 #2

"Wiseguy" <no***@celeron.local> wrote in message
news:3f********@127.0.0.1...
"Troll" <ab***@microsoft.com> tried to express:

open (INFILE, "test.txt");
while (<>) {
print $_;
s/one/two/g;
print $_;
}

I really cannot think now - why are the contents of test.txt not being
changed?
print$_ output gives me
one
two
one
two

but cat test.txt is still:
one
one


because you are in no way writing to the file. You are simply reading
it and processing the data that you read. You would have to write the
changed data back out to a new file after it is processed like with:

perl myscript > test.txt.changed


What if....
I have corrected my opener to accept writing to the file:
open (INFILE, "+<test.txt");
while (<>) {
s/one/two/g;
print INFILE $_;
}

but still it's playing up.

If the contents of test.txt are:
one

then the new file test.txt becomes:
one
one
two

Any hints? I'm going round in circles it seems...


Jul 19 '05 #3
Troll wrote:
open (INFILE, "test.txt");
while (<>) {
print $_;
s/one/two/g;
print $_;
}

I really cannot think now - why are the contents of test.txt not being
changed?


Why should it be changed?
You are not writing to the file anywhere in your code, so of course the file
on the hard drive remains unchanged.
It would be a terrible thing if Perl would alter the content of a file while
reading it.

Maybe you are looking for the answer of PerlFAQ5:
"How do I change one line in a file/delete a line in a file/insert a
line in the middle of a file/append to the beginning of a file?"

jue
Jul 19 '05 #4
Troll wrote:
"Wiseguy" <no***@celeron.local> wrote in message
news:3f********@127.0.0.1...
"Troll" <ab***@microsoft.com> tried to express: I have corrected my opener to accept writing to the file:
open (INFILE, "+<test.txt");


You should have read the docs, too. About using "+" it says (among other
things):
[...] You can't usually use either
read-write mode for updating textfiles, since they have variable
length records.
while (<>) {
s/one/two/g;
print INFILE $_;
}

but still it's playing up.

If the contents of test.txt are:
one

then the new file test.txt becomes:
one
one
two
I am surprised that you are getting any meaningful results at all.
This random mix of reading and writing without any consious repositioning of
the file pointer can only result in chaos eventually.
Any hints? I'm going round in circles it seems...


Yes, Read The Fine Manual.
And maybe get some basic understanding about how files work. This has
nothing to do with Perl but is general programming 101.

jue
Jul 19 '05 #5

"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:vS*****************@nwrddc03.gnilink.net...
Troll wrote:
"Wiseguy" <no***@celeron.local> wrote in message
news:3f********@127.0.0.1...
"Troll" <ab***@microsoft.com> tried to express: I have corrected my opener to accept writing to the file:
open (INFILE, "+<test.txt");


You should have read the docs, too. About using "+" it says (among other
things):
[...] You can't usually use either
read-write mode for updating textfiles, since they have

variable length records.
while (<>) {
s/one/two/g;
print INFILE $_;
}

but still it's playing up.

If the contents of test.txt are:
one

then the new file test.txt becomes:
one
one
two
I am surprised that you are getting any meaningful results at all.
This random mix of reading and writing without any consious repositioning

of the file pointer can only result in chaos eventually.
Any hints? I'm going round in circles it seems...


Yes, Read The Fine Manual.
And maybe get some basic understanding about how files work. This has
nothing to do with Perl but is general programming 101.

jue


Thanks. I guess I'm not the only one surprised. I'd love to read the manual
in depth but have no time atm. :(
That's why I'm posting here so much. Is there a really silly reason for the
script failure [other that not having read the manual]?

Also, which FAQ did u refer to? http://www.faqs.org/faqs/perl-faq/ ? If so,
I could not find what I wanted in part5. Or did I miss it?

Jul 19 '05 #6
Troll wrote:
Thanks. I guess I'm not the only one surprised. I'd love to read the
manual in depth but have no time atm. :(
That's why I'm posting here so much. Is there a really silly reason
for the script failure [other that not having read the manual]?
Listen, you are using the wrong tool!
It's like you are trying to use screwdriver to hammer a nail into a wall.
The docs explain that that is not a good idea and that you should use a
hammer instead.

Side note: yes, you can read and write to the same file at the same time,
but it requires MUCH(!) more knowledge, work, and diligence, and it works
completely different then you seem to believe.
Also, which FAQ did u refer to? http://www.faqs.org/faqs/perl-faq/ ?
If so, I could not find what I wanted in part5. Or did I miss it?


I don't know who this guy U is, but I was refering to "perldoc -q middle"
which will take you right to the relevant answer.

jue
Jul 19 '05 #7

"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:YA*****************@nwrddc02.gnilink.net...
Troll wrote:
Thanks. I guess I'm not the only one surprised. I'd love to read the
manual in depth but have no time atm. :(
That's why I'm posting here so much. Is there a really silly reason
for the script failure [other that not having read the manual]?


Listen, you are using the wrong tool!
It's like you are trying to use screwdriver to hammer a nail into a wall.
The docs explain that that is not a good idea and that you should use a
hammer instead.

Side note: yes, you can read and write to the same file at the same time,
but it requires MUCH(!) more knowledge, work, and diligence, and it works
completely different then you seem to believe.
Also, which FAQ did u refer to? http://www.faqs.org/faqs/perl-faq/ ?
If so, I could not find what I wanted in part5. Or did I miss it?


I don't know who this guy U is, but I was refering to "perldoc -q middle"
which will take you right to the relevant answer.

jue


Sorry Jue. I just got into perldoc. Do I enter the whole "perldoc -q middle"
into the search field or just a part of it? Not sure what the -q means.
Never been there b4...
Jul 19 '05 #8
Troll wrote:
Sorry Jue. I just got into perldoc. Do I enter the whole "perldoc -q
middle"
Yes (well, without the quotes)
into the search field or just a part of it?
Search field? What search field? You enter it at the command line.
Not sure what the
-q means. Never been there b4...


Reply from "perldoc" when called without any arguments:
The -h option prints more help. Also try "perldoc perldoc" to get
acquainted with the system.

And from "perldoc perldoc":
[...]
OPTIONS
-h help
Prints out a brief help message.
[...]
-q perlfaq
The -q option takes a regular expression as an argument. It will
search the question headings in perlfaq[1-9] and print the entries
matching the regular expression.

jue
Jul 19 '05 #9

"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:Mi*****************@nwrddc02.gnilink.net...
Troll wrote:
Sorry Jue. I just got into perldoc. Do I enter the whole "perldoc -q
middle"
Yes (well, without the quotes)
into the search field or just a part of it?


Search field? What search field? You enter it at the command line.
Not sure what the
-q means. Never been there b4...


Reply from "perldoc" when called without any arguments:
The -h option prints more help. Also try "perldoc perldoc" to get
acquainted with the system.

And from "perldoc perldoc":
[...]
OPTIONS
-h help
Prints out a brief help message.
[...]
-q perlfaq
The -q option takes a regular expression as an argument. It will
search the question headings in perlfaq[1-9] and print the

entries matching the regular expression.

jue


Ohhhh. I was looking at perldoc online. Now, why would any1 do that if it
can be run from the command line? Don't ask me. It was just a natural
reaction.

The only drama I have is that Perl 5.8.0 is not available to me so I cannot
use the Tie::File module.
As such I'm trying to go with the solution proposed in
http://www.perldoc.com/perl5.005_03/...ing-of-a-file-

but I'm getting the following errors:
Global symbol $file requies explicit package name at ./
Global symbol $old requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $new requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $bak requies explicit package name at ./
etc
My script begins with:
******************
....
use strict;
$file = test.html;

$old = $file;
$new = "$file.tmp.$$";
$bak = "$file.bak";
etc


Jul 19 '05 #10
Troll wrote:
The only drama I have is that Perl 5.8.0 is not available to me so I
Why not? You can get it for free from e.g. CPAN
cannot use the Tie::File module.
I am not aware that Tie::File would require 5.8, but I may be wrong.
As such I'm trying to go with the solution proposed in
http://www.perldoc.com/perl5.005_03/...ing-of-a-file-
but I'm getting the following errors:
Global symbol $file requies explicit package name at ./
Global symbol $old requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $new requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $bak requies explicit package name at ./
etc
My script begins with:
******************
...
use strict;
$file = test.html;
Probably you meant
my $file = 'test.html';
$old = $file;
$new = "$file.tmp.$$";
$bak = "$file.bak";


Probably you meant
my $old = $file;
my $new = "$file.tmp.$$";
my $bak = "$file.bak";

jue
Jul 19 '05 #11

"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:HP****************@nwrddc02.gnilink.net...
Troll wrote:
The only drama I have is that Perl 5.8.0 is not available to me so I


Why not? You can get it for free from e.g. CPAN
cannot use the Tie::File module.


I am not aware that Tie::File would require 5.8, but I may be wrong.
As such I'm trying to go with the solution proposed in

http://www.perldoc.com/perl5.005_03/...ing-of-a-file-

but I'm getting the following errors:
Global symbol $file requies explicit package name at ./
Global symbol $old requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $new requies explicit package name at ./
Global symbol $file requies explicit package name at ./
Global symbol $bak requies explicit package name at ./
etc
My script begins with:
******************
...
use strict;
$file = test.html;


Probably you meant
my $file = 'test.html';
$old = $file;
$new = "$file.tmp.$$";
$bak = "$file.bak";


Probably you meant
my $old = $file;
my $new = "$file.tmp.$$";
my $bak = "$file.bak";

jue


Actually yes. Thanks. I got the same result by initialising the variables
but the 'my' option is better.
I'm unable to use the latest Perl distro as the sysadmins have not updated
to it. I forgot which ver they are running but it sure ain't one of the
recent ones.

With your guidance, I finally got the result I wanted - now it's just a case
of putting my own RegExes in there. Someone had suggested that I use the
"+<test.txt" and that's how the whole problem started. I was running around
like a headless chook not really knowing what I'm looking for.

Thanks very much for sticking with me on this one. Greatly appreciated :c)

Jul 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
24
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
2
by: mingke | last post by:
Hi... So I have problem with my if condition..I don't know what's wrong but it keeps resulting the wrong answer.... So here's the part of my code I have problem with: for (i=0; i<size2;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.