Link to home
Create AccountLog in
Avatar of Tolgar
Tolgar

asked on

How to assign the match of string to another variable in perl?

I try to assign the matched result to another variable but instead the other variable gets the value 1 which means that there is match.

Here is the example:

my ($clientRoot) = $infoArray[3] =~ /Client root:\s.*/;

Open in new window



$infoArray[3]  is something like this: Client root: some/path/in/here

Expected result for $clientRoot:

some/path/in/here

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of brendonfeeley
brendonfeeley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Tolgar
Tolgar

ASKER

I normally use my other pattern but why didn't it work in here? Is it because I have an array instead of a single variable?
SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
You can do it this way:

my ($clientRoot) = $infoArray[3] =~ /Client root:\s(.*)/;

Only difference is the capturing group in the regex.
Your original pattern did not work because you forgot the capturing parens (see my second option for the correct equivalent of your original idea (which I missed when reading the question)).  The parens around the match may not be required in mine but I usually include them just to make precedence explicit.
my ($clientRoot) = ($infoArray[3] =~ m{Client root:\s+(.*)});

Open in new window

SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account