[ILUG] [OT] perl help

Philip Reynolds phil at redbrick.dcu.ie
Wed Sep 13 17:17:15 IST 2000


Paul Jakma's [paulj at itg.ie] 28 lines of dribble included:
:>as per kevin l.'s suggestion i've decided to try my hand at perl
:>(and cause this list could do with a spate of perl newbie questions)..
:>
:>so i'm trying to capture parts of the results of a /regex/ into
:>variables, eg something along the lines of:
:>
:>basically i'm trying to figure out how to do something like the
:>following:
:>
:>while (<MSGS> ) {
:>        if (/^([A-Za-z]{3}) ([0-9]{2}) ([0-9{2}).*$regex[$i]/) {
:>                $month[$i] = \1;
:>                $date[$i] = \2;
:>                $hour[$i] = \3      
:>}

What you're doing (assuming your regexp is correct) is something similar to
the following?

#!/usr/bin/perl
# Testing Read-Only vars

$_ = "First Second";
/(\w+)\W+(\w+)/;

print "$1 \n";
print "$2 \n";

It can also be done using

#!/usr/bin/perl
$_ = "First Second";
($first, $second) = /(\w+)\W+(\w+)/;
print "$first\n";
print "$second\n";


Y'see, \1 marks the first part of the paranthesised values, and $1, $2, $3
etc. are set after the regular expression has executed.
Phil.




More information about the ILUG mailing list