On Sep 11, lyf said:

>hi, I am a perl beginner, and I am confused by $1.
>what does $1 ($2,and so on) mean?
>and how to use them?

The $DIGIT variables correspond to sets of ()'s in a regex.  Here's an
example:

  $pn = "1-800-555-1212";
  if ($pn =~ /^\d-(\d)\d{2}-(\d{3}-\d{4})$/) {
    print "toll free\n" if $1 eq '8';
    print "main number: $2\n";
  }

This would print:

  toll free
  main number: 555-1212

Each $DIGIT is related to the DIGITth set of capturing parentheses in a
regex.  The first set of () sets $1, the second set of () sets $2, and so
on.

For more information, read:

  perldoc perlrequick
  perldoc perlretut
  perldoc perlre

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to