Dave's Brain

Browse - programming tips - perl case insensitive compare

Date: 2008feb14
Language: perl

Q.  How can I do a case insensitive string compare?

A.  Here's a pretty nice way to do it:

sub ceq($$)
{
	my($a, $b) = @_;
	$b = quotemeta($b); # For safety
	return $a =~ m/^$b$/i;	# Using the i is key
}

sub ceq2($$)	# Another way
{
	my($a, $b) = @_;

	return lc($a) eq lc($b);	# Convert both to lowercase
}

sub exampleUse()
{
	if (ceq('apple', 'aPPle'))
	{
		print "same\n";
	}
	else
	{
		print "different\n";
	}
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: