Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex

Status
Not open for further replies.

donny750

Programmer
Joined
Jul 13, 2006
Messages
145
Location
FR
hello;

I've want to make a subroutine ou find a path;
it's my script
Code:
#!/usr/bin/perl
use strict;

sub findme
{




    if ($_[1] =~ /$_[0]/) {
        print "I'm find it\n";
        print "We are looking for  ----------->  $_[0]\n";
        print "In  -----------> $_[1]\n";
    }
    else {
        print "Isn 't here\n";
        print "We are looking for  ----------->  $_[0]\n";
        print "In  -----------> $_[1]\n";
    }

}



my $var1 = "/opt/sqlserver/admin/general/bin:/opt/sqlserver/product/ISOO99/bin:/opt/sqlserver/product/bin:/usr/esso/bin:/usr/local/bin:/bin:";
my $var2 = "/opt/sqlserver/product/bin";
&findme($var2,$var1)

my sub find $var2;
it's correct or it can be not return the good result ?
if i've this
"/opt/sqlserver/admin/general/bin:/opt/sqlserver/product/bin/ISOO99/bin:/opt/sqlserver/product/bin:"

he find the first
/opt/sqlserver/product/bin
??
thanks
 
not sure what you are doing really but if it's just to find
$var2 in $var1 your code should work.

- Kevin, perl coder unexceptional!
 
You'd want to be careful about that regexp. If you search for "/opt/sqlserver/product/bin" and the paths you're searching in contains "/usr/lib/opt/sqlserver/product/bin" or "/opt/sqlserver/product/bin/foo", you'll get a match. I'd prefer to do it this way:
Code:
my $var1 = "/opt/sqlserver/admin/general/bin:/opt/sqlserver/product/ISOO99/bin:/opt/sqlserver/product/bin:/usr/esso/bin:/usr/local/bin:/bin:";
my $var2 = "/opt/sqlserver/product/bin";
print findme($var2,$var1) ? "Found it!\n" : "Not here!\n";

sub findme {
   my $to_find = shift;
   my %find_in = map +( $_ => 1 ), split ':', shift;

   return exists $find_in{ $to_find };
}
 
can you explain your code ishnid please
 
Yea, ishnid raises a good point. If you need an exact match you need to use a better regexp or use split(). Ishnids code does the same as this easier to read code:

Code:
my $var1 = "/opt/sqlserver/admin/general/bin:/opt/sqlserver/product/ISOO99/bin:/opt/sqlserver/product/bin:/usr/esso/bin:/usr/local/bin:/bin:";
my $var2 = "/opt/sqlserver/product/";
print findme($var2,$var1) ? "Found it!\n" : "Not here!\n";

sub findme {
   my $to_find = shift;
   return 1 if grep {$_ eq $to_find} split(/:/,shift);
}

- Kevin, perl coder unexceptional!
 
First two lines just initialise variables.
Code:
print findme($var2,$var1) ? "Found it!\n" : "Not here!\n";
That's equivalent to:
Code:
if ( findme( $var2, $var1 ) ) {
   print "Found it\n";
}
else {
   print "Not here!\n";
}

A long-winded way of writing that subroutine, so that I can comment it better:
Code:
sub findme {
   # the first argument is the path that I'm looking for
   my $to_find = shift;

   # the second argument is the list of paths to search in
   my $path_list = shift;

   # split up the path list into an array (there are colons between the paths)
   my @paths = split ':', $path_list;

   # put @paths into a hash, where the key is the path and the value is "1"
   # it's very quick to look something up in a hash to see if it's there
   my %find_in = map +( $_ => 1 ), @paths;

   # if the hash contains the string we're looking for, return true
   # return false otherwise
   return exists $find_in{ $to_find };
}
 
Ishnid,

why do you use map like this:

map +(EXPRESSION), list

instead of:

map {BLOCK} list



- Kevin, perl coder unexceptional!
 
Habit, really. Someone once told me the EXPR version is quicker. I believe it is but only slightly. Doesn't really make a difference.
 
I prefer ishnid's solution for this problem. But given that the subject of this thread is "regex", I think that it's appropriate that the proper regex be included as a listed response as well.

This simply matches the string at either anchors or colon boundaries. It also escapes all meta characters in the searched for path, just in case.

Code:
if ($_[1] =~ /(?:^|:)\Q$_[0]\E(?:$|:)/) {
 
split() is a regexp..... [wink]

- Kevin, perl coder unexceptional!
 
I had a feeling someone might point that out.

Should have known it would be you, Kevin. [smarty]
 
hehehe... sorry, can't help it. Anal retentive tendencies left over from my early childhood. [poke]

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Similar threads

Replies
2
Views
347
  • Locked
  • Question Question
Replies
4
Views
468
  • Locked
  • Question Question
Replies
1
Views
307
  • Locked
  • Question Question
Replies
5
Views
450

Part and Inventory Search

Sponsor

Back
Top