Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Answer: How do I concatenate a string?

by davido (Archbishop)
on Sep 14, 2003 at 02:49 UTC ( #291337=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How do I concatenate a string? - Answer contributed by davido

For simple concatenation,

my $foo = "Foo"; my $bar = "Bar"; my $string = $foo . $bar; # $string contains "FooBar".

For concatenating the new string to the end of the existing string (appending):

my $foo = "Foo"; my $bar = "Bar"; $foo .= $bar; # $foo now contains "FooBar"

For concatenating multiple strings all at once, use join. Join is (according to the Camel book) the most efficient way to concatenate many strings together. Join can be used with a delimiter, or with a null string as the delimiter, so that nothing is inserted between the strings that are joined (concatenated) together.

my $this = "This"; my $that = "That"; my $the = "The"; my $other = "Other"; my $string = join "", $this, $that, $the, $other; # $string now contains "ThisThatTheOther".

Join can do a lot more than just simple concatenation. Whatever you put between the quotes will be inserted between the strings that are being joined together. Also, join can accept not just a list of scalar variables, but also an array, or a list of literal strings.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[holli]: Can somebody identify this connector?

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-11-19 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (281 votes). Check out past polls.

    Notices?