Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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
Replies are listed 'Best First'.
Re: Join function
by toolic (Bishop) 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 lurking in the Monastery: (12)
As of 2015-07-07 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls