What's the output of the following script?

-------------------------------------------

#!/usr/bin/perl -w

$text = "abc_defg_hij_klm.";


@a =($text =~ m/[a-z]+[_|\.]/g);
print "array A, size: ", @a+0, ", elements: ";
print join (" ", @a);
print "\n";

@b =($text =~ m/([a-z]+)[_|\.]/g);
print "array B, size: ", @b+0, ", elements: ";
print join (" ", @b);
print "\n";


@c =($text =~ m/([a-z]+)([_|\.])/g);
print "array C, size: ", @c+0, ", elements: ";
print join (" ", @c);
print "\n";

-------------------------------------------

The output is as follows:

--------------------------------
array A, size: 4, elements: abc_ defg_ hij_ klm.
array B, size: 4, elements: abc defg hij klm
array C, size: 8, elements: abc _ defg _ hij _ klm .

--------------------------------

What's difference of the three patterns?

The first one: m/[a-z]+[_|\.]/g -- no curves in this pattern, and the results matching the whole pattern are saved in the result array.


The second one: m/([a-z]+)[_|\.]/g -- a pair of curves embrace the part of "[a-z]+", and the results matching this part are saved in the result array.

The third one: m/([a-z]+)([_|\.])/g -- two pair of curves embrace the two parts of the pattern respectively, and the results matching the two parts are all saved in the result array in the order of ($1, $2, $1, $2, ...).

See. Do you learn the rule?






Read More about [Perl: storing results of pattern matching in an array...