in reply to Re^2: Concatenate printing a string and array
in thread Concatenate printing a string and array

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

Replies are listed 'Best First'.
Re^4: Concatenate printing a string and array
by spickles (Scribe) on Jan 05, 2009 at 20:09 UTC
    Thanks to everyone that responded. I like getting the varied responses that come about through the different experiences we've all had with Perl. I often don't understand what I'm reading, but I did find JohnGG's response to be something I was able to work with, despite the fact that I don't understand it much. I think it has something to do with specifying that the normal string separator $" (which normally has a value of a whitespace) to have a value of null. So even though the separator is still in effect, we're controlling it output by modifying its attributes. Is this correct?
    Here is my working code:

    print do { local $" = q{}; "Your result is:" . qq{@binary}; }

    Cheers,
    Scott

      You have the essence of it but it ($") is called the "list separator" (see $LIST_SEPARATOR or $" in perlvar). It determines what character will separate elements of a list or array that has been interpolated into a double quoted string (or double quote-like string, see Quote and Quote like Operators in perlop). Also, I think that the word "null" is not correct as I take that to mean local $" = qq{\0}; ... i.e. the NUL character. As NUL is non-printing the effect would be the same but the code is actually setting $" to a zero-length, empty string which is not the same thing.

      You should note that the quoting constructs I habitually use, q{...} and qq{...}, are really the same as single and double quotes respectively. They are not commonly used but they make life much easier when doing Perl one-line commands at the shell or command prompt, which is why I have got into that habit. As a general guide, only use double quotes when you want to interpolate a variable into the string; use single quotes if no interpolation is needed.

      As to your working code, I think it will be easier to read and more maintainable if you employ some form of code indentation. This becomes more and more important as projects grow and require multiple developers/maintainers. Have a look around the Monstery at what some of the other Monks do, choose (and adapt if necessary) a scheme that suits you then use it consistently.

      In the last expression in the do BLOCK you have concatenated two strings to form the one you want to print. The concatenation is unnecessary since your @binary array could be interpolated in the first string. Using quote marks rather than my usual quoting constructs, your code becomes.

      print do { local $" = ''; "Your result is:@binary"; };

      I hope these points help to clarify things for you but please ask further if there's something you don't understand.

      Cheers,

      JohnGG

Re^4: Concatenate printing a string and array
by AnomalousMonk (Archbishop) on Jan 05, 2009 at 18:46 UTC
    Rats - forgot the local.

    All the more reason to use join().

      I often use join but it can begin to get tedious if, for instance, you are printing more than one array using the same separator. Localising a change to the list separator then begins to simplify things, as in this rather contrived and silly example.

      use strict; use warnings; my @ladies = qw{ ann flo jen }; my @gents = qw{ bob pip tim }; print q{The ladies, (}, join( q{, }, @ladies ), q{), met the gents, (}, join( q{, }, @gents ), qq{).\n}; print qq{The ladies, (@{ [ join q{, }, @ladies ] }), }, qq{liked the gents, (@{ [ join q{, }, @gents ] }).\n}; print do{ local $" = q{, }; qq{The ladies, (@ladies), married the gents, (@gents).\n}; };
      The ladies, (ann, flo, jen), met the gents, (bob, pip, tim). The ladies, (ann, flo, jen), liked the gents, (bob, pip, tim). The ladies, (ann, flo, jen), married the gents, (bob, pip, tim).

      Cheers,

      JohnGG

        My own inclination is to see a statement like
            { local $" = 'foo';  print "array @ra values"; }
        as being more elegant and 'quiet' and to prefer it over a statement like
            print "array ", join('foo', @ra), " values";
        which just seems messy to me.

        This preference continues until I forget (as in my reply) to actually use the local built-in, or fail to properly specify the limiting scope, or forget that a change to a package variable will 'propagate' to any subroutines called within the limiting scope after the change is made.

        Then I realize that I have inflicted upon myself a headache, possibly a massive 3 A.M. headache, and I resolve not to localize $" (or any other package variable if I can help it) and to use join() instead, however messy.

        And I keep my resolution for a while, and then start backsliding.