in reply to Re: clustering pairs
in thread clustering pairs

i tried fixing it using arrays :)
#!/usr/bin/perl open FILE,"sampledata"; @arr = <FILE>; chomp @arr; close(FILE); local $,="\n"; while(@arr) { my @reslt; @str = shift @arr; push(@reslt,$str[0]); while (@str) { $flag = 0; $str = shift @str; $s1,$s2,$flag) = split(/ /,$str); my $count = -1; my $acount = 0; #to arrange o/p foreach(@arr) { $count++; if($_ =~ /$s1|$s2/) { $acount++; if($acount == 2 || $flag == 1) { unshift(@reslt,$_); unshift(@str,$_." 1"); } else { push(@reslt,$_); push(@str,$_); } splice(@arr,$count,1); } } } print @reslt,"\n"; }

Replies are listed 'Best First'.
Re^3: clustering pairs
by ig (Vicar) on Dec 05, 2008 at 04:36 UTC

    You have almost arrived at yet another solution to your problem. You have made good progress from your initial posting.

    I agree with GrandFather's comments.

    Also, note the warning regarding modification of the list a foreach loop is iterating over, in perlsyn

    If any part of LIST is an array, "foreach" will get very confused if you add or remove elements within the loop body, for example with "splice". So don’t do that.

    Your "ID" is not compatible with the clusters you gave in your original post, which will only be produced if you ignore the characters preceding and including the period in each half of the string.

    These issues are addressed somewhat in the following: