Richard Lee wrote:
say I have

my $var1 = 'abcdefg';

my @array = ( 'abcdefg_3432', 'defg_333', 'abcdefg_' , 'abcdefg_a' );

Let's say I want to go through the array to see if $var1 exists and also to see if it followed by _ and then 4 digits (only first one should quailfy , abcdefg_3432 )

I tried,

for (@array) {
  next unless $_ =~ m#$var1_\d\d\d\d# ;
  print "$_\n";
}


obviously this does not work.. so I tried

It works here:

$ perl -le'
my $var1 = "abcdefg";
my @array = qw( abcdefg_3432 defg_333 abcdefg_ abcdefg_a );
for ( @array ) {
    next unless $_ =~ m#$var1_\d\d\d\d#;
    print;
    }
'
abcdefg_3432

As to why it "works", turning on warnings will give you a hint.


   next unless $_ =~ m#{$var1}_\d\d\d\d#;

which also didn't work(? should it have worked?)

That should be m#${var1}_\d\d\d\d# instead and that works also:

$ perl -le'
my $var1 = "abcdefg";
my @array = qw( abcdefg_3432 defg_333 abcdefg_ abcdefg_a );
for ( @array ) {
    next unless $_ =~ m#${var1}_\d\d\d\d#;
    print;
    }
'
abcdefg_3432



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to