Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Concatenating more than one variable in perl

by manoj_speed (Vicar)
on Jun 19, 2014 at 13:45 UTC ( #1090469=perlquestion: print w/ replies, xml ) Need Help??
manoj_speed has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I would like to concatenate more than one variable in assignment.

For Ex:
If I want to assign two variables in a single shot. I can use this.
($myvar1, $myvar2) = ('testing1', 'testing2');

Likewise, I would like to use this for two variables concatenation in a single shot.
($myvar1, $myvar2) = ('Hi ', 'Hello');
($myvar1, $myvar2) .= ('Welcome', 'World');
print "$myvar1, $myvar2\n";   # prints Hi, HelloWorld

The above code is doing appending value for variable-2 only.

Is there any possible way to do this in a single statement ?

-- The wisest mind has something yet to learn.

Comment on Concatenating more than one variable in perl
Re: Concatenating more than one variable in perl
by toolic (Bishop) on Jun 19, 2014 at 13:50 UTC
    The above code is doing appending value for variable-2 only.
    If you want Perl to give you a hint as to why that happens, see Tip #1 from the Basic debugging checklist: warnings
Re: Concatenating more than one variable in perl
by Athanasius (Abbot) on Jun 19, 2014 at 14:08 UTC

    If you want to concatenate the same text to each variable, just use a for loop:

    23:50 >perl -wE "my ($var1, $var2) = ('Hi', 'Hello'); $_ .= ' world' f +or $var1, $var2; say qq[$var1, $var2];" Hi world, Hello world 23:53 >

    But it looks like you want to concatenate a different piece of text to each variable; in which case, maybe this is what you’re looking for:

    use strict; use warnings; my @vars = qw[Hi Hello Greetings ]; my @suffixes = qw[there world earthlings]; @vars = map { $_ . ' ' . shift(@suffixes) . '!' } @vars; print join(', ', @vars), "\n";

    Output:

    0:06 >perl 923_SoPW.pl Hi there!, Hello world!, Greetings earthlings! 0:06 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Concatenating more than one variable in perl
by LanX (Canon) on Jun 19, 2014 at 14:18 UTC
    > Is there any possible way to do this in a single statement ?

    most likely a XY Problem, people who number their vars hate arrays.

    Though for fun...

    DB<110> use List::MoreUtils "pairwise" DB<111> ($x,$y)=("X","Y") => ("X", "Y") DB<112> pairwise { $$a .= $b } @{[\($x,$y)]}, @{["x","y"]} => ("Xx", "Yy") DB<113> $x,$y => ("Xx", "Yy")

    Not sure if versions newer than 5.10 allow to get rid of the '@{...}'.

    Cheers Rolf

    (addicted to the Perl Programming Language)

      Teach me to number not the vars of my scripts, but the indices of my arrays...

        > Teach me to number not the vars of my scripts, but the indices of my arrays...

        well, if you beg me so nicely... ;-)

        DB<100> use List::MoreUtils "pairwise" DB<101> @vars=qw(X Y) => ("X", "Y") DB<102> @appends=qw(x y) => ("x", "y") DB<103> pairwise { $a .= $b } @vars, @appends => ("Xx", "Yy") DB<104> @vars => ("Xx", "Yy")

        and for completeness

        DB<105> @vars=qw(X Y) => ("X", "Y") DB<106> @vars = pairwise { $a . $b } @vars, @appends => ("Xx", "Yy")

        Cheers Rolf

        (addicted to the Perl Programming Language)

Re: Concatenating more than one variable in perl
by perlfan (Curate) on Jun 19, 2014 at 15:48 UTC
    Like so?
    use Data::Dumper (); my @foo = @bar = (1, 2, 3); print Data::Dumper::Dumper(\@foo, \@bar);
    Update, upon further reflection looks like you may want to look at join or sprintf/printf.
Re: Concatenating more than one variable in perl
by MidLifeXis (Monsignor) on Jun 19, 2014 at 17:14 UTC

    Update: LanX had the module (List::MoreUtils) that I was trying to find. Nothing to see here. Move along.

    I am not certain that this is what you are looking for, but I seem to remember seeing code to fold arrays together in various ways. Below you will find an example of a simple folding subroutine. This could be made generic enough to be able to handle any number of arrays.

    use strict; use warnings; use Data::Dumper; $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 0; sub array_fold { my $a1 = shift; my @a1 = @$a1; my $a2 = shift; my @a2 = @$a2; my $folder = shift || sub { [ @_ ] }; die "Arrays are not of the same size" unless scalar( @a1 ) == scalar( @a2 ); my @results; while( scalar( @a1 ) && scalar( @a2 ) ) { push @results, $folder->( shift( @a1), shift( @a2 ) ); } return @results; } my @a = qw( a b c d ); my @b = qw( A B C D ); my @results = array_fold( \@a, \@b ); print Dumper( { results => \@results } ), "\n"; my @results2 = array_fold( \@a, \@b, sub { "@_" } ); print Dumper( { results2 => \@results2 } ), "\n"; __END__ $VAR1 = {'results' => [['a','A'],['b','B'],['c','C'],['d','D']]}; $VAR1 = {'results2' => ['a A','b B','c C','d D']};

    --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1090469]
Approved by toolic
Front-paged by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2015-07-07 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls