Page 1 of 1
String comparisons
Posted: Fri Oct 05, 2007 9:30 pm
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?
Posted: Fri Oct 05, 2007 11:30 pm
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.
Posted: Fri Oct 05, 2007 11:44 pm
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?
Posted: Fri Oct 05, 2007 11:51 pm
by s.dot
There's no need to hash.
Posted: Sat Oct 06, 2007 12:01 am
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?
Posted: Sat Oct 06, 2007 2:46 am
by stereofrog
array_count_values()
Posted: Sat Oct 06, 2007 10:32 am
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...