For completeness:

my @a = qw/foo bar quz/; print "----- 1 -----\n"; print @a; # "foobarquz" print "\n----- 2 -----\n"; print scalar @a; # "3" print "\n----- 3 -----\n"; print @a , "\n"; # "foobarquz" print "----- 4 -----\n"; print @a . "\n"; # "3" print "----- 5 -----\n"; print "@a"; # "foo bar quz" print "\n----- 6 -----\n"; $" = "&"; print "@a\n"; # "foo&bar&quz" print "----- 7 -----\n"; $, = "%"; print @a , "\n"; # "foo%bar%quz%" print "----- -----\n";
  1. @a is in list context and provides the arguments to print, which simply outputs them one after the other.
  2. @a is forced to scalar context, and an array in scalar context returns the number of its elements.
  3. The same as 1. except there's an additional argument, "\n", compare this to the next case.
  4. The dot operator (.) concatenates two strings, and here @a is forced into scalar context the same way the scalar function does in 2.
  5. String interpolation of an array is like doing join($", @a) (see perldata).
  6. Same as 5. except we've fiddled with $" (the list separator) for demonstration purposes.
  7. Here's an interesting one not yet mentioned: $, (the output field separator) is what print outputs between its arguments. The extra % at the end is because that's output between the last element of @a and the \n.

In reply to Re: Strange behaviour with Arrays and print? by Anonymous Monk
in thread Strange behaviour with Arrays and print? by pingufreak83

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.