Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I wondered if I could have some help using flags.

I am simply checking through a file of records, looking for any that match and then extracting that particular record.

My problem is that if I find one that matches, my code takes all the records from then onwards, regardless of whether they pass the criteria.

The file of records looks something like this:

>RC|ae000001||99-143 ACGFEHIGJUHIDFHDIHGFHHIIFHIHHFBIHB DHFIJHGIJFIGKFKPDOKFKFKFFFFF >RC|ae000002|150-179 THDFHOIHDIOVIJJOFPJGFJVOKVKFFKBDJK HHTHTHTHAAAAA >RC|ae000003|185-207 KLJDJFGJFJDJFKJKJFSJFKDHSHKFJKDJDK SAJSAJJSDSPIDOSDKSKFKF
I am simply trying to check the numbers (e.g 99-143) against an array of numbers and pull out that particular record if it matches. Please have a look at my code to see where i'm going wrong: Many thanks
foreach my $l (@file) { if (($l =~ /^>(\w{2})(.{1})(\w{2}\d{6}.{2})(\D?)(\d+)(\W{1})(\d+)/ +)) { $current = undef; if ($4) { $hit = "$7-$5"; } else { $hit = "$5-$7"; } } foreach my $k (@positions) { if ($k == $hit) { $in_gene = 1; } } if ($in_gene) { $gene .= $l; } unless ($l =~ /^>/) { next; } } print $gene;

Replies are listed 'Best First'.
Re: using flags
by Abigail-II (Bishop) on Oct 07, 2003 at 13:33 UTC
    Well, once you set $in_gene to 1, it never gets set to a false value again. Try adding a my $in_gene; at the beginning of the loop.

    Abigail

      the problem is that I need this variable outside of the loop too..... any other ideas?
        If you need the variable, that's not a problem as using 'my $in_gene' just creates a different $in_gene for the loop. If you need the value, you'll have to use 2 variables, one to signal that there was a match somewhere (which is what $in_gene is doing right now), and one that signals a match at the current iteration. For that, a lexical variable will be perfect.

        Abigail

Re: using flags
by cchampion (Curate) on Oct 07, 2003 at 14:33 UTC

    Since Abigail-II has already told you where the problem lies, I would like to contribute by showing you a previous post where the good and evil of flags are evaluated in depth:
    Flag variables

Re: using flags
by Not_a_Number (Prior) on Oct 07, 2003 at 17:21 UTC

    A virtually identical question was asked by another (?) anonymous monk yesterday. Check stajich's answer here.

    hth

    dave