[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Arrarys and numbers



If you want all six orders of each set of three labels, it's easy to do,
but to get just the "cannonical" ordering of the three, you can use the
following (never tried) triple loop: 

for $first (0..$count-3){
   $l1 = $label[ $first ];
   for $second ($first+1..$count-2) {
      $l2 = $label[ $second ];
      for $third ($second+1..$count-1) {
	 $l3 = $label[ $third ];
	 $sum = $hash{ $l1 } + $hash{ $l2 } + hash{ $l3 };
         print "$l1 + $l2 + $l3 add up to $sum";
         }
      }
   }

This assumes that in addition to the hash, you build an array called
@label with $count entries from 0 to $count-1.

Dick

Responding to:
I have an associative array of words and numbers, i.e. ("Label1", 10,
"Label 2", 20, "Label3", 30, "Label4", 40...). This array goes on for 30
labels and numbers. I'd like to generate all combinations of three items,
listing the three label names and the sum of their numbers. For example,
"Label1, Label2 and Label3 equal 60. Label1, Label2 and Label4 equal
70."


***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch