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

Hello , I am trying to grep for a name within a big file with a lots of data somthing like :
blablblablabjb blabalbbaabb;b blablababja;b Type: Divorced dffggf blbab
so I am greping for the value in the Type which is Divorced and thats in the fourth line , I want somthing like :
$goal = Divorced;
I am doing the follwing :
if (m/^(Type:)\s*(\w+)/); { $goal = $1; }
can someone just tell me what is wrong with my code !! thanks

Replies are listed 'Best First'.
Re: grep for a name
by DamnDirtyApe (Curate) on Jul 30, 2002 at 20:35 UTC
    1. Drop the semicolon from the end of your if.
    2. You want $goal = $2, not $1. (Even better, drop the parentheses around Type:.)
    HTH
    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: grep for a name
by VSarkiss (Monsignor) on Jul 30, 2002 at 20:38 UTC

    You don't mention what's not working up to your expectations, but my guess is that $goal being set to Type: is not what you want. You're unnecessarily capturing a constant string. You should either not capture it: if (m/^Type:\s*(\w+)/) { $goal = $1 }or use non-capturing parens: if (m/^(?:Type:)\s*(\w+)/) { $goal = $1 }or keep the pattern the way it is and pull out the second pair of parens: if (m/^(Type:)\s*(\w+)/) { $goal = $2 }Many more details at perlop (look for "Quotes and quote-like operators") and perlre.

    This is all guesswork on my part about what the problem is. Hope I guessed right!