P
Peng Yu
Hi,
I want to use some variable in regex. But the below code does not work
as I expected. Can somebody let me know what I am wrong?
Thanks,
Peng
#!/usr/bin/perl
use warnings;
use strict;
my @array = ('a' , 'b');
my @strings = ('aa', 'ab', 'ba', 'bb', 'cc', 'cd');
foreach (@array) {
print "$_\n";
my @subset = grep(/^$_.+$/, @strings);
# The above line does not work as I want
# I want search for all the stings in @strings
# that are started with a letter in @array.
foreach (@subset) {
print "$_\n";
}
}
print "here\n";
my @subset = grep(/^a.+$/, @strings);
foreach (@subset) {
print "$_\n";
}
I want to use some variable in regex. But the below code does not work
as I expected. Can somebody let me know what I am wrong?
Thanks,
Peng
#!/usr/bin/perl
use warnings;
use strict;
my @array = ('a' , 'b');
my @strings = ('aa', 'ab', 'ba', 'bb', 'cc', 'cd');
foreach (@array) {
print "$_\n";
my @subset = grep(/^$_.+$/, @strings);
# The above line does not work as I want
# I want search for all the stings in @strings
# that are started with a letter in @array.
foreach (@subset) {
print "$_\n";
}
}
print "here\n";
my @subset = grep(/^a.+$/, @strings);
foreach (@subset) {
print "$_\n";
}