L
leifwessman
Hi!
I need to extract a certain value from a text. But the result isn't
always in the variable $1 - it might be in $2, $3, $4 or some other
predefined variable.
Some code to illustrate my problem:
$regexp = "(\d)(\w)(\d)";
$numb = 3; # Means the result I'm looking for is in $3
# I don't know this number, it's submitted
by user
# and may differ
if ($data =~ /$regexp/) {
print $numb; # does not work, prints "3"
# alternative solution that works
# but it's UGLY
if ($numb == 1) {
print $1;
} elsif ($numb == 2) {
print $2;
} elsif ($numb == 3) {
print $3;
}
# is there another way?
}
Thanks for any input!
Leif
I need to extract a certain value from a text. But the result isn't
always in the variable $1 - it might be in $2, $3, $4 or some other
predefined variable.
Some code to illustrate my problem:
$regexp = "(\d)(\w)(\d)";
$numb = 3; # Means the result I'm looking for is in $3
# I don't know this number, it's submitted
by user
# and may differ
if ($data =~ /$regexp/) {
print $numb; # does not work, prints "3"
# alternative solution that works
# but it's UGLY
if ($numb == 1) {
print $1;
} elsif ($numb == 2) {
print $2;
} elsif ($numb == 3) {
print $3;
}
# is there another way?
}
Thanks for any input!
Leif