my %hash_F2_1 =(); my %hash_F2_2 =(); my %hash_F2_3 = ();my %hash_F2_4= (); while(){ (my $probeset_id, my $origin, my $probeseq, my $pip, my $gc, my $affyscore) = split("\t", $_); push (@{$hash_F2_1{$origin}}, $pip); # This makes a hash of multiple value for each probeset ID } #clculate the max and put into hash (origin and max of pip) for (sort keys %hash_F2_1){ #print RESULTS "$_", "\t", max(@{$hash_F2_1{$_}}), "\n"; $hash_F2_2{$_} = max(@{$hash_F2_1{$_}}); } # then go thorough $hash_F2_2 and that has the key (origin) # and max as value and make a multivalue hash as follows: for my $k (sort keys %hash_F2_2){ my $v = $hash_F2_2{$k}; #print RESULTS "$k\t$v\n"; for (my $i=1; $i <= $v; $i++){ push (@{$hash_F2_3{$k}}, $i); # this hash (%hash_F2_3) is the hash of origins and PIPs from 1 to max } } LOOP1: foreach my $key(sort keys %hash_F2_3){ LOOP2: foreach my $position (@{$hash_F2_3{$key}}){ LOOP3: foreach my $key1(sort keys %hash_F2_1){ LOOP4: foreach my $position1(@{$hash_F2_1{$key1}}){ LOOP1: foreach my $key(sort keys %hash_F2_3){ LOOP2: foreach my $position (@{$hash_F2_3{$key}}){ LOOP3: foreach my $key1(sort keys %hash_F2_1){ LOOP4: foreach my $position1(@{$hash_F2_1{$key1}}){ if ($key =~ m/$key1/ && $position==$position1 ) { print "$key\t$position\t1\n"; } else { print "$key\t$position\t0\n"; } } } } }