List Info

Thread: dual-variable pattern matching




dual-variable pattern matching
user name
2006-07-19 22:25:50
hi josh --  
 
In a message dated 7/19/2006 4:22:20 P.M. Eastern Standard Time, Josh.Perlmutterschange.com writes:
 
> Gurus-
>;
> I'm trying to help a co-worker on reg exp/pattern match issue.
>;
> we have a set of items to go through and another set of input that's
> tested against the first.
>;
> we need to check that items in the second set are a one-for-one match or
> subset of the latter.
>
>
> ie:
>
> $var1 =~ /$var2/
>
> where this returns true in a case such as $var1=Jose, Joseph, and Josh and
> $var2 will be James and Joseph
>;
> when $var2 is James they need to return false and when it is Joseph both
> Jose and Joseph should return true while Josh returns false.
>
> So far we haven't found a solution in orielly and trying
>
> if( $var1 =~ m/$var2/) { print "$var2 contains $var1\n"; }
> else { print "failure on $var2 and $var1\n"; }
>
>
> has failed to work.
>
> -Josh
 
i may not understand this correctly, it seems to me that you are saying that items
in the first set (represented by $var1) will always be equal to or less than the length
of items in the second set (represented by $var2).   i'm also assuming that by
``subset'' you mean ``subset anchored at the beginning of the string''.  ;
 
if that is so, it seems that the way to go about comparing the two sets is the opposite
of the example you give, e.g., 
    $var2 =~ /$var1/;  
since we don't really care if part of $var2 slops over on the end. 
 
maybe something like this: 
 
C:\Work\Perl>perl -we "use strict;   my $v1 = shift;   for my $v2 (ARGV) { 
my $re = qr/$v2/x;
print qq($v1 ), ($v1 =~ / ^ $re /x) ? q(contains) : q(does NOT contain), qq( $v2 \n) }"
Joseph Jose Joseph Josh Josephhh
Joseph contains Jose
Joseph contains Joseph
Joseph does NOT contain Josh
Joseph does NOT contain Josephhh
 
note the ^ beginning of string anchor; easy enough to take this out and have the match
be anywhere in the string.  
 
there's also a version that doesn't involve regexes:  
 
C:\Work\Perl>perl -we "use strict;   my $v1 = shift;   for my $v2 (ARGV) {
print qq($v1 ), (index($v1, $v2) >= 0) ? q(contains) : q(does NOT contain), qq( $v2 \n) }"
Joseph Jose Joseph Josh Josephhh
Joseph contains Jose
Joseph contains Joseph
Joseph does NOT contain Josh
Joseph does NOT contain Josephhh
 
hth -- bill walters  
 
dual-variable pattern matching
user name
2006-07-20 13:34:34
responses embedded

activeperl-bounceslistserv.ActiveState.com wrote on
07/19/2006 06:25:50 
PM:

> hi josh -- 
> 
> In a message dated 7/19/2006 4:22:20 P.M. Eastern
Standard Time, 
> Josh.Perlmutterschange.com writes:
> 
> > Gurus-
> > 
> > I'm trying to help a co-worker on reg exp/pattern
match issue.
> > 
> > we have a set of items to go through and another
set of input that's 
> > tested against the first.
> > 
> > we need to check that items in the second set are
a one-for-one match 
or 
> > subset of the latter.
> > 
> > 
> > ie:
> > 
> > $var1 =~ /$var2/
> > 
> > where this returns true in a case such as
$var1=Jose, Joseph, and Josh 
and 
> > $var2 will be James and Joseph
> > 
> > when $var2 is James they need to return false and
when it is Joseph 
both 
> > Jose and Joseph should return true while Josh
returns false.
> > 
> > So far we haven't found a solution in orielly and
trying
> > 
> > if( $var1 =~ m/$var2/) { print "$var2
contains $var1\n"; }
> > else { print "failure on $var2 and
$var1\n"; }
> > 
> > 
> > has failed to work.
> > 
> > -Josh
> 
> i may not understand this correctly, it seems to me
that you are 
> saying that items 
> in the first set (represented by $var1) will always be
equal to or 
> less than the length 
> of items in the second set (represented by $var2).  
i'm also 
> assuming that by 
> ``subset'' you mean ``subset anchored at the
beginning of the string''.  

> 
> if that is so, it seems that the way to go about
comparing the two 
> sets is the opposite 
> of the example you give, e.g., 
>     $var2 =~ /$var1/; 
> since we don't really care if part of $var2 slops over
on the end. 
> 
> maybe something like this: 
> 
> C:\Work\Perl>perl -we "use strict;   my $v1 =
shift;   for my $v2 
(ARGV) { 
> my $re = qr/$v2/x;
> print qq($v1 ), ($v1 =~ / ^ $re /x) ? q(contains) :
q(does NOT 
> contain), qq( $v2 \n) }"
> Joseph Jose Joseph Josh Josephhh
> Joseph contains Jose
> Joseph contains Joseph
> Joseph does NOT contain Josh
> Joseph does NOT contain Josephhh
> 
> note the ^ beginning of string anchor; easy enough to
take this out 
> and have the match 
> be anywhere in the string. 
Yeah, the example fails in the sub string may not start at
the beginning. 

How it's being attained is also a bit more complex. Than
you for your 
answer. The hints you, the other Bill, et al, have given so
far have 
gotten us playing around and have found some methods that
partially work. 
We're going to try to manipulate those to something that
does. At that 
point, If Joe feels okay with it I'll ask he post the
solution we found. 

If we decide we need more help I'll see if we can post part
of the code to 
show how the two variables are attained. Maybe that could
help too when a 
solution is found. maybe someone might even have a far more
elegant and 
simple solution than we find.

> there's also a version that doesn't involve regexes: 
> 
> C:\Work\Perl>perl -we "use strict;   my $v1 =
shift;   for my $v2 
(ARGV) {
> print qq($v1 ), (index($v1, $v2) >= 0) ? q(contains)
: q(does NOT 
> contain), qq( $v2 \n) }"
> Joseph Jose Joseph Josh Josephhh
> Joseph contains Jose
> Joseph contains Joseph
> Joseph does NOT contain Josh
> Joseph does NOT contain Josephhh
> 
> hth -- bill walters 
>  _______________________________________________
> ActivePerl mailing list
> ActivePerllistserv.ActiveState.com
> To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
ActivePerllistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )