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

I know of the differences between

print @array and print "@array"

I've written a decimal to binary conversion program, and I need to print the array. So I'd like to print something like:

"Your result is: 1111"

I've tried

print "Your result is: " . @array

The output of that is "Your result is: 4" I'm assuming it is because the array is getting converted to a string automatically. How can I achieve the output I'm looking for?

Regards, Scott

Replies are listed 'Best First'.
Re: Concatenate printing a string and array
by CountZero (Bishop) on Jan 05, 2009 at 06:00 UTC
    The concatenation "." (dot) operator provides a scalar context to its arguments. The value of an array in scalar context is the length of the array. As your array has 4 elements, its return value in scalar context is 4.

    Double quotes maintain the array context, so you could try the following:

    { local $" = undef; print "Your result is: @array"; }
    Note the accolades to restrict the change to the $" (list separator variable) to the smallest scope possible.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      local $" = undef;
      This produces warnings.
      >perl -wMstrict -le "my @ra = (1,2,3,4,5); { $\" = undef; print qq{@ra}; } " Use of uninitialized value in join or string at -e line 1. 12345
      Try something like assigning $" the empty string instead of undef:
      >perl -wMstrict -le "my @ra = (1,2,3,4,5); { $\" = ''; print qq{@ra}; } " 12345
      Or better yet, just use join('', @ra) as suggested by others.
        Try something like assigning $" the empty string instead of undef:
        >perl -wMstrict -le "my @ra = (1,2,3,4,5); { $\" = ''; print qq{@ra}; } "

        You are correct about the use of undef but the local that CountZero used is necessary otherwise the change is global in scope.

        $ perl -Mstrict -wle ' > my @arr = ( 1, 2, 3 ); > print qq{@arr}; > { $" = q{}; print qq{@arr} } > print qq{@arr};' 1 2 3 123 123 $

        I usually limit the scope to the print itself by using a do BLOCK.

        $ perl -Mstrict -wle ' my @arr = ( 1, 2, 3 ); > print do{ local $" = q{}; qq{@arr} };' 123 $

        I hope this is of interest.

        Cheers,

        JohnGG

Re: Concatenate printing a string and array
by marcussen (Pilgrim) on Jan 05, 2009 at 05:10 UTC

    You might want join
    print join("",@array);

    I think you might have misunderstood how interpolation and scalar versus list context works. However I would prefer to leave the explanation up to someone who is better at expressing their wisdom than myself.

    Confucius says kill mosquito unless cannon
Re: Concatenate printing a string and array
by jethro (Monsignor) on Jan 05, 2009 at 06:12 UTC

    Was that on purpose or a typo that you have a period (.) instead of a comma (,) between the string and your @array ?

    If the former, you should read up about context. The concatenation operator (.) joins two scalars, so the array is in scalar context (i.e. the concatenation needs a scalar, so the array provides a scalar). An array provides its size in scalar context, in this case 4

    The parameters of a print on the other hand (with the possible exception of the optional filehandle parameter) provide list context which is why a comma works

      print "The array is ", @array, "\n";
      or
      print( "The array is ", @array, "\n");
      print "The array is ", @array, "\n"; or print( "The array is ", @array, "\n");
Re: Concatenate printing a string and array
by velusamy (Monk) on Jan 05, 2009 at 05:44 UTC
    Hi,
    Try this
    use strict; use warnings; my @array=(1,1,1); print "Reslut:",@array;
      For the benefit of those whose eyesight isn't quite what it was, the difference between the original posting and this reply is that the dot/period has been replaced with a comma. :-)

      (Oh, and "result" has been re-spelt more rudely!)

      For the record, the same kind of approach can be expressed as:

      print "Your result is: "; print @array;

      --
      use JAPH;
      print JAPH::asString();

Re: Concatenate printing a string and array
by imrags (Monk) on Jan 05, 2009 at 05:18 UTC
    Try this:
    @array = (1,2,3,4,5); local $"= ""; print "Your result is @array";
    I am not sure how using output list separator ($") is going to affect the rest of your program
    That is why i've put it local (assuming u r using a subroutine).
    Raghu
      I am not sure how using output list separator ($") is going to affect the rest of your program That is why i've put it local (assuming u r using a subroutine).

      It is better to limit the scope of the change using a bare code BLOCK

      { local $" = q{}; print qq{@array}; }

      or a do BLOCK

      print do{ local $" = q{}; qq{@array} };

      I hope this is useful.

      Cheers,

      JohnGG

Re: Concatenate printing a string and array
by Bass-Fighter (Beadle) on Jan 06, 2009 at 08:43 UTC
    and if you use:

    my $n = @array; my $count=0; for (my $i=0; $i<$n; $i++){ print $array[$count]; $count++ }
    $n gets in here the number of binary numbers
    and $count gives you which number in the array must be printed