Perl Monk, Perl Meditation PerlMonks

### Re^2: interpolating operators within strings

by abualiga (Scribe)
 on Apr 14, 2013 at 03:29 UTC ( #1028592=note: print w/replies, xml ) Need Help??

in reply to Re: interpolating operators within strings
in thread interpolating operators within strings

Your insight is much appreciated! I was trying to do addition and subtraction of two lists of paired numbers and wanted to avoid separate loops.

• Comment on Re^2: interpolating operators within strings

Replies are listed 'Best First'.
Re^3: interpolating operators within strings
by kcott (Chancellor) on Apr 14, 2013 at 04:01 UTC

Well, in that case, you might be better off with the pairwise function of List::MoreUtils:

```\$ perl -Mstrict -Mwarnings -E '
use List::MoreUtils qw{pairwise};
my @x = 0 .. 3;
my @y = map { 10 + \$_ } @x;
say for pairwise {
"\$a + \$b = " . (\$a + \$b) . "; \$b - \$a = " . (\$b - \$a)
} @x, @y;
'
0 + 10 = 10; 10 - 0 = 10
1 + 11 = 12; 11 - 1 = 10
2 + 12 = 14; 12 - 2 = 10
3 + 13 = 16; 13 - 3 = 10

Note how \$a holds @x elements and \$b holds @y elements. These (\$a & \$b) are the same special variables used by sort, so don't declare them; they're described in perlvar - SPECIAL VARIABLES - General Variables. You can use them in either order: cf. \$a + \$b and \$b - \$a.

-- Ken

Create A New User
Node Status?
node history
Node Type: note [id://1028592]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-05-21 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (161 votes). Check out past polls.

Notices?