| [reply] [d/l] [select] |
Thanks gjb, I feel like an idiot... I've come unbound!
| [reply] |
Use qr//. It is used to store a regex in a variable. Think you want something like this.
my $re = qr/.+\-(.+)/;
$foo =~ s/$re/$1/;
but if all are in domain-computer form, you could just use
$foo =~ s/.+\-(.+)/$1/;
Update:
but on second thought the first way could run in a loop and you could just define the regex before the loop using qr//. That would probably be more efficient.
Example:
use strict;
my $re = qr/.+\-(.+)/;
while(<DATA>){
s/$re/$1/;
print;
}
__DATA__
domain-comp1
domain-comp2
domain-comp3
Output:
comp1
comp2
comp3
| [reply] [d/l] [select] |
Thanks tcf22! Thanks for taking the time to answer my question! That's exactly what I was trying to achieve.
“Pie Iesu Domine. Dona eis Requiem.” Whack!
| [reply] |
The only issue with using a var as the pattern (other than your missing =~ or course) is that it MAY contain regex metachars. quotemeta and its RE friends \Q and \E are your friends. Do:
$foo =~ s/\Q$find\$E/$replace/g;
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] [d/l] |
Adiing to what the others said, if $scalar does not contain a RegEx, but a string that you want to match, you might be interested in the quotemeta(...) function, which avoids, for example, $scalar = '.*'; to match the whole string and transforms it to \.\*. Just do something like
# first possibility
$scalar = quotemeta('.*');
$foo =~ s/$scalar/string/;
# second possibility
chomp($scalar = <>);
$scalar = quotemeta($scalar);
$foo =~ s/$scalar/string/;
Please note also that a subtitution substitues a pattern by a string, and not by a pattern. (This is probably what you meant, but some people try things like s!<a>\d{3}</a>!\d{3}! and wonder why it doesn't work)
Cheers, CombatSquirrel. | [reply] [d/l] [select] |
while ( <DATA> ) {
my $comp = (split /-/)[-1];
print $comp;
}
__DATA__
domainA-comp1
domainB-comp2
domainC-comp3
dave
| [reply] [d/l] |