in reply to Calculating variable {n} in regex match

It allows for a simple variable to be used, instead of an expression including a variable:
use warnings; use strict; my $foo = shift || 12; my $i = 2; print "Is \`$foo' a two-digit number ... "; #my $rc = ($foo =~ /^[0-9]{($i + 1)}$/) ? 'yes' : 'no'; my $rc = ($foo =~ /^[0-9]{$i}$/) ? 'yes' : 'no'; #my $rc = ($foo =~ /^[0-9]{2}$/) ? 'yes' : 'no'; print $rc,"\n"; __END__ Is `12' a two-digit number ... yes

Add re to your code to see what is happening under the hood:

use re 'debug';

Replies are listed 'Best First'.
Re^2: Calculating variable {n} in regex match
by atreyu (Sexton) on Jun 05, 2012 at 17:56 UTC
    toolic,

    i know i can merely do $i = 2, but i am wondering if it is possibly to instead not have to set/reset any variable, but instead calculate it using the ($n + 1) convention.

    thanks for the use re 'debug' tip, never used that before.