in reply to Re: Seek and Find
in thread Seek and Find

So here is an example of some of may data: GLU 66 PRO 436 9.116044197 Blank Asterisk GLU 67 ASN 427 9.34269896 Asterisk Asterisk What I would like it to do is if column 2 matches a number in the hash (or array), and the value in column 4 is within a given range of numbers, and column 7 matches a search phrase such as "Asterisk" it would print the entire matching line to a new file, and then for it to continue searching until the end of the document.

Replies are listed 'Best First'.
Re^3: Seek and Find
by kennethk (Abbot) on Feb 08, 2016 at 19:16 UTC
      here is the code I have so far:
      #!/usr/bin/perl @cand (63, 66, 69, 72, 73, 74, 95, 96, 98, 134, 135, 137, 138, 139, 14 +0, 159, 162, 172, 173, 175, 177, 178, 197, 198, 201, 210, 225, 232, 2 +37, 240, 243, 246, 247); open (<CONTACT0>, "Contactmap00_0.txt"); while (<CONTACT0>) { foreach $c (@cand){ foreach $_ { @line = split (//, $_); if $c = $line[2] { if $line[4] >= 133 { if $line[4] <= 298 { if $line[6] != ['Asterisk' 'Colon']{ print $_ "\n"; } else print "Mismatch at level Type \n";} else print "Too large for range\n";} else print "To small for range \n";} else print "Not a match to candidates\n";} } }
      My data looks like this
      ILE 105 GLY 404 9.31639356 Blank Asterisk ILE 105 VAL 430 7.716578207 Blank Colon GLY 106 SER 429 9.615782075 Asterisk Asterisk GLY 106 VAL 430 6.799878306 Asterisk Colon GLY 106 LEU 433 8.984377971 Asterisk Colon GLY 106 SER 434 9.609582172 Asterisk Asterisk GLY 107 SER 428 8.263658329 Asterisk Asterisk GLY 107 SER 429 6.30787898 Asterisk Asterisk GLY 107 VAL 430 4.124362412 Asterisk Colon GLY 107 LEU 431 7.670615378 Asterisk Asterisk GLY 107 ALA 432 8.308317297 Asterisk Period GLY 107 LEU 433 5.934571065 Asterisk Colon GLY 107 SER 434 7.71234967 Asterisk Asterisk LYS 108 ASN 427 8.67223833 Blank Asterisk LYS 108 SER 428 5.458321198 Blank Asterisk
        Fore the moment only some correction: the block
        while (<CONTACT0>) { foreach $c (@cand){ foreach $_ {
        is totally wrong for me: read it two or more times in english to see what i mean.

        Also if $c = $line[2] { is totally wrong: maybe you mean if ($c == $line[2]) { instead?

        basic Perl documentation can be very helpful to learn control structure and loops.

        If i'll still be awake after some duty i'll add some code.

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        Hmm, I very much doubt that this code is going to compile.

        Besides, I don't think that the approach is good.

        My basic idea is something like this:

        use strict; use warnings; my %required = map { $_ => 1 } 63, 66, 69, 72, 73, 74, 95, 96, 98, 134 +, 135, 137, 138, 139, 140, 159, 162, 172, 173, 175, 177, 178, 197, 19 +8, 201, 210, 225, 232, 237, 240, 243, 246, 247; open my $CONTACT0, "<", "Contactmap00_0.txt" or die "unable to open Co +ntactmap00_0.txt $!";; while (<CONTACT0>) { chomp; my @line = split (//, $_); print "$_: Not a match\n" and next unless exists $required{$line[1 +]}; # your other conditions here ... }
        What materials are you using to learn Perl? You have a substantial amount of illegal syntax there. I would highly recommend perusing http://learn.perl.org, particularly A Beginner's Introduction to Perl 5.10.

        Obvious errors you should be aware of:

        • Checking for equality is == for numeric equivalence and eq for string-wise equivalence. See Equality Operators in perlop.

        • In general, conditional clauses and lists need to be in parentheses. See Compound Statements in perlsyn.

        • Control structures require blocks wrapped in curly brackets. Again, See Compound Statements in perlsyn.

        • You use the default variable a lot without actually using its features. That's a recipe for disaster. See $_ in perlvar.

        • Arrays are indexed starting at 0, not 1.

        • $line[6] != ['Asterisk' 'Colon'] (in addition to missing a comma) really doesn't do what you think. You need to check equality twice with an or statement, and even then you need stringwise equivalence with ne, not numeric equivalence.

        Where did you come up with this syntax? What is your programming background? I do not mean to be disrespectful.


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        This is a poor set of sample data because they all fail to match the array.
        Bill