On Mon, 4 Jun 2001, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote,
> Date: Mon, 4 Jun 2001 19:36:13 -0500 (CDT)
> From: [EMAIL PROTECTED]
> To: Perl Discuss <[EMAIL PROTECTED]>
> Subject: creating @array
>
> open(FILE, ">>$filename");
> print FILE "$add\@$domain\t$destination_email\n";
> while (<FILE>) {
> chomp $_;
> push(@entries, $_);
> }
> close(FILE);
>
> i have a file that i would like to put each of its lines into an array as
> seperate elements. the @entries array does not exist before being called
> in the push statement in this while loop. as a result, i think that none
> of the file's contents are being put into the array.
The >> put the file in append mode, so you can, well, append content
to it. So the "print FILE ...." statement makes sense but the while
loop doesn't. You need the < for reading the file, or just leave the
filename alone since reading is the default mode when opening a file.
And you should check the status of the operation. $! contains error
if for some reason you fail to open the file.
open FILE, $filename or die "failed to open file because $!\n";
# it's the same as: open FILE, "<$filename" or ...
> is this a valid conclusion? must an @array have some sort of content
> before something can be pushed into it? and if so, how can i "touch" an
> array so that i can push stuff into it?
No, the array is initialized for you so you don't need to fill before
push()ing something into it. But it's highly recommended that you
"touch" any variable before using it by declaring using my(). And use
strict near the top of your script.
Oh, you don't actually need to loop over the lines and push them
one by one just to get the whole content. You can assign them all
directly.
#!/usr/bin/perl -w
use strict;
open FILE, 'filename' or die "failed to open filename: $!\n";
my @entries = <FILE>;
close FILE;
And btw, if you really need to read and write the file in the same
time, you need to use +<.
perldoc -f open
will tell you more about open.
hth
s.a.n
--
Hasanuddin Tamir: [EMAIL PROTECTED] - Trabas: www.trabas.com