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

Please see How do I post a question effectively?. In particular, note how the whitespace in your posted input has become mangled. This is because you did not include <code> tags. It's hard to help you code/debug if we see something different than what you see. Markup in the Monastery or Writeup Formatting Tips may be helpful.

For some sample code for list comparisons, which is close enough to be informative for your use case, see How can I tell whether a certain element is contained in a list or array? and/or How do I compute the difference of two arrays? How do I compute the intersection of two arrays? in perlfaq4.

You may also want to use a pre-rolled parsing solution, such as Text::CSV.

Have you actually written an code we can look at? Even if it doesn't work, it's very helpful in understanding how you are thinking.


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

Replies are listed 'Best First'.
Re^4: Seek and Find
by hbrown.bios (Initiate) on Feb 08, 2016 at 21:27 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.

        I have taken one perl programming class, and that was about 2 years ago and lets just say that I was taught more by google than the instructor.
      This is a poor set of sample data because they all fail to match the array.
      Bill