Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dumping a complete array in a file?

Status
Not open for further replies.

Tve

Programmer
May 22, 2000
166
FR
I use perl mainly for opening, closing and reading files...

My question is about writing to a file.

In other languages, it is possible to dump a complete array in a file. Translated to perl, it could look like this

@test = qw(a, b, c, d, e, f, g, h, i, j, k, l, m);
open (OUTFILE,">c:/test.txt") or die "Error $f_file: $!";
dump ( OUTFILE,@test);


I know I could make a loop and print to the file, like:

foreach $line (@test) {
print OUTFILE "$line";
}


but this seems very cumbersom.

Does anyone know if something like the first syntax(blue) exists?

Thierry



 
you can just print the array to the filehandle.

open (FILE, ">txt.txt");
print FILE @array;
close FILE;

will dump the array into the file.
but, it doesn't separate the items in the array.

so with the array from your example, you would end up with a line like this:

abcdefghijklm.

if you want each one delimited by something you can always join() the array with whatever delimeter you like.

 
Thanks for your tip.

The main advantage of the array is that the file to which I write is only open for a split second.

Thierry
 
Isn't there also a variable $@ or something that is the array seperator, that is it prints whatever this is set to between each element of the array?

Couldn't you set this to "\n" and print the filehandle to get one record / line?

Mike B.
Go Wolfpack!
 
yeah, i forgot all about that.

its $"

you can set this to be the delimter of a list when printed.

$@ however is the special variable for the last eval error.
 
don't you have to evaluate an array in double quotes to make use of $" ?

like this:

print FILE "@array";

I think that join() is quicker, but this is *real* easy.
Mike
michael.j.lacey@ntlworld.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top