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

my @numbers = (4,7,11,14); my $start='0 '; print join ("$start" , @numbers),"\n";
result
40 70 110 14 # why no "0 space" before the 4?
desired
0 4 0 7 0 11 0 14 # also need to add a space after each list item

Replies are listed 'Best First'.
Re: array join missing zero and space
by ikegami (Patriarch) on Jun 21, 2011 at 04:12 UTC

    Because that's not what join does.

    Assuming you don't want a trailing space:

    join ' ', map { 0, $_ } @numbers
      use strict; use warnings; my @numbers = (4,7,11,14); my $start='0 '; #print join ("$start" , @numbers),"\n"; print join ' ', map { 0, $_ } @numbers;

      The code above gives this:

      Not enough arguments for map at 910662.pl line 9, near "} @numbers" syntax error at 910662.pl line 9, near "} @numbers" Execution of 910662.pl aborted due to compilation errors.

      I tried this:

      print join ' ', map { ' 0 ' . $_ } @numbers;

      And get this:

       0 4  0 7  0 11  0 14

      Not sure where the extra spaces after 4,7 and 11 come from.

      Update: duh, the join has a space.

        Brackets are needed to make the mapping function return a 2-element list for each element of @numbers, which is what I think ikegami intended:

        print join ' ', map { (0, $_) } @numbers;

        --

        "Any sufficiently analyzed magic is indistinguishable from science" - Agatha Heterodyne

        bah, the parser is thinking {} is a hash constructor instead of a block. A semi-colon will disambiguate.
        print join ' ', map {; 0, $_ } @numbers;
Re: array join missing zero and space
by armstd (Friar) on Jun 21, 2011 at 04:17 UTC

    'join' glues stuff together, with the string you specified as the glue.

    You only have four spaces between your five fingers. Notice you don't get a trailing zero either...

    To do what you want using a similar approach:

    my @numbers = (4,7,11,14);
    my $start='0 ';

    print $start, join (" $start" , @numbers),"\n"; # notice the added space in the join string, but not the prefix string

    --Dave

Re: array join missing zero and space
by johngg (Canon) on Jun 21, 2011 at 11:01 UTC

    Another way using join and substr.

    knoppix@Microknoppix:~$ perl -E ' > @numbers = ( 4, 7, 11, 14 ); > $start = q{ 0 }; > say substr join( $start, q{}, @numbers ), 1;' 0 4 0 7 0 11 0 14 knoppix@Microknoppix:~$

    I hope this is helpful.

    Cheers,

    JohnGG

Re: array join missing zero and space
by hbm (Hermit) on Jun 21, 2011 at 12:26 UTC

    You can also fiddle with the list separator:

    @numbers=(4,7,11,14); {local $"=' 0 ';print"0 @numbers"}
Re: array join missing zero and space
by Anonymous Monk on Jun 21, 2011 at 13:39 UTC

    join is used to create a field separator (opposite: split )

    code:

    my $a='4,7,11,14'; print join(',',split(',',$a));

    result:

    4,7,11,14

    code:

    my @numbers=(4,7,11,14); my $start='0 '; my $middle=' 0 '; my $end="\n"; print $start,join("$middle",@numbers),$end;

    result:

    0 4 0 7 0 11 0 14

      Another alternative:

      @numbers = (4,7,11,14); printf '0 %d ' x $#numbers . '0 %d', @numbers