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:
$infoArray[3] is something like this: Client root: some/path/in/here
Expected result for $clientRoot:
Here is the example:
my ($clientRoot) = $infoArray[3] =~ /Client root:\s.*/;
$infoArray[3] is something like this: Client root: some/path/in/here
Expected result for $clientRoot:
some/path/in/here
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can do it this way:
my ($clientRoot) = $infoArray[3] =~ /Client root:\s(.*)/;
Only difference is the capturing group in the regex.
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+(.*)});
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER