I wanted to also follow-up explaining what each of the syntaxes you've used in your original post are actually doing. Since they each compile, and many do different things, some explanation is probably in order.

The first one:

my @sorted_animals = ( sort @animals ); print "@sorted_animals";

This sorts @animals. sort then populates a list, and assigning the list to @sorted_animals.

print sort "@animals";

@animals is interpolated inside double-quotes. But the double-quotes form a single string filled with the contents of @animals. So you're just sorting a single string, not a list of strings. sort only acts on lists, not on the contents of a string.

print sort @animals;

Here sort is sorting the contents of @animals, returning a list of the contents of @animals, in sorted order. Here, the $, list separator comes into effect, which defaults to no space, so that's what you get.

print sort(@animals);

Same as the previous example. The parantheses in this case just disambiguate the boundries of the sort function's parameters.

print sort("@animals");

This is the same as print sort "@animals";. No significant difference in this case.

print (sort("@animals"));

This is the same as print sort "@animals";. You've just disambiguated the boundries of the parameter lists for sort and print.

print (sort(@animals));

This is the same as print sort @animals; but with parenthesis around the parameter lists of sort and print. Again, this may lend clarity, and in some situations is a necessary way of bounding the parameter lists. But in this case is just a redundant syntax.

Just keep in mind, and array interpolated inside of a string gives you just a big string, not a lot of little ones.


Dave


In reply to Re: array sorting by davido
in thread array sorting 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.