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.
Re: Concatenating more than one variable in perl
by LanX (Saint) 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)
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
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)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Concatenating more than one variable in perl
by Athanasius (Archbishop) 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,
| [reply] [Watch: Dir/Any] [d/l] [select] |
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
| [reply] [Watch: Dir/Any] |
|
perl -MO=Deparse yourscript.pl
| [reply] [Watch: Dir/Any] [d/l] |
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']};
| [reply] [Watch: Dir/Any] [d/l] |
Re: Concatenating more than one variable in perl
by perlfan (Vicar) 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.
| [reply] [Watch: Dir/Any] [d/l] |
|
|