Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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 ( [id://1103394]=note: print w/replies, xml ) Need Help??


in reply to Why is print output different for function returning an array versus printing an array variable?

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.
  • Comment on Re: 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^2: Why is print output different for function returning an array versus printing an array variable?
by Anonymous Monk on Oct 10, 2014 at 16:11 UTC
    You swapped the defaults.
Re^2: Why is print output different for function returning an array versus printing an array variable?
by aixtools (Novice) on Nov 06, 2014 at 14:00 UTC
    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!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1103394]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found