For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2006 > file io









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author file io
Blah

2006-02-22, 9:55 pm

I have to subroutines for both reading out and reading in from a txt
file to a 15 unit array


sub load
{
open SAVEGAME, "E:/savegame.txt" or die $!;
@board = <SAVEGAME>;
printboard();
}

sub save
{
open SAVEGAME, ">E:/savegame.txt" or die $!;
<SAVEGAME> = @board;
move();
}

the first one seems to work okay even though the data doesn't print out
like its supposed to when printboard is called

The second one gives me errors. Any reason why reversing the assignment
won't work?

Marcel

2006-02-23, 3:55 am

I am not sure if "<SAVEGAME> = @board; " is supposed to work, but I
would write the data to savegame.txt like this:

open (SAVEGAME, ">E:/savegame.txt") or die "$!";
print SAVEGAME $_ foreach (@board);
close (SAVEGAME);

Paul Lalli

2006-02-23, 7:55 am

Blah wrote:
> I have to subroutines for both reading out and reading in from a txt
> file to a 15 unit array
>
>
> sub load
> {
> open SAVEGAME, "E:/savegame.txt" or die $!;
> @board = <SAVEGAME>;
> printboard();
> }
>
> sub save
> {
> open SAVEGAME, ">E:/savegame.txt" or die $!;
> <SAVEGAME> = @board;
> move();
> }
>
> the first one seems to work okay even though the data doesn't print out
> like its supposed to when printboard is called


Then you have an odd definition of "work okay".

> The second one gives me errors.


And you don't think that maybe the text of those errors might be
relevant?

> Any reason why reversing the assignment won't work?


Because as a great man once said, "You can't just make s*** up and
expect it to work." Where did you get the idea that such a syntax was
valid?

< > is the readline operator. It reads a line from the enclosed
filehandle in scalar context, or all lines in list context.

= is the assignment operator. It stores the values on the right side
into the variable on the left side.

You are attempting to store the values in @board into the result of the
readline operator. That is non-sensical.

If you want to store values in a file, you print to that file, just
like you print to the console:

print SAVEGAME @board;

I suggest you start reading some documentation, rather than just making
it up as you go:

perldoc perlopentut
perldoc -f print

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com