Link to home
Start Free TrialLog in
Avatar of Kapusta
Kapusta

asked on

Adding to strings together in a script

It would be helpful to learn how to add two string variables together in a script.

Example:

$firstvar = "Hello "
$secondvar = "world"

How do you make a new variable with "Hello world" using $firstvar and $secondvar

In GWBASIC I can simply do this:

Hi$ = Firstvar$ + SecondVar$

How does one do this in Perl?
ASKER CERTIFIED SOLUTION
Avatar of twexperts
twexperts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thewitt
thewitt

Actually, the concatenation operator in perl is a ., however joining two strings by using variable expansion inside quotes is the more common method.

$hello = "hello ";
$there = "there";
$hi = $hello . there;

You can see why this would not be the simpliest method since you need to leave blank spaces if you want your $hi string to be "hello there" and not "hellothere."

Tim
Avatar of ozo
Unquoted string "there" may clash with future reserved word at thewitt line 3.

#And to complete the list:
$hi = join("",$firstvar,$secondvar);
$hi = pack("a*a*",$firstvar,$secondvar);
($hi = $firstvar).=$secondvar;
substr($hi=$secondvar,0,0)=$firstvar;
($hi = $firstvar)=~s/$/$secondvar/;
#hmm, any more?