Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help! How do I add two strings together?

Status
Not open for further replies.

huckfinn

Programmer
May 19, 2000
4
US
My problem isn't that simple though...<br>One of those strings is in a array.<br><br>For example, the following program:<br>@test = ('bob', 'sid', 'derek');<br>foreach $test (@test) {<br>&nbsp;&nbsp;&nbsp;&nbsp;$test = $test + &quot;n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;print $test;}<br><br>produces:<br>000<br><br>not bobnsidnderekn like I would suspect.<br>How do I add a scalar/string to a scalar/string in array.<br><br>Chris<br>
 
$test corresponds to the individual array item, so you can't exactly just go performing commands on it. Best to use a different variable like this:<br><br>foreach $test (@test) {<br>&nbsp;&nbsp;$testPrint = $test + &quot;n&quot;;<br>&nbsp;&nbsp;print $testPrint;<br>}<br><br>Or for this example alone, just do this:<br><br>print $test + &quot;n&quot;;<br><br>Best of luck! <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Additionally,<br>the '+' is an arithmatic operator.&nbsp;&nbsp;When you add alphas you get zero.<br><br>If you want to concatentate your elements you need to use '.' or 'join'.<br><br>A simple example......<br><FONT FACE=monospace>#!/usr/local/bin/perl<br>@test = ('bob','sue','joe','ann');<br>$added = $test[0] + &quot;n&quot;;<br>print &quot;Added -&gt; $added\n&quot;; <font color=red># prints 0</font><br>$firstTwo = $test[0].$test[1];<br>print &quot;$firstTwo\n&quot;;<font color=red># prints bobsue<br></font><br><br>Hope this helps... <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
I'm not sure what it means to concatentate something.......<br>I probably meant 'concatenate'&nbsp;&nbsp;;^) <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
thanks, goBoating- I've been working with Java too long, I've almost forgotten these things :eek:) <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Man,<br><br>You guys Rock!<br>Thanks for correcting me!<br><br>HuckFinn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top