in reply to RE: Writing the contents of an array to a file
in thread Writing the contents of an array to a file

print invokes list context:
[firewheel ~]$ perl my @list = (1 .. 4); print STDOUT @list;
I prefer something like print STDOUT "@list";, which is semantically equivalent to print STDOUT (join($", @array));

Fastolfe is correct, though. This is a scoping issue.

Replies are listed 'Best First'.
RE: RE: RE: Writing the contents of an array to a file
by Fastolfe (Vicar) on Sep 28, 2000 at 23:49 UTC
    FYI, $" is to print "@list" as $, is to print @list or print $a, $b, $c
    @a = qw{ one two }; @b = qw{ tre for }; { local $" = "-"; print "@a @b"; # one-two tre-for } { local $, = "-"; print @a, @b; # one-two-tre-for local $" = ":"; print "@a", "@b"; # one:two-tre:for }
    Cheers

      Note also that while each pair of lines of code below produce the same output:

      print FILE "@array"; print FILE join $", @array; print FILE @array; print FILE join $,, @array;
      they are not equivalent in terms of performance. Only print FILE @array avoids building a potentially huge string in memory before writing. In practice, the speed improvement is usually small (or even negative) but I prefer to allow for huge arrays and avoid the join if I'm just going to write the result to a file and then throw the constructed string away.

      Though I do find appealing the way that using join prevents your code from breaking if you accidentally sneak a scalar context into the mix (typing a "." when you meant a ",", for example).

              - tye (but my friends call me "Tye")