aixtools has asked for the wisdom of the Perl Monks concerning the following question:

I took an example from the internet as a start on working with arrays and sort. I am surprised by the different output of
print sort(@myarray);
and
@sarray = sort (@myarray); print @sarray;
Example
#!/usr/bin/perl # AN ARRAY @coins = ("Quarter","Dime","Nickel"); print "@coins"; print "\n\r"; # ADD ELEMENTS push(@coins, "Penny"); print "@coins"; print "\n\r"; unshift(@coins, "Dollar"); print "@coins"; print "\n\r"; @scoins = sort(@coins); print sort(@coins); print "\n\r"; print "@scoins"; print "\n\r"; # REMOVE ELEMENTS pop(@coins); print "@coins"; print "\n\r"; # BACK TO HOW IT WAS shift(@coins); print "@coins"; print "\n\r";
Output
Quarter Dime Nickel Quarter Dime Nickel Penny Dollar Quarter Dime Nickel Penny DimeDollarNickelPennyQuarter Dime Dollar Nickel Penny Quarter Dollar Quarter Dime Nickel Quarter Dime Nickel
p.s. the code blocks look really small in my preview. Hope this is normal.
  • Comment on Why is print output different for function returning an array versus printing an array variable?
  • Select or Download Code

Replies are listed 'Best First'.
Re: Why is print output different for function returning an array versus printing an array variable?
by choroba (Cardinal) on Oct 10, 2014 at 11:07 UTC
    Hi aixtools, welcome to the Monastery!

    The difference is the double quotes. When interpolating an array in double quotes, Perl separates the elements with $" which is a space by default. Update: Without quotes, elements are separated by $, which is empty by default.

    To change your font, check your settings (especially Large Code Font at Display Settings).

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Why is print output different for function returning an array versus printing an array variable?
by BrowserUk (Patriarch) on Oct 10, 2014 at 11:11 UTC

    Because: print sort(@coins);

    Uses $, (also known as $OUTPUT_FIELD_SEPARATOR (default value ' ' (space)) to separate items printed from a list. (The list returned by sort.)

    Whereas: print "@coins";

    Forces the array to be concatenated into a single string using $", (also known as $LIST_SEPARATOR, default value '' (null)).

    Hence the former has spaces, but the latter has none.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      You swapped the defaults.
      michael@x054:[/data/prj/labserv/perftools/perl]cat *2.pl @coins = ("Quarter","Dime","Nickel"); print "@coins"; print "\n\r"; print sort(@coins); print "\n\r"; michael@x054:[/data/prj/labserv/perftools/perl]perl *2.pl Quarter Dime Nickel DimeNickelQuarter

      Confused again. I seem to getting the opposite of what you said should happen. "@coins" is separated while sort(@coins) is not.

        I added one print statement - and now I understand the very literal impact of having an array in/not-in a string context.

        @coins = ("Quarter","Dime","Nickel"); print "@coins"; print "\n\r"; print @coins; print "\n\r"; print sort(@coins); print "\n\r"; Quarter Dime Nickel QuarterDimeNickel DimeNickelQuarter
        Again, thanks!!
Re: Why is print output different for function returning an array versus printing an array variable?
by Discipulus (Canon) on Oct 10, 2014 at 11:59 UTC
    see also the only Perl novel i know about: Perl White Magic

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Thanks all. This will help moving forward. Seems I located a poor example as far as formatting is concerned.
Re: Why is print output different for function returning an array versus printing an array variable?
by hippo (Archbishop) on Oct 10, 2014 at 11:16 UTC

    Simply, context and interpolation.

    print sort(@coins); # List context print "@scoins"; # Scalar context which uses the list separator in +interpolation of the array

    See responses in this thread and the section on $LIST_SEPARATOR in perlvar for more info.