A
Andy
To all,
I have an array:
my @uniq=qw{a b c d}
and a second array with pairs of elements:
my @pairs = ( ['z','g'], ['a','c'], ['b','a'] )
I would like to identify all possible pairs of elements (order not
important) in @uniq and add them to @pairs if @pairs doesn't already
include these pairs. I would also like to print out any new pair added
to @pairs. So, all possible pairs in @uniq=
ab ac ad bc bd cd
ab and ac already occur in @pairs but the other 4 pairs do not.
Therefore, @pairs should be updated to:
my @pairs = ( ['z','g'], ['a','c'], ['b','a'],['a','d'], ['b','c'],
['b','d'],['c','d'] )
The code would print out:
ad
bc
bd
cd
My attempt is shown below. I think that I am very close. Any
suggestions would be welcome. Thanks.
#!/usr/bin/perl -w
use warnings;
use strict;
my @uniq=qw{a b c d};
my @pairs = ( ['z','g'], ['a','c'], ['b','a'] );
my $b=1;
my %hash1;
@hash1{@uniq} = ();
my $d;
my $e;
my @bud1;
my @hash1;
my $f;
my $g;
my $done=0;
my $k=$#pairs;
foreach my $i ( 0 .. $#uniq-1 ) {
foreach my $j ( $b .. $#uniq ) {
$f=$uniq[$i];
$g=$uniq[$j];
while ($done==0){
foreach my $p ( 0 .. $k ) {
if( ($pairs[$p][0]=$f or $pairs[$p][1]=$f) and
($pairs[$p][0]=$g or $pairs[$p][1]=$g) ){
$done=1;
}
else {
$d=$f;
$e=$g;
@bud1=( [$d,$e] );
push(@pairs, @bud1);
print $d." ".$e;
print "\n";
$done=1;
}
}
$done=0;
}
print "\n";
}
$b=$b+1;
}
I have an array:
my @uniq=qw{a b c d}
and a second array with pairs of elements:
my @pairs = ( ['z','g'], ['a','c'], ['b','a'] )
I would like to identify all possible pairs of elements (order not
important) in @uniq and add them to @pairs if @pairs doesn't already
include these pairs. I would also like to print out any new pair added
to @pairs. So, all possible pairs in @uniq=
ab ac ad bc bd cd
ab and ac already occur in @pairs but the other 4 pairs do not.
Therefore, @pairs should be updated to:
my @pairs = ( ['z','g'], ['a','c'], ['b','a'],['a','d'], ['b','c'],
['b','d'],['c','d'] )
The code would print out:
ad
bc
bd
cd
My attempt is shown below. I think that I am very close. Any
suggestions would be welcome. Thanks.
#!/usr/bin/perl -w
use warnings;
use strict;
my @uniq=qw{a b c d};
my @pairs = ( ['z','g'], ['a','c'], ['b','a'] );
my $b=1;
my %hash1;
@hash1{@uniq} = ();
my $d;
my $e;
my @bud1;
my @hash1;
my $f;
my $g;
my $done=0;
my $k=$#pairs;
foreach my $i ( 0 .. $#uniq-1 ) {
foreach my $j ( $b .. $#uniq ) {
$f=$uniq[$i];
$g=$uniq[$j];
while ($done==0){
foreach my $p ( 0 .. $k ) {
if( ($pairs[$p][0]=$f or $pairs[$p][1]=$f) and
($pairs[$p][0]=$g or $pairs[$p][1]=$g) ){
$done=1;
}
else {
$d=$f;
$e=$g;
@bud1=( [$d,$e] );
push(@pairs, @bud1);
print $d." ".$e;
print "\n";
$done=1;
}
}
$done=0;
}
print "\n";
}
$b=$b+1;
}