Matching stuff

Joshua Keroes joshua_keroes at eli.net
Wed Jun 5 17:26:30 CDT 2002


Found out my posts have been going to /dev/null ever since my company mailserver decided that my emails really aren't From: jkeroes but joshua_keroes. As the Church-lady would say, "Isn't that special?"

So here's the this post. If I can find the other, I'll repost it too:

__BEGIN__
use strict;

my $service  = "cns-noc_server1";
my $rule     = "cns-noc";

# What you want:
if ( $service =~ /$rule/ ) {
    # do stuff
}

# another possibility:
if ( index $service, $rule ) {
    # do stuff
}

# silly
if ($service =~ s/$rule/$rule) {
    # do stuff;
}

# sillier
for ( 0 .. length($service) - length($rule) ) {
    if ( substr($service, $_, length($rule)) eq $rule ) {
	# do stuff
    }
}

# yet more silly
my @maybe = map { substr $service, $_, length $rule } 0 .. length $service;
if ( grep { $_ eq $rule } @maybe ) {
    # do stuff
}


# beep beep honk honk
my %maybe = map { substr($service, $_, length $rule) => 0 } 0 .. length $service;
if ( exists $maybe{$rule} ) {
    # do stuff
}

# sill-to-the-y
my %permutations;
for my $start (0..length $service) {
    for my $len (1..length($service) - $start) {
	$permutations{ substr $service, $start, $len }++;
    }
}
if ( exists $permutations{$rule} ) {
    # do stuff
}

# Other assorted silliness shall be left as an exercise to the reader.

__END__

Wuv,
Joshua
TIMTOWTDI



More information about the Pdx-pm-list mailing list