Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

#!/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?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2015-11-27 00:08 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (710 votes), past polls