Re: regex variable matching when it's more than variable

2008-04-20 Thread Richard Lee

Rob Dixon wrote:

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# ;



This is much more concisely written

  next unless /$var1_\d\d\d\d/;

  

   print "$_\n";
}


obviously this does not work.. so I tried



Why do you say 'obviously'. It can't be so obvious or you wouldn't have
tried it in the first place. What problem did you see? The code you
posted prints

  abcdefg_3432

Try it!

But there are several things wrong. First of all, always

  use strict;
  use warnings;

at the start of your program. That would stop the code above compiling
and would tell you what error you made. Perhaps you should try that now too.

If you tried it, it would have said

  Global symbol "$var1_" requires explicit package name

So it's including the trailing underscore as part of the variable's
name. But I suspect you already have a clue about this problem,
otherwise why write this

  

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

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



Which is close, but as you say it didn't work.

If you'd used parentheses

  /($var1)_\d\d\d\d/

instead of braces you would have got the right result, but for the wrong
reasons.

Braces have several different special meanings throughout Perl, and
which meaning is in effect isn't always obvious. Within regular
expressions, a pair of braces can surround a quantifier ( /.{4}/ or
/a{1,5}/ ) or can be used to delimit the name of an interpolated
variable, which is what I guess you are trying to do here.

But the variable name doesn't include the $, % or @ at the beginning
(called the sigil) so what you need is

  /${var1}_\d\d\d\d/

  
how do you normally match through regex when you have to compare more 
than it's own variable?



You can build regexes and then print them to see if they do what you
want. For instance

  my $regex = qr/${var1}_\d\d\d\d/;
  print $regex, "\n";
  for (@array) {
next unless $_ =~ $regex;
print "$_\n";
  }

and errors like this will show up immediately.

HTH,

Rob
  

I need to study more on qr.. but ${ } definitely works for me.. thank you!

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




Re: regex variable matching when it's more than variable

2008-04-20 Thread Rob Dixon
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# ;

This is much more concisely written

  next unless /$var1_\d\d\d\d/;

>print "$_\n";
> }
> 
> 
> obviously this does not work.. so I tried

Why do you say 'obviously'. It can't be so obvious or you wouldn't have
tried it in the first place. What problem did you see? The code you
posted prints

  abcdefg_3432

Try it!

But there are several things wrong. First of all, always

  use strict;
  use warnings;

at the start of your program. That would stop the code above compiling
and would tell you what error you made. Perhaps you should try that now too.

If you tried it, it would have said

  Global symbol "$var1_" requires explicit package name

So it's including the trailing underscore as part of the variable's
name. But I suspect you already have a clue about this problem,
otherwise why write this

> next unless $_ =~ m#{$var1}_\d\d\d\d#;
> 
> which also didn't work(? should it have worked?)

Which is close, but as you say it didn't work.

If you'd used parentheses

  /($var1)_\d\d\d\d/

instead of braces you would have got the right result, but for the wrong
reasons.

Braces have several different special meanings throughout Perl, and
which meaning is in effect isn't always obvious. Within regular
expressions, a pair of braces can surround a quantifier ( /.{4}/ or
/a{1,5}/ ) or can be used to delimit the name of an interpolated
variable, which is what I guess you are trying to do here.

But the variable name doesn't include the $, % or @ at the beginning
(called the sigil) so what you need is

  /${var1}_\d\d\d\d/

> how do you normally match through regex when you have to compare more 
> than it's own variable?

You can build regexes and then print them to see if they do what you
want. For instance

  my $regex = qr/${var1}_\d\d\d\d/;
  print $regex, "\n";
  for (@array) {
next unless $_ =~ $regex;
print "$_\n";
  }

and errors like this will show up immediately.

HTH,

Rob

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




Re: regex variable matching when it's more than variable

2008-04-20 Thread Dr.Ruud
Richard Lee schreef:

> 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 )

for my $item ( @array ) {
next unless $item =~ m/^\Q$var1\E_[0-9]{4}$/;
print $item, "\n";
}

or in more steps:

for my $item ( @array ) {
next if substr( $item, 0, length $var1 ) ne $var1;
next if substr( $item, length $var1 ) !~ m/^_[0-9]{4}$/;
print $item, "\n";
}

(untested)

-- 
Affijn, Ruud

"Gewoon is een tijger."

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




Re: regex variable matching when it's more than variable

2008-04-20 Thread Dr.Ruud
Richard Lee schreef:

> 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..

Most often you want \Q$var1\E inside your regex. 
See perldoc -f quotemeta. 

-- 
Affijn, Ruud

"Gewoon is een tijger."

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




Re: regex variable matching when it's more than variable

2008-04-20 Thread Aruna Goke

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

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

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

how do you normally match through regex when you have to compare more 
than it's own variable?





perl -e "@array = ( 'abcdefg_3432', 'defg_333', 'abcdefg_','abcdefg_a'); 
$var1='abcdefg'; for(@array){print $_ unless !/$var1_\d{4}/g;}"


output is

abcdefg_3432

goksie


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




Re: regex variable matching when it's more than variable

2008-04-20 Thread John W. Krahn

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/




regex variable matching when it's more than variable

2008-04-20 Thread Richard Lee

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

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

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

how do you normally match through regex when you have to compare more 
than it's own variable?



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




Re: Variable matching.....

2004-03-09 Thread John W. Krahn
Silverfox wrote:
> 
> Hi all,

Hello,

> I'm trying to figure out how can I check if a variable matches the
> first 5 digits of the line below without removing anything from the line.
> 
> 13384 R 20020920 N Gatekeeper, The

In perl, if a variable looks like a number then perl will treat it as a
number, so:

my $line = '13384 R 20020920 N Gatekeeper, The';

if ( $line == 13384 ) {
print "The line matched\n";
}

However if you just want only the first five digits:

my ($number) = $line =~ /^(\d{5})/;

That will skip any numbers that are less than five digits long.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Variable matching.....

2004-03-09 Thread david
Silverfox wrote:

> Hi all, I'm trying to figure out how can I check if a variable matches the
> first 5 digits of the line below without removing anything from the line.
> 
> 13384 R 20020920 N Gatekeeper, The

if you are only concern about digits in front of the line, you don't even 
need a regex. just compare them with '==':

#!/usr/bin/perl
use strict;
use warnings;
no warnings 'numeric';

my $var  = 3384;
my $line = "3384 R 20020920 N Gatekeeper, The";

if($line == $var){
print "match\n";
}else{
print "not match\n";
}

__END__

prints:

match

it will fail for the number 0

david
-- 
s$s*$+/http://learn.perl.org/> 




Re: Variable matching.....

2004-03-09 Thread WilliamGunther
In a message dated 3/9/2004 3:08:10 PM Eastern Standard Time, 
[EMAIL PROTECTED] writes:
>Careful, I believe we want the above capture anchored to the start of
>the line, though I prefer the direct match approach...

You're right about the anchor, but direct match wouldn't be the preferable 
method here. Say, using your code, my $variable = 1338. The regex is true, even 
though the number will be different.You'd have to do /^$variable\s+/ (even 
then, $variable could be "13384 R", be true, and mess things up). Regex in 
conditionals can be effy and ambiguous, and if they can be avoided, why not avoid 
them. 

-will
(the above message is double rot13 encoded for security reasons)

Most Useful Perl Modules
-strict
-warnings
-Devel::DProf
-Benchmark
-B::Deparse
-Data::Dumper
-Clone (a Godsend)
-Perl::Tidy
-Beautifier

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Variable matching.....

2004-03-09 Thread Wiggins d Anconia
> In a message dated 3/9/2004 2:43:16 PM Eastern Standard Time, 
> [EMAIL PROTECTED] writes:
> >Hi all, I'm trying to figure out how can I check if a variable
matches the 
> >first 5 digits of the line below without removing anything from the
line. 
> >
> >13384 R 20020920 N Gatekeeper, The
> >
> >Silver Fox
> 
> use strict;
> use warnings;
> my $line = "13384 R 20020920 N Gatekeeper, The";
> my $var = 13384;
> my ($match) = ($line =~ /(\d{5})/);

Careful, I believe we want the above capture anchored to the start of
the line, though I prefer the direct match approach...

http://danconia.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Variable matching.....

2004-03-09 Thread Wiggins d Anconia
> Hi all, I'm trying to figure out how can I check if a variable matches
the 
> first 5 digits of the line below without removing anything from the line. 
> 
> 13384 R 20020920 N Gatekeeper, The
> 
> Silver Fox
> 

I'm a bit confused, are you trying to test the line against a variable
or a variable against the line?  Oh that helped didn't it ;-)

my $variable = 13384;
my $line = '13384 R 20020920 N Gatekeeper, The';

if ($line =~ /^$variable/) {
   # line starts with variable
}

- or -

if ($variable eq substr($line, 0, 5)) {
  # variable equals first 5 characters of line
}

http://danconia.org



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Variable matching.....

2004-03-09 Thread WilliamGunther
In a message dated 3/9/2004 2:43:16 PM Eastern Standard Time, 
[EMAIL PROTECTED] writes:
>Hi all, I'm trying to figure out how can I check if a variable matches the 
>first 5 digits of the line below without removing anything from the line. 
>
>13384 R 20020920 N Gatekeeper, The
>
>Silver Fox

use strict;
use warnings;
my $line = "13384 R 20020920 N Gatekeeper, The";
my $var = 13384;
my ($match) = ($line =~ /(\d{5})/);
print "Match! ", $var,"==", $match,"\n" if $var == $match;
#OR
my $substr = substr $line, 0, 5;
print "Match! ",$var,"==",$substr,"\n" if $var == $substr;

#Is the line the same?
print $line,"\n";



-will
(the above message is double rot13 encoded for security reasons)

Most Useful Perl Modules
-strict
-warnings
-Devel::DProf
-Benchmark
-B::Deparse
-Data::Dumper
-Clone (a Godsend)
-Perl::Tidy
-Beautifier

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Variable matching.....

2004-03-09 Thread SilverFox
Hi all, I'm trying to figure out how can I check if a variable matches the 
first 5 digits of the line below without removing anything from the line. 

13384 R 20020920 N Gatekeeper, The

Silver Fox

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]