Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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'

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2018-06-20 19:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.