Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Question about concatenation

by chiggly007 (Initiate)
on Dec 23, 2012 at 20:13 UTC ( #1010116=perlquestion: print w/ replies, xml ) Need Help??
chiggly007 has asked for the wisdom of the Perl Monks concerning the following question:

I have a general question about concatenation. I was under they impression that I could concatenate using a . , etc. I'm confused why this wont work:
print "Counting up: " . (1..6) . "\n";
but instead this works:
print "Counting up: " , (1..6) , "\n";

Comment on Question about concatenation
Select or Download Code
Re: Question about concatenation
by LanX (Canon) on Dec 23, 2012 at 20:21 UTC
    the dot . is a scalar operator expecting something convertible to a string.

    from perlop: Binary "." concatenates two strings.

    print operates on lists and the comma is the necessary list operator to delimit the elements.

    try

    print "Counting up: " . join ( "", 1..6 ) . "\n";

    or

    @arr=1..6; print "Counting up: @arr\n";

    Cheers Rolf

Re: Question about concatenation
by toolic (Chancellor) on Dec 23, 2012 at 20:24 UTC
Re: Question about concatenation
by eyepopslikeamosquito (Canon) on Dec 23, 2012 at 20:27 UTC

    The difference is because the range operator behaves differently in list context to scalar context. BTW, so do arrays. As indicated in the test program below, the array returns the number of elements in scalar context, while the flip flop operator returns false (represented as undef), which prints as an empty string. Running:

    use strict; use warnings; print "Counting up: " . (1..6) . "\n"; # scalar context (undef) print "Counting up: " , (1..6) , "\n"; # list context print "Counting up: @{[1..6]}\n"; # list context, space bet +ween each element my @one_to_six = 1..6; print "Counting up: " . @one_to_six . "\n"; # scalar context (no. of +elts) print "Counting up: " , @one_to_six , "\n"; # list context
    produces:
    Use of uninitialized value $. in range (or flip) at cnt.pl line 4. Counting up: Counting up: 123456 Counting up: 1 2 3 4 5 6 Counting up: 6 Counting up: 123456

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2014-10-26 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls