Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Pair of items

by swkronenfeld (Hermit)
on Jul 29, 2003 at 20:10 UTC ( #278962=note: print w/replies, xml ) Need Help??


in reply to Pair of items

artist,

A better problem description is necessary. Do you just want to find any pair of items with no similar numbers? Do you want to continue to find them after the first one? If so, can you use the same item in multiple matches? Assuming the answers to the questions are yes, yes, and no, here's a basic skeleton for your code, using dragonchild's above subroutine

for(my $i=0; $i<@array; $i++) { next if(!$array[$i]); for(my$j=0; $j<@array; $j++) { next if($i == $j); next if(!$array[$j]); if(is_ok($array[$i], $array[$j]) { print "Found a match, $array[$i], $array[$j]"; $array[$i] = $array[$j] = ""; } } }
edit: originally had next if(!$i) and next if(!$j) which dragonchild pointed out to me. Corrected above (next if(!$array[$i]))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://278962]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-09-30 02:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (125 votes). Check out past polls.

    Notices?