[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


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

Posted on 2012-12-21
Medium Priority
Last Modified: 2012-12-21
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:


Open in new window

Question by:Tolgar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

brendonfeeley earned 800 total points
ID: 38714243
This should work:

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

Author Comment

ID: 38714262
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?
LVL 27

Assisted Solution

wilcoxon earned 800 total points
ID: 38714270
Or, more verbosely:
if ($infoArray[3] =~ m{Client root:\s+(.*)}) {
    $clientRoot = $1;

Open in new window

Or, more compactly:
my ($clientRoot) = ($infoArray[3] =~ m{Client root:\s+(.*)});

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 38714271
You can do it this way:

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

Only difference is the capturing group in the regex.
LVL 27

Expert Comment

ID: 38714275
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

LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 400 total points
ID: 38714283
It did work, but the only thing output is the entire match. This is because you didn't specify any capture groups (parentheses, like brendonfeeley demonstrated). When you capture the result of a regex in list context in Perl, the capture groups are what are output to the variable. Since you didn't specify any groups, the output reverted to the overall match.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question