R
RedGrittyBrick
Consider
#!/usr/bin/perl
use strict;
use warnings;
while(<DATA>) {
my($t1,$t2,$value);
($t1,$t2)=qw(A P); $value = $1 if /^$t1.*$t2=(.)/;
($t1,$t2)=qw(B Q); $value = $1 if /^$t1.*$t2=(.)/;
($t1,$t2)=qw(C R); $value = $1 if /^$t1.*$t2=(.)/;
print "$value\n";
}
__DATA__
A P=1 Q=2 R=3
B P=8 Q=2 R=7
C Q=2 P=1 R=3
I'd like to replace the repetition with an elegant loop over pairs of
$t1,$t2 values stored in an array (or other structure) like one of
my @pairs = qw (A,P B,Q C,R);
my @pairs = qw (A P B Q C R);
I've not had much success with a brief attempt at combining `while`,
`split` and `unshift`.
What concise, elegant solution am I missing?
(Also posted to stackoverflow.com, let me know if you object. I'll post
a combined summary of answers to both fora as penance)
#!/usr/bin/perl
use strict;
use warnings;
while(<DATA>) {
my($t1,$t2,$value);
($t1,$t2)=qw(A P); $value = $1 if /^$t1.*$t2=(.)/;
($t1,$t2)=qw(B Q); $value = $1 if /^$t1.*$t2=(.)/;
($t1,$t2)=qw(C R); $value = $1 if /^$t1.*$t2=(.)/;
print "$value\n";
}
__DATA__
A P=1 Q=2 R=3
B P=8 Q=2 R=7
C Q=2 P=1 R=3
I'd like to replace the repetition with an elegant loop over pairs of
$t1,$t2 values stored in an array (or other structure) like one of
my @pairs = qw (A,P B,Q C,R);
my @pairs = qw (A P B Q C R);
I've not had much success with a brief attempt at combining `while`,
`split` and `unshift`.
What concise, elegant solution am I missing?
(Also posted to stackoverflow.com, let me know if you object. I'll post
a combined summary of answers to both fora as penance)