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

My simple join function isn't working. What kind of stupid typo did I make now?
my @fruits = "Apple Banana Cherry Honeydew Watermelon" ; my $tmp_fruits = join ":", @fruits; say "$tmp_fruits \n"; output: Apple Banana Cherry Honeydew Watermelon
Thanks in advance,

The catfish

Replies are listed 'Best First'.
Re: join function
by pryrt (Abbot) on Oct 16, 2018 at 21:24 UTC

    Yes it is. Your @fruits "array" consists of one element, the string "Apple Banana Cherry Honeydew Watermelon", so there is nothing to join.

    If you intended for @fruits to contain 5 elements, then you need to split it up on assignment. The easiest way to do that is to change your first line to my @fruits = qw/Apple Banana Cherry Honeydew Watermelon/;

    update: see http://perldoc.perl.org/perlop.html#Quote-Like-Operators for the qw// quote-like operator. Alternately, use split to break up the string into multiple elements.

Re: join function
by LanX (Saint) on Oct 16, 2018 at 23:49 UTC

      Sir,

      Thanks for supplying me with the link. I changed the line to

        my @fruits = ("Apple", "Banana", "Cherry", "Honeydew", "Watermelon") ;

      Works fine now.

      Thank you,

      Catfish

Re: join function
by morgon (Priest) on Oct 17, 2018 at 00:29 UTC
    It's a bit of a type error that perl doesn't catch as you assign a string-literal (not an array!) to an array-variable.

    Perl is of course forgiving but you end up joining an array consisting of only one element...