http://www.perlmonks.org?node_id=470373


in reply to Scalar joining, concatenation, involving varying text

If everything was in a hash, it would be easier:

my %data; $data{a} = 1; $data{b} = 2; $data{c} = 3; my $combined = join ' ', map { "$_=$data{$_}" } grep { $data{$_} } sort keys %data;

There are two alternatives to using hashes.

1) Mucking around with the symbol table. This won't work if $a, $b, etc are lexical (my) variables.

our $a = 1; our $b = 2; our $c = 3; my $combined = join ' ', map { "$_->[0]=$_->[1]" } grep { $_->[1] } map { [ $_, do { no strict 'refs'; ${$_} } ] } qw(a b c);

2) Using eval. This is slow and risky.

my $a = 1; my $b = 2; my $c = 3; my $combined = join ' ', map { "$_->[0]=$_->[1]" } grep { $_->[1] } map { [ $_, eval('$'.$_) ] } qw(a b c);

Update: Added grep to every snippet to fix the bug crenz pointed out.

Here's the above without using map+grep:

my $combined; foreach (sort keys %data) { my $val = $data{$_}; # OR: my $val = do { no strict 'refs'; ${$_} }; # OR: my $val = eval('$'.$_); next unless $val; $combined .= "$_=$val "; } chop($combined);

Replies are listed 'Best First'.
Re^2: Scalar joining, concatenation, involving varying text
by crenz (Priest) on Jun 27, 2005 at 20:37 UTC

    Uh, no. You're loosing the if-condition here, so your approach has different semantics than what the original poster wanted.