Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

juxtapose

by Keef (Sexton)
on Mar 31, 2001 at 05:57 UTC ( [id://68594]=perlquestion: print w/replies, xml ) Need Help??

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

I have a two variables, how do I justapose them so they create a 3rd variable. Like so:
$var1 = "cream"; $var2 = "coffee"; $var3 = $var2 " & " $var1; print $var3 # returns "coffee & cream"

Replies are listed 'Best First'.
Re: juxtapose
by MeowChow (Vicar) on Mar 31, 2001 at 06:38 UTC
    To quote the Beasty Boys, "I like my sugar with coffee & cream". Moving on...
    $var3 = "$var1 & $var2"; $var3 = $var1 . " & " . $var2; $var3 = join ' & ',$var1,$var2; undef $var3; $var3 .= $_ for $var1 =~ /./sg,' ','&',' ',split//,$var2;
    The choice of WTDI is up to you =)
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: juxtapose
by dws (Chancellor) on Mar 31, 2001 at 06:02 UTC
    The string concatenation operator is .

    Applying this operator is left as an exercise.

Re (tilly) 1: juxtapose
by tilly (Archbishop) on Mar 31, 2001 at 23:53 UTC
    The usual answer is to use interpolation.
    print "$var2 & $var1"; # Prints "coffee & cream"
    That is why all of those funny little symbols are used, to make it easy to drop variables directly in strings...
Re: juxtapose
by greenFox (Vicar) on Mar 31, 2001 at 06:05 UTC
    I'm not sure I understand what you mean by juxtapose but this works for me-
    $var1 = "cream"; $var2 = "coffee"; $var3 = $var2 . " & " . $var1; print $var3 # prints "coffee & cream"
    which uses the '.' operator to concatenate the strings, is that what you wanted?

    --
    my $chainsaw = 'Perl';

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://68594]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2024-04-23 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found