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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed with matching 4

Status
Not open for further replies.

kevin197

Programmer
Mar 21, 2002
88
GB
I'm trying to do something if I match a string to part of a string

$cdsowned = "ABD001,ABD002,ABD003,ABD004,ABD005,ABD006,ABD007,ABL1A";

and

$row[0] = "ABD003";

I've got this code but it doesn't work for some reason

if ($cdsowned =~ m/"$row[0]"/i) {
DO THIS
}

Also, how would I make it match say ABD003 but not ABD0033?

would it just be this?

if ($cdsowned =~ m/",$row[0],"/i) {
DO THIS
}


Can anyone help?
 
Hi,
Regarding your first query, the regex should be
Code:
my $pattern = $row[0] ;
if ($cdsowned =~ /$pattern/i) {
DO THIS
}

If you want a exact match (ABD003 but not ABD0033), append a ',' at the end of the string and your regex should be
Code:
$cdsowned = "ABD001,ABD002,ABD003,ABD004,ABD005,ABD006,ABD007,ABL1A";
$cdsowned .= "," ;

[code]
my $pattern = $row[0]."," ;
if ($cdsowned =~ /$pattern/i) {
DO THIS
}
chop($cdsowned) ; // remove the "," appended




--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Hi,
Saw prex1's post after I submitted mine.
In case you are confused between the two posts, prex1's solution is more proper and elegant.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Yep, prex's code will definitely do what you want. There is one last thing that I noticed in your original code.

Also, you don't want to have quotes inside your regular expressions unless you are actually looking for quote characters.

Perl uses a bunch of different string delimiters unlike most languages. What's really happening is that Perl interprets the slashes as string delimiters
[tt]m/$row[0]/i[/tt]

What's more interesting, any sort of single-character delimiter will work and a regular expression delimeter (especially useful if you're trying to match something with slashes in it)
[tt]m"$row[0]"i[/tt]
[tt]m($row[0])i[/tt]
[tt]m#$row[0]#i[/tt]

So putting quotes inside is like double-delimiting a string, you get the inside quotes as literal quotation marks:
[tt]'"Hello World"'[/tt]
 
Thanks for all that everyone.

I've got

if ($cdsowned=~m/\b$row[0]\b/i) {

now but it's still not matching :(
 
or better yet, use the following:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$cdsowned[/blue] = [red]"[/red][purple]ABD001,ABD002,ABD003,ABD004,ABD005,ABD006,ABD007,ABL1A[/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]@row[/blue] = [red]"[/red][purple]ABD003[/purple][red]"[/red][red];[/red]

[olive][b]if[/b][/olive] [red]([/red][blue]$cdsowned[/blue] =~ [red]m{[/red][purple][purple][b]\b[/b][/purple][purple][b]\Q[/b][/purple][blue]$row[/blue][0][purple][b]\E[/b][/purple][purple][b]\b[/b][/purple][/purple][red]}[/red][red]i[/red][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]ta da[/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

- Miller
 
Ok, I've got

if ($cdsowned =~ m{\b\Q$row[0]\E\b}i) {

no but still doesn't work :(

I'm doing this inside a sql statement, would that make a difference?

$dbh = DBI->connect('DBI:mysql:DATABASE', 'USER', 'PASSWORD'),{RaiseError => 1};
$sql = <<EOF;
select DISTINCT code from songs ORDER BY code ASC
EOF
my $sth = $dbh->prepare("$sql");
$sth->execute or print("Cannot execute statement!<br>".$sth->errstr);
while(@row = $sth->fetchrow) {

print "ROW is $row[0] and CDSOWNED is $cdsowned <br>";


if ($cdsowned =~ m{\b\Q$row[0]\E\b}i) {
print "MATCHED <br>";
#print "$row[0] <INPUT TYPE=\"checkbox\" NAME=\"$row[0]\" VALUE=\"$row[0]\"checked><br>";
} else {
print "$row[0] <INPUT TYPE=\"checkbox\" NAME=\"$row[0]\" VALUE=\"$row[0]\"><br>";
}

}
$sth->finish;
$dbh->disconnect;
 
Seems like it's time for hunches.

Are you sure that in your database all of the song codes are 'ABD001' with no whitespace and not 'ABD001\n' or something like that? To double-check, does it match if you force [tt]$row[0] = 'ABD003';[/tt] after you enter the [tt]while[/tt] loop now that you have fixed the regular expression?

Also, is everything falling through to the [tt]else[/tt] and printing a checkbox and are you getting all the CD codes you expect?
 
Thanks, that was it. I had a load of white spaces after them. After I cleaned them up it works great!

Thanks to everyone who helped me, I've learnt a lot of new code and fixed my problem :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top