tags:

views:

118

answers:

5

What does this line do in Perl?

my @parsedarray = ($rowcol =~ m/([A-Z]?)([0-9]+)/);

$rowcol is something like A1, D8 etc... and I know that the script somehow splits them up because the next two lines are these:

my $row = $parsedarray[0];
my $col = $parsedarray[1];

I just can't see what this line does ($rowcol =~ m/([A-Z]?)([0-9]+)/); and how it works.

+1  A: 

It:

  • matches against the regular expression (zero or more capitalised letters, followed by one or more numbers.)
  • Captures two groups:
    • zero or more letters
    • one or more numbers
  • Assigns those captured groups to the @parsedarray array

Example code to test:

use Data::Dumper;
my $rowcol = "A1";
my @parsedarray = ($rowcol =~ m/([A-Z]?)([0-9]+)/);
print Dumper(\@parsedarray);

yields:

$VAR1 = [ 'A', '1' ];

Note that if the string had no leading capitalised letter (e.g. "a1") then it would return an empty string for $parsedarray[0].

mopoke
A: 

My Perl is a little rusty but if I understood your question, the answer is that it matches the regular expression of :

optional any capital letter between a through z followed by one or more number digits, and assigns it to rowcol

Amir Afghani
+2  A: 

The operator m// is a pattern match, basically a synonym of //. This matches an optional first letter and then 1 or more digits in row column. An array is returned as the result of the match with each element containing one of the matched groups (in brackets). Therefore $parsedarray[0] contains the letter (or nothing) and $parsedarray[1] contains the digits.

Dean Povey
+7  A: 

([A-Z]?) means capture at most one uppercase letter. ([0-9]+) means match and capture at least one digit.

Next time, you can install YAPE::Regex::Explain to tell you what's going on. eg

 use YAPE::Regex::Explain;
 my $regex = "([A-Z]?)([0-9]+)";
 my $exp = YAPE::Regex::Explain->new($regex)->explain;
 print $exp."\n";

Note that m// in list context returns all the captured sub-strings.

ghostdog74
+3  A: 

Broken into pieces, this is what's going on:

my @parsedarray =                   # declare an array, and assign it the results of:
                  (
                   $rowcol =~       # the results of $rowcol matched against
                              m/    # the pattern:
                                ([A-Z]?)   # 0 or 1 upper-case-alpha characters (1st submatch),
                                           # followed by
                                ([0-9]+)   # 1 or more numeric characters (2nd submatch)
                               /x          # this flag added to allow this verbose formatting
                  );                # ...which in list context is all the submatches

So if $rowcal = 'D3':

my @parsedarray = ('D3' =~ m/([A-Z]?)([0-9]+)/);  # which reduces to:
my @parsedarray = ('D', '3');

You can read about regular expressions in depth at perldoc perlrequick (a quick summary), perldoc perlretut (the tutorial), and perldoc perlre (all the details), and the various regular expression operations (matching, substitution, translation) at perldoc perlop.

Ether