Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: filtering an array

by ww (Bishop)
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";

Output:

C:\ 991184.pl 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.


Comment on Re: filtering an array
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2014-10-22 16:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (119 votes), past polls