in reply to Re^2: Regex (?{ code }) and use re 'eval'
in thread Regex (?{ code }) and use re 'eval'

What do you mean by "returns something"? I'm going to assume you mean "returns something defined". Adjust as needed.

foreach my $range (values %Build) { $message =~ s{ (\[ ([$range]+) \]) }{ my $val = return_number("$2"); defined($val) ? $val : $1 }exig }

If return_number is costly, you can memoize the returned value.

my %memoize; foreach my $range (values %Build) { $message =~ s{ (\[ ([$range]+) \]) }{ my $val = exists($memoize{$2}) ? $memoize{$2} : return_number("$ +2"); $memoize{$2} = $val; defined($val) ? $val : $1 }exig }

Why is Build a hash instead of an array? You don't use the keys. Also, Build is not a particularly good name.

Replies are listed 'Best First'.
Re^4: Regex (?{ code }) and use re 'eval'
by SFLEX (Chaplain) on Nov 21, 2007 at 20:47 UTC
    You assumed right ^^. looks good and I will be playing with it.
    the function I'm using wont be costly and the hash key will be used in the final version. I wanted to keep it as simple as possible in my examples.

    My final code would look something like this.
    # Hash format: $Build_AUBBC{$name} = $pattern . '||' . $type . '|| +' . $fn; foreach my $b_key (keys %Build_AUBBC) { warn "ENTER foreach do_build_tag $self" if $DEBUG_AUBBC; my ($pattern, $type, $fn) = split (/\|\|/, $Build_AUBBC{$ +b_key}); $message =~ s/(\[$b_key\:\/\/([$pattern]+)\])/ my $ret = check_build_tag( $2 , $fn ) || ''; $ret ? $ret : $1; /exig if ($type eq 1); $message =~ s/(\[$b_key\]([$pattern]+)\[\/$b_key\])/ my $ret = check_build_tag( $2 , $fn ) || ''; $ret ? $ret : $1; /exig if ($type eq 2); }

    Thanks ^^