Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Pulling pairs of values from an array

Reply
Thread Tools

Pulling pairs of values from an array

 
 
RedGrittyBrick
Guest
Posts: n/a
 
      12-17-2010
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)

--
RGB
 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      12-17-2010
On 17/12/2010 11:59, RedGrittyBrick wrote:
> 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)
>


In
<http://stackoverflow.com/questions/4470189/perl-pulling-pairs-of-values-from-an-array/4470453#4470453>
Sorpigal suggested

#!/usr/bin/perl
use strict;
use warnings;

my %pairs = qw/A P B Q C R/;

foreach my $data (<DATA>) {
while(my($t1, $t2) = each(%pairs)){
$data =~ /^$t1.*$t2=(.)/ && print "$1\n";
}
}

Which I like. (YMMV)

--
RGB
 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      12-17-2010
On 17/12/2010 14:01, Tad McClellan wrote:
> RedGrittyBrick<(E-Mail Removed)> wrote:
>> 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

>
>
> That data looks like an HoH to me...
>
>
>> 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);

>
>
> my %pairs = qw/A P B Q C R/;
>
> while (<DATA>) {
> my($type, %values) = split /[\s=]/;
> print "$values{$pairs{$type}}\n";
> }
>
>


Thanks. The inventiveness of clpm contributors is a great source of
inspiration.

A Sinan Ünür posted four solutions with some commentary in
<http://stackoverflow.com/questions/4470189/perl-pulling-pairs-of-values-from-an-array/4470720#4470720>.
I'm not sure I can summarise it adequately so I'll just post the URL
here rather than paste it all in verbatim.

--
RGB
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      12-17-2010
On Fri, 17 Dec 2010 11:59:38 +0000, RedGrittyBrick <(E-Mail Removed)> wrote:

>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)


You don't have to check all the pairs.
You can just get the value with a single if statement
per line of data. Its must faster this way.

Either of these will work (and there is validation so warnings
are not emitted) :

/^(\S+)/g && exists $pairs{$1} && /\G.*$pairs{$1}=(\S+)/
/^(\S+)(??{exists $pairs{$1} ? ".*$pairs{$1}=" : "(*FAIL)"})(\S+)/

-sln

-----------------------------
use strict;
use warnings;

my %pairs = qw/A P B Q C R/;

while ( <DATA> )
{
if (/^(\S+)/g && exists $pairs{$1} && /\G.*$pairs{$1}=(\S+)/) {
print $1,"\n";
}
}
__DATA__

A P=1 Q=2 R=3
B P=8 Q=2 R=7
C Q=2 P=1 R=3


Optional, uses extended regex (??{..}):
while ( <DATA> )
{
if (/^(\S+)(??{exists $pairs{$1} ? ".*$pairs{$1}=" : "(*FAIL)"})(\S+)/) {
print $2,"\n";
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pulling values from strings Michael Furmaniuk Ruby 4 04-15-2009 12:46 PM
generating random array of pairs puzzlecracker Java 10 07-29-2008 05:43 PM
pulling specific dup. elements out of an array Jon Hawkins Ruby 7 08-06-2007 04:48 PM
Algorithm to find pairs in array [can not order] obaqueiro@gmail.com Java 6 11-17-2006 05:33 PM
pulling set of unique values from a list Ben Davies Python 3 01-14-2004 08:03 PM



Advertisments