sneaky has asked for the wisdom of the Perl Monks concerning the following question:

Hello, currently in learning and I'm following this tutorial: http://www.perl-begin.org/tutorials/perl-for-newbies/part1/ At the bottom of the page there is the following example:
print "Test 4:\n"; print join("\n", (("hello") x 5)), "\n\n"; print "Test 5:\n"; print join("\n", ("hello" x 5)), "\n\n"; >> Test 4: hello hello hello hello hello Test 5: hellohellohellohellohello
I do not understand what is happening here. In my mind these should behave the opposite. I have also tried:
print ("hello" x 5); print "\n"; print (("hello") x 5); >> hellohellohellohellohello hellohellohellohellohello
So can someone please help me understand the difference in the join operations above?

Replies are listed 'Best First'.
Re: Join with x operator
by hippo (Archbishop) on Jul 09, 2014 at 09:10 UTC

    (("hello") x 5)) is a list with 5 elements, each of which is a string with value "hello". Conversely, ("hello" x 5) is a list with one element, specifically a string with value "hellohellohellohellohello". So, your first join joins a list with 5 elements which is why you see all the newlines whereas your second joins a list with just one element. HTH.

Re: Join with x operator
by johngg (Canon) on Jul 09, 2014 at 09:13 UTC

    The x operator can act as a list multiplier as well as a string multiplier. The parentheses around ("hello") make it a list and the x 5 turns the statement into

    print join("\n", ("hello", "hello", "hello", "hello", "hello")), "\n\n +";

    I hope that makes things clearer.

    Cheers,

    JohnGG

Re: Join with x operator
by thanos1983 (Parson) on Jul 09, 2014 at 15:16 UTC

    Hello sneaky,

    As everyone explained the difference lies on the parenthesis, on how you use it. It can have different affect on your script.

    Since I am also relatively new to Perl I am also a more visual guy that a reader. So most of the time I create small scripts to help me understand how they operate.

    This is a sample of code that simply puts numbers on the strings as process repeats its self through the x (multiplier). This shirt example maybe will help you to observe what happens on the script with different parenthesis.

    #!/usr/bin/perl use strict; use warnings; print "Test 4:"; foreach $_ (0 .. 5) { print join("\n", (("hello[$_]") x $_)), "\n\n"; } print "Test 5:"; foreach $_ (0 .. 5) { print join("\n", ("hello[$_]" x $_)), "\n"; }

    Output of the code:

    Test 4: hello[1] hello[2] hello[2] hello[3] hello[3] hello[3] hello[4] hello[4] hello[4] hello[4] hello[5] hello[5] hello[5] hello[5] hello[5] Test 5: hello[1] hello[2]hello[2] hello[3]hello[3]hello[3] hello[4]hello[4]hello[4]hello[4] hello[5]hello[5]hello[5]hello[5]hello[5]

    Maybe the foreach function you have not heard about it yet. But soon when you will reach the loops you will read about it, but in short what it does is simple put one by one the elements that you specify in the parenthesis (0 .. 5).

    I hope that I did not confuse you more that tried to help you understand.

    Seeking for Perl wisdom...on the process...not there...yet!
Re: Join with x operator
by AnomalousMonk (Archbishop) on Jul 09, 2014 at 13:44 UTC

    Another way to (literally) look at it:

    c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my @ra = ('hi') x 5; dd \@ra; ;; @ra = 'hi' x 5; dd \@ra; " ["hi", "hi", "hi", "hi", "hi"] ["hihihihihi"]
Re: Join with x operator
by sneaky (Initiate) on Jul 09, 2014 at 10:41 UTC
    Thanks both of you. I understand the difference now.