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

Having issues with regex, input file:
# Title A 2 B
A and B can vary for any other letter. But each letter is associated with know value. I need to find what letters are presented then add up the values linked to them. Trying:
if ( $_ =~ /^\S\s+\S+\s[\A\b]/) {$sum = 2} elsif ( $_ =~ /^\S\s+\S+\s[\B\b]/) {$sum = 2} # then for next letter if ( $_ =~ /^\S\s+\S+\s\S\s+[\B\b]/) {$sum += 1}
it works fine in this case, but if i was to change A to B in input, then the sum printed is wrong??

Replies are listed 'Best First'.
Re: regex problem
by kutsu (Priest) on Apr 09, 2004 at 19:23 UTC

    If there is always going to spaces between them you might want to look at split

    #an example ($title, $letter1, $num, $letter2) = split(/ /,$_);

    Update: Fixed grammer error

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      would this be useful if the input file had multiple entries of the example line, with other infomration embedded?
      # TITLE C 4 B INFO INFO # TITLE A 4 B
        Yes it would ...
        #an example ($title, $letter1, $num, $letter2) = split(/ /,$_) if /^# TITLE/;

        Plankton: 1% Evil, 99% Hot Gas.