How to update entries in a file

K

ko

John said:
open DATA, ">>test.xml"; # append to file


You should always, yes *always*, check the return value from open():

open DATA, '>>test.xml' or die "could not open 'test.xml' $!";


print DATA @lines [0]; # print the first element in the array


You should enable warnings when developing Perl code!


while <$addr> { # this is the input


That is not Perl code. You are wasting the time of *thousands* of
people around the world because you cannot be troubled to
provide actual code!

That's it. You've used up all your coupons.

So long.

[snip explanation of hoework specs, etc]
So pls feel free to flame me when my search for clues is wasting your time
cause the code examples I provide are irrelevant but spare yourself from
judging the way in which I do my search. There will be those who support the
Monarchy and others who support the Republic. I'm probably somewhere in the
middle.

It may be hard to sort out all the advice offered when it seems like
you're getting flamed solely for this reason, but you're not helping
yourself. What I mean is that people have offered *good* advice, which
has been ignored in your *posted* code.
while <$addr> { # reads input from client
^^^^^^^^^^^^^^^^

Here's an example. As Tad pointed out in your quoted text, this is not
Perl code. It won't compile, try it. I'm assuming you have read the
posting guidelines (http://mail.augustmail.com/~tadmc/clpmisc.shtml), so
try and put yourself in the shoes of the regulars/experts. They are busy
people and take time every day to offer advice to many people. Its only
natural that you may get flamed or ignored if you can't at least show
some kind of effort - at the very least post code that compiles.
Granted, everyone makes mistakes, but in this case you did it at least
twice, even after someone took the time to point it out. Actually, its
obvious that instead of copy/paste, you're typing in your code snippets:
print $_; # this prints to the screen the
first line of the file passed by the client
@lines <$addr>; # I want this array to capture the whole
file passed from the client
}
fileupdate (); # calling function
************

Q: Why isn't line 1 passed into the array?
print $_ echoes line 1 on the server side [so we know it's been received]
and 'cat FILE_B' only shows:
line 2
line 3

The above code won't pass anything into the array because, as is, it
won't compile. Since you're gettting results the code you are running
must be different than what was posted above. Your code is probably
something like this:

while (<$addr>) {
print $_;
@lines = <$addr>;
...
}

Which, if you wanted to assign all lines in the file to an array, should
be done like this:

@lines = <$addr>;

'perldoc perlop', - 'I/O Operators' for details.
Everything was working fine when I only had this:
************
while <$addr> {
open DATA, ">>test.xml" or die "Could not open test.xml $!\n";
print DATA $_;
close DATA;
}
************

And this won't compile either.

And none of what has been written is meant to be critical. What you need
to understand is that the people helping you can't read your mind, and
that no one can help you if you don't first help yourself (use warnings,
use strict, etc). Once again, put yourself in the shoes of the regulars.
Would you want/be able to sort through a bunch of incomprehensible
code that doesn't even compile? Its expected that you put some kind of
effort in what you post *before* you post, and more simply, just common
sense/courtesy. On a more positive note, I agree with your opinion that
everyone learns differently - I think its hard for some people to
remember what its like learning your first programming language :)

HTH - keith
 
M

Martien Verbruggen

[snip loads of justifications in which I am not interested]
while <$addr> { # reads input from client

Did you actually read what Tad wrote?

make sure you provide _real_ Perl code. It is rather disrespectful of
you to expect the people here to do the work of the Perl parser and
compiler for you, when you have a computer that can do it for you.

Once you have code that actually compiles, and utilises the strict and
warnings pragmas, feel free to come back. Until then, especially since
this is homework, work on it.
print $_; # this prints to the screen the
first line of the file passed by the client

You don't need the $_ there. You should make sure that your comments
don't wrap, resulting in more invalid code.
@lines <$addr>; # I want this array to capture the whole
file passed from the client

This does not even look vaguely like Perl code.
fileupdate (); # calling function
************

Q: Why isn't line 1 passed into the array?

You ave no code to run, so that's why nothing happens.
print $_ echoes line 1 on the server side [so we know it's been received]

I doubt it, sincerely. The stuff you posted does not compile, so the
print has never been executed.
Everything was working fine when I only had this:
************
while <$addr> {

You are not telling us the truth. Not even after Tad told you to make
sure to submit your real code.

I am not at all surprised that Tad is no longer willing to assist you.

Martien
 
T

Tad McClellan

John said:
For those of us who write books, not everything is covered in them


But everything _is_ covered for those of us that don't write books?

What relevance does authorship have?

So pls feel free to flame me when my search for clues is wasting your time


Your search for clues is wasting our time!

cause the code examples I provide are irrelevant


It isn't the irrelevancy, it is the It's Not Perl Code in the first place.

Everything was working fine when I only had this:


I doubt that that is an accurate statement.

while <$addr> {


Looks like you cannot be troubled to provide actual code even when
asked to provide actual code.

Sheesh!
 
J

John

[snipped]
Looks like you cannot be troubled to provide actual code even when
asked to provide actual code.

Sheesh!


I guess it was my inexperienced judgement which allowed me to think that
whatever I was providing was sufficient.
Since I'm coding on a different PC and posting from a different one, it was
not as easy as just doing a copy/paste.
So yes, I was trying to avoid a situation where I'm typing everything in. In
hindsight, that may not have been the correct decision.

Actual server.pl code follows. It is now working and it saves the data
passed from client.pl into test.xml.
***************
#!/usr/bin/perl -w

use strict;
use IO::Socket;

my ($server);
my (@lines);

sub fileupdate {
open TEST, "test.xml" or die "Could not open test.xml $!\n";
print DATA @lines;
print DATA "\n";
close DATA;
}

$server = new IO::Socket::INET {
LocalAddr => 'localhost:9000',
Proto => 'tcp',
Reuse => 1,
) or die "Can't setup server $!\n";

$server->listen();
$server->autoflush(1);
print "Server is waiting for data...\n";

my $data;
while ($data = $server->accept()) {
print "Connected from: ", $data->peerhost();
print " on port: ", $data->peerport(), "\n";
while <$data> { # this line has been removed - it is here for
illustration of the old code only
@lines = <$data>;
} # this line has been removed - it is
here for illustration of the old code only
fileupdate();
print "Connection closed\n";
close $data;

print "Waiting for more data...\n";
}
************
The
while <$data> {
is the old
while <$addr> {
which was giving me grief as line 1 of input received was going into $_ and
not into the @lines array as intended.


Does the above make more sense now? Do I need to post the client.pl script
as well?
Once again, apologies for the hassles.
 
T

Tad McClellan

John said:
Actual server.pl code follows.
^^^^^^

I doubt that.

It is now working


Liars never prosper.

sub fileupdate {
open TEST, "test.xml" or die "Could not open test.xml $!\n"; ^^^^
print DATA @lines; ^^^^
print DATA "\n"; ^^^^
close DATA; ^^^^
}


One of these things is not like the other, one of these things
just isn't the same...

(and you are opening for input rather than for output...)

$server = new IO::Socket::INET { ^
^
LocalAddr => 'localhost:9000',
Proto => 'tcp',
Reuse => 1,
) or die "Can't setup server $!\n";
^
^

One of these things is not like the other, one of these things
just isn't the same...

Once again, apologies for the hassles.


Yeah, right.
 
J

John

Tad McClellan said:
^^^^^^

I doubt that.




Liars never prosper.




One of these things is not like the other, one of these things
just isn't the same...

(and you are opening for input rather than for output...)


^
^

One of these things is not like the other, one of these things
just isn't the same...




Yeah, right.

Thanks for pointing out the errors.
As expected, there were typos when I re-typed the code here. I'm sure you
had never done that, right?
Well, I guess some of us ain't perfect.
Anyway, once again with the corrections:
************************
#!/usr/bin/perl -w

use strict;
use IO::Socket;

my ($server);
my (@lines);

sub fileupdate {
open TEST, ">>test.xml" or die "Could not open test.xml $!\n";
print TEST @lines;
print TEST "\n";
close TEST;
}

$server = new IO::Socket::INET (
LocalAddr => 'localhost:9000',
Proto => 'tcp',
Reuse => 1,
) or die "Can't setup server $!\n";

$server->listen();
$server->autoflush(1);
print "Server is waiting for data...\n";

my $data;
while ($data = $server->accept()) {
print "Connected from: ", $data->peerhost();
print " on port: ", $data->peerport(), "\n";
@lines = <$data>;
fileupdate();
print "Connection closed\n";
close $data;

print "Waiting for more data...\n";
}


Feel free to point out any errors you find. After all, I am here to learn.
Having said that, I see this thread coming to an end. I have achieved what I
wanted for now.
I'm sure there'll be more Qs coming.

BTW, in order for me to be a liar you have to prove intent. So until you do
that you have no case. :)
Ohh, and another thing: I will not lose any sleep if you don't feel like
accepting my apologies.
Stay tuned - I'm sure I'll give you plenty of opportunities to have a go at
me later on.
l8r
 
H

Helgi Briem

Thanks for pointing out the errors.
As expected, there were typos when I re-typed the code here. I'm sure you
had never done that, right?

You have been told several times to "copy and paste" code,
not retype it. It's not a joke.

<plonk>
 

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

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top