in reply to array sorting

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