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

I have tried and cant seem to get just one match on each one of these files:
4201xcab.exe

0501i32.exe
I want to find one occurence of each and if a match then put it into a variable: Here is my attempt but not quite working:
if ($_ =~ /(\d{4}i32\.exe)/) { $word1 = $1;}
if ($_ =~ /(\d{4}xcab\.exe)/) { word2 = $1;}

Replies are listed 'Best First'.
(jeffa) Re: Matching problem
by jeffa (Bishop) on May 08, 2002 at 16:50 UTC
    What exactly is not working? You appear to have a syntax error - word2 is not prefixed with $. This worked for me:
    use strict; my ($word1,$word2); while (<DATA>) { if ($_ =~ /(\d{4}i32\.exe)/) { $word1 = $1; } if ($_ =~ /(\d{4}xcab\.exe)/) { $word2 = $1; } } print "$word1\n"; print "$word2\n"; __DATA__ 4201xcab.exe 0501i32.exe

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Matching problem
by bassplayer (Monsignor) on May 08, 2002 at 16:48 UTC
    Update: Withdrew embarrassingly stupid answer (misread question).
    bassplayer <- needs stronger coffee

    bassplayer