Click to See Complete Forum and Search --> : Perl string concatenation?


Riley
08-20-2002, 01:46 PM
Is there some Perl function to concatenate strings? I just had the idea to do something like this:
$string1 = "$string1$string2";
and I'm hoping that will work, but any other assistance would be appreciated.

roshern
08-20-2002, 02:07 PM
try using the "dot" operator:

$string1 = $string1 . $string2;

roshern
08-20-2002, 02:08 PM
a quick google search yields:

http://www.devdaily.com/perl/edu/articles/pl010003.shtml

debiandude
08-20-2002, 02:42 PM
Perl has a special operator for this, the dot, '.' operator.

So to concat you just do:

$string1 .= $string2;

Riley
08-20-2002, 02:58 PM
thank you all, but I've decided to stick with my idea seeing as how it works and i'm not one to take extra time to change something that works

TheLinuxDuck
08-20-2002, 06:02 PM
Riley,

Just to give you a little more information so you can feel a little more comfortable with string manipulation/etc in perl, you obviously found out that what you are doing, via "$string1$string2" works, but do you know why it works? And there are some issues with this method that warrant using the string concatenation operation, or '.' as well as another method of putting scalars into a string.

As you have done, "$string1$string2" works.. this technique is called interpolation.. it's the replacement of a variable in a string, and is automatic in perl when the string is enclosed in double-quotes, or ".

If you use single quotes, the scalars will not be interpolated, and thus your resultant string will be the exact same thing that you started with. Do a small test, and see:

#!/usr/bin/perl
use strict;
use warnings;

my($string1) = "basic";
my($string2) = "business";

print "$string1$string2\n",
'$string1$string2\n";
exit;


Using double quotes, it would be the exact same thing as saying:

print $string1 . $string2, "\n",


I don't know which is faster to execute of the two, but I would imagine that concatenation would be... but that's only a guess at this point.

But, there are scenarios when interpolation won't work (but there are work arounds). Let's say your variable name is $bacon, but you're putting it into a string of other text, as:

print "Blatblat$baconblatblat\n";


Perl can't find $bacon in that string, because the variable name is butted up against the other text, making interpolation unusable, because it will be expecting a variable called $baconblatblat.

One way to get around this is to use concatenation, such as:

print "Blatblat" . $bacon . "blatblat\n";


But, a neat way to get around it is to surround the variable name with curly brackets, or {}, such as:

print "Blatblat${bacon}blatblat\n";


That works really well, too. Not quite as readable, but I use it more often than concatenation... mostly from habit.

And, if the code is using print to display some info, according to "Programming Perl 3rd Edition", use a comma instead of the string concatention operator. It's supposed to be faster in this case:

print "Here is one line of text\n",
"Another line still\n",
"Interpolate \$bacon to $bacon\n";


I hope that his helps you understand the different ways to achieve your goal, and also educates you so that you don't feel as uncomfortable adjusting code.

I dig perl.. (=

TLD

debiandude
08-20-2002, 07:44 PM
Okay thats fine and good that you want to stick to what you wrote becuase it works. However that is the WORST resason I have ever heard for using something.

I mean for instance I have


foreach my $i( 1 .. 100000) {
print "hello" if $i == 100000;
}


and then someone tells me Oh geeze you know you can do it this way:
print "hello";

Saying thanks but mine works too seems awefully stupid. So perhaps what your doing right now if fine and dandy and wont incure any noticable slowdown, it wont always be like that. Just becasue TISMTOWTDI dosn't mean that they are all good ways.

Have fun with perl :-)

Riley
08-20-2002, 09:21 PM
Don't worry I fully review my options. Speed would be reason enough to change and I would change if the code was ugly. You must also factor in the will to be lazy which resists change. I've actually scripted perl a lot, it has just been a long time since I last did it. And I have lots of current experience with C/C++.

debiandude
08-20-2002, 09:45 PM
Well if speed is reason enough for you then the concatenation operation is faster.

that is
$string1 .= $string2;

is faster than

$string1 = "$string1$string2";

first since it's in double quotes perl has to interpolate which takes time.
Seconly by doing it that way perl resets $string1's value and then assigns it the right value.

Think like this:
$string1 .= $string2 is more like strcat in c

Where as:
$string1 = "$string1$string2" is more like sprintf