Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Syntactic Confectionery Delight
 
PerlMonks  

Join function

by truthseeker66 (Novice)
on Oct 09, 2012 at 20:25 UTC ( #998064=perlquestion: print w/ replies, xml ) Need Help??
truthseeker66 has asked for the wisdom of the Perl Monks concerning the following question:

When I execute this I expect see
'five-@-four-@-one-@-three-@-two'

#!/usr/bin/perl use strict; use warnings; my@array = qw(one two three four five); print "The original array contains @array \n\n"; my$string = join ("-@-", sort@array); print "The string contains $string \n";

But why do I see?
'The string contains five-four-one-three-two'

Comment on Join function
Download Code
Re: Join function
by toolic (Chancellor) on Oct 09, 2012 at 20:29 UTC
    Double quotes interpolate variable values, and @- is a special variable. Use single quotes instead:
    my $string = join ('-@-', sort@array); # My output # The string contains five-@-four-@-one-@-three-@-two

    UPDATE: Here is a quote from perlop which I think is a handy bit of trivia:

    "Punctuation" arrays such as @* are usually interpolated only if the name is enclosed in braces @{*}, but the arrays @_ , @+ , and @- are interpolated even without braces.
      yes, it works. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998064]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2014-04-16 20:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls