It looks like the elements in your array must already end in newlines (and have no newlines otherwise). That may be a valid assumption for your situation, but if you have more complicated data you want to recover, you may want to use something like Data::Dumper instead.

If all you're doing is printing an array to a file, you can do it without a for loop, which seems to be quite a bit faster on my laptop using this benchmark code:

use Benchmark 'cmpthese'; my @a = map {"$_\n"} 1..1000000; cmpthese(100, { forloop => sub { open FILE, ">file.out" or die "Can't open file: $!\n"; print FILE $_ for @a; close FILE; }, array => sub { open FILE, ">file.out" or die "Can't open file: $!\n"; print FILE @a; close FILE; } });
Results:
Benchmark: timing 100 iterations of array, forloop... array: 28 wallclock secs (22.62 usr + 2.74 sys = 25.36 CPU) @ 3 +.94/s (n=100) forloop: 69 wallclock secs (61.52 usr + 2.75 sys = 64.27 CPU) @ 1 +.56/s (n=100) Rate forloop array forloop 1.56/s -- -61% array 3.94/s 153% --

-- Mike

--
just,my${.02}


In reply to Re: Printing arrays to a file. by thelenm
in thread Printing arrays to a file. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.