Computer science: Perl regex: grouping with parentheses

Self-tutoring about computer science: the tutor mentions using parentheses in Perl regular expressions to form groups.

A useful feature of regular expressions is the optional use of parentheses. There are numerous reasons to use them, but one is to report parts of the match.

Consider the following:

$strng=”Hehe_12345_68d-910″;

if($strng=~m/(hehe(_[\d]+_)([\d]+d)(-[d]+))/i){

print “$1\n$2\n$3\n$4\n”;

}
else{

print “no match”;
}

The output would be (assuming no typos:)

Hehe_12345_68d-910
_12345_
68d
-910

$1 denotes the contents of the first set of parentheses in the matching pattern, $2 the second, and so on. The groupings can be used to organize the output from the match.

Source:

perldoc.perl.org

roberts perl tutorial

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Perl: use feature ‘say’;

More self-tutoring about Perl: the tutor mentions another way to enable ‘say’ in Perl.

In my post from Aug 15 I mention the Perl feature say and that, to enable it, a user might key

use v5.10;

below the she-bang line. (Apparently, a version past 5.10 may also suffice.)

I’ve read since that the line

use feature ‘say’;

also enables the use of say.

Source:

perldoc.perl.org

www.sthomas.net

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Using say in Perl

Tutoring computer science, new language ideas will always be noticed. The tutor mentions the Perl construct say.

use v5.10; #Apparently you need this to use say.
$var1=”Hey there:)”;
say $var1; #same effect as print “$var1\n”;

I’ve never known of say, but I tried it on a console and indeed, it works. I needed to include use v5.10; for say to work. I understand, however, that the version can be higher.

Source:

perldoc.perl.org

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.