in reply to Removing Poly-ATCG from and Array of Strings

1. Remove the 'portion' calculations by using the integer product of 'lim' and 'length' in the comparisons. (Eliminates floating point calcuations and comparisons in the loop!)
2. Put the 'tr's in the comparison so that you can short-circuit having to do all the translations.
3. Use 'grep'.
4. Return an array reference from the subroutine.
#!/usr/bin/perl use strict; use warnings; MAIN: { my @set = qw (AAAAAT ATCGAT TTTTTG GCCCCC GTGGGG); my $lim = 0.75; print("BEFORE: ", scalar(@set), "\n"); my $results = remove_poly(\@set, $lim); print("AFTER: ", scalar(@$results), "\n"); #print(join("\n", @$results), "\n"); } exit(0); sub remove_poly { my $array = $_[0]; my $lim = $_[1]; $lim = int($lim * length($$array[0])); my @results = grep { (($_ =~ tr/A//) <= $lim) && (($_ =~ tr/T//) <= $lim) && (($_ =~ tr/C//) <= $lim) && (($_ =~ tr/G//) <= $lim) } @$array; return (\@results); } # EOF

Replies are listed 'Best First'.
Re^2: Removing Poly-ATCG from and Array of Strings
by Limbic~Region (Chancellor) on Jun 09, 2005 at 19:30 UTC
    jdhedden,
    It is only fair to calculate $lim once if there is some guarantee that all strings will be the same length. It is possible that is what was intended by ewijaya's example since I am the only one that dynamically calculated the limit each iteration.

    Cheers - L~R