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

Hi Monks,

If you have three variables, "$a", $b, $c in which $a = Mike, $b = 100, $c = 200 and you want to join them as $d in a way that when you print $d you get Mike-100_200.How would you do that? One way it to use join two times. One time to join Mike and 100 with "-" separator and then join the new variable with "_". Is there another one step function?

Pedro

Replies are listed 'Best First'.
Re: Join with more than one EXPR
by Corion (Patriarch) on Jan 29, 2009 at 07:53 UTC

    What's wrong with plain string interpolation?

    $d = "$a-${b}_$c";

    Alternatively, use the simple string concatenation:

    $d = $a . '-' . $b . '_' . $c;
      Thanks all, All of your suggestions would work.
Re: *Join* with more than one EXPR
by tye (Sage) on Jan 29, 2009 at 08:14 UTC

    I avoid long runs of concatenation.

    my $key= "$a-$b" . "_$c"; my $key= join '', $a, '-', $b, '_', $c; # or my $key= join '', "$a-$b", "_$c";

    - tye        

      I avoid long runs of concatenation.
      I don't want to criticize that, or try to change your mind, but it makes me wonder why you try to avoid them. Furthermore, you give three alternatives, but you exclude the obvious "$a-${b}_$c" which uses neither concatenations nor joins. Do you avoid long runs of interpolations as well?

        I'm sorry, were the two nodes showing the "obvious" solution not enough? Well, there are three now, so I hope that finally suffices. :)

        There are lots of ways to follow a variable's contents by a \w character or ' or :: or brackets/braces. Numerous of them I find less ugly than the "obvious" "${x}_", especially when you include cases like "${h{$k}}{$s}". This is simply a personal preference, not even something I bother to recommend to others. But it is worth demonstrating one or more of the alternatives on occasion.

        Long runs of expressions separated by commas have a lot of advantages over longs runs of concatenations. Those advantages are just some of the reasons why I use join in places where others might use concatenations or sprintf or here-docs or large strings full of interpolations (or maybe even a templating system). The code that implements PerlMonks does a lot of building of strings and over the years I've tried a lot of different styles but join is the one that has survived, faring significantly better than the alternatives in the face of having to maintain such code.

        Concatenation has the wrong precedence for even trivial cases like $i+1 . ") $a[$i]\n". Coders are quite used to formatting long runs of comma-separated expressions nicely while the "obvious" way to format a long run of concatenations (over multiple lines of code) is not clear, IME. The dot is easy to lose sight of and also easy to confuse with a comma. Conversely, the comma's cannonical positioning without whitespace in front of it and either at the end of a line or with whitespace after it, makes it easy to recognize. Thankfully, Perl allows the last expression to also be followed by a comma (not true with concatenating). join ' ' is also often a better choice, preventing some common mistakes like print "There are", $x->count(), "items.".

        Okay, I'm tired. I'm sure there are still some advantages that I've neglected to mention just comparing concatenation to join. And I'm going to go to bed rather than mention any of the problems with sprintf or here-docs etc.

        - tye        

Re: Join with more than one EXPR
by GrandFather (Saint) on Jan 29, 2009 at 07:54 UTC
    $d = "$a-${b}_$c";

    Update: missing {} added - Thanks moritz!


    Perl's payment curve coincides with its learning curve.
Re: Join with more than one EXPR
by moritz (Cardinal) on Jan 29, 2009 at 07:54 UTC
    my $d = $a . '-' . $b . '_' . $c;

    If you don't play to join's strengths, don't use it.

Re: Join with more than one EXPR
by Mr. Muskrat (Canon) on Jan 29, 2009 at 21:30 UTC

    You received some good answers so I won't bother trying. I will however offer a pieces of advice.

    Don't use one letter variable names for anything other loop variables, e.g. for my $i ( 0 .. 5 ).

    Don't use $a or $b for anything other than a sort subroutine.