Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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'

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 exploiting the Monastery: (3)
As of 2016-08-31 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (426 votes). Check out past polls.