String comparisons

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

String comparisons

Post by legend986 »

When I want to compare many strings, which is a better way of doing things - Direct Comparison using strcmp or construct a hash table and then do the comparison?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Depends on the purpose. If you don't need to hash them, then why hash them?

If you do hash them, it presents the (small) problem of some strings may evaluate to the same hash even though they are different data.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Post by legend986 »

The purpose is to find values that repeat themselves and set a value of one in a third column if the value is found. For example, I take the first string and compare it with the rest of the 999999 strings and wherever I find a match, I will set the corresponding third column to 1. Next, I will take the second string and then put 2 in the third column wherever i find a match. Can you advice now please?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

There's no need to hash.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Post by legend986 »

So you want me to use the native string function in php to do that? Wouldn't that be slow? I mean, I will have to take the first string and compare it with the rest of the 999999 and then take the second and compare it with 999997... I see it as too many computations... Otherwise, do you suggest anything more efficient here?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

array_count_values()
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Post by legend986 »

Thank you. Actually that would've worked for one array but my data is distributed in rows so I need to fetch one row at a time and make a comparison I guess. To give you an example, it would look like this:

...Column A...Column B...Column C
...12..............14..............1
...32..............44..............2
...16..............15..............
...72..............12..............1
...11..............14..............1
...64..............44..............2

In the above example, Column C is initially 0 and as I traverse through the table and find corresponding similarities, I set a bit in Column C to 1,2,3...
Post Reply