Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: filtering an array

by ww (Archbishop)
on Sep 02, 2012 at 01:50 UTC ( #991225=note: print w/replies, xml ) Need Help??

in reply to filtering an array

Alternate approach (with a little error checking, which I hope is so basic and transparent as to need no explanation):

You might want to upend your thought process. Simply warn about any elements of your data that have "G" or "T" (and, as you'll see in the code below, any non-conforming data -- letters other'n ACTG; too many or few letters, etc.) and then next past them without saving. Save the desired elements (the digits) to your @positions array, only after substituting away any combinations of "A" and "C" and -- when you've processed all the data, spit out the positions that satisfy your criteria:

#!/usr/bin/perl use 5.014; # 991184 my @positions; my @data = ('1 ACAC', '2 AGAC', '3 AGTC', '4 ACCA', '5 DUMMY', '6 ACAATG', '7 CAAC', '8 acaacc', '9 aca ', ); for my $data(@data) { if ( $data =~ /[GT]/ || $data =~ /[^[ACTG]{4}$/) { say "Data ERROR or contains G or T ( $data )"; next; } elsif ( $data =~ /^\d+ [AC]{4}$/i ) { # too many; too few? cove +red here $data =~ s/ [AC]{4}//i; chomp $data; push @positions, $data; } else { say "Problem with data? $data"; } } print "\n Good data at positions: "; for my $position (@positions) { print "$position "; # depending on size of valid position +s, you may } # want to stack them vertically -- # simply replace the 'print' with 'sa +y' say "\n Done";


C:\ Data ERROR or contains G or T ( 2 AGAC ) Data ERROR or contains G or T ( 3 AGTC ) Data ERROR or contains G or T ( 5 DUMMY ) Data ERROR or contains G or T ( 6 ACAATG ) Data ERROR or contains G or T ( 8 acaacc ) Data ERROR or contains G or T ( 9 aca ) Good data at positions: 1 4 7 Done

Done in babytalk, to some extent, to ensure clarity. Compare OP's questions about the Monks' responses, above.

Replies are listed 'Best First'.
Re^2: filtering an array
by prbndr (Acolyte) on Sep 02, 2012 at 03:12 UTC

    yes, i ended up doing exactly this ww. i realized i was going after too complicated of a solution instead of trying a much simpler approach. glad we were on the same page!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991225]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2018-03-17 20:40 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (226 votes). Check out past polls.