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

Greetings Monks, I have this little problem of looking for certain regex values. I managed to print out how many times a regex matches in a text but I need to know how I can print out when that regex has a certain value. For example from a PDB file I can print out how many ATOM lines are there -that match with ATOM string so to say- but I'm trying to print the amount of lines that have a certain number -like 2,3,4..whatever in $13 of regex.- can anyone provide me a general code line for this ? Thanks.

Replies are listed 'Best First'.
Re: searching something IN a regex ?
by kennethk (Abbot) on Aug 06, 2009 at 19:38 UTC
    Not to be dismissive, but please read How do I post a question effectively?. It would assist us terribly if you provided both some sample text that one would find in a PDB file as well as the code you've written that counts your lines.

    Barring additional information, you're probably best off using a hash to index captured values. Something like:

    use strict; use warnings; my %matches = (); while (<DATA>) { if (/Good\s(\d)/) { $matches{$1}++; } } foreach my $key (keys %matches) { print "$key: $matches{$key}\n"; } __DATA__ Good 1 Good 2 Good 3 Bad 1 Bad 2 Bad 1 Good 3 Good 1