This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
H4X0RZ's profile picture

How can I compare Strings?

Started by H4X0RZ, 22 April 2013 - 03:26 AM
H4X0RZ #1
Posted 22 April 2013 - 05:26 AM
Hi all,
I want to compare two strings ^_^/>
I need It for my OS cuz It have two login systems (one with the http API (mysql) and one with user data on the computer). So, or the second oneI need to compare two SHA1 Hashs :)/>
how can I do that?
Sammich Lord #2
Posted 22 April 2013 - 05:27 AM

if 'stringTest1' == 'stringText1' then
  print('they match')
else
  print('they don't match')
end
theoriginalbit #3
Posted 22 April 2013 - 05:30 AM

print('they don't match')
BUGFIX:
print("they don't match")
H4X0RZ #4
Posted 22 April 2013 - 05:30 AM

if 'stringTest1' == 'stringText1' then
  print('they match')
else
  print('they don't match')
end
Thx :)/>
Sammich Lord #5
Posted 22 April 2013 - 05:33 AM

print('they don't match')
BUGFIX:
print("they don't match")
I just came home from my girlfriend's house when it is 49F outside. :P/> I am could and tired :P/> I should stop coming on Ask A Pro at 8AM…
SuicidalSTDz #6
Posted 22 April 2013 - 06:16 AM
I am could and tired
Nice grammar :P/>

local function check(firstStr, secStr)
 if firstStr ~= secStr then
  return false
 end
 return true
end
Everything is better with functions ^_^/>
Espen #7
Posted 22 April 2013 - 06:17 AM
Since SHA1 is case-insensitive, I'd add to the above:
if string.lower(hash1) == string.lower(hash2) then
  print('they match')
else
  print('they don\'t match')
end

string.lower(), as you might guess, makes every letter lowercase and in the example above I do that with both hashes.
This makes sure that both are lowercase, which essentially means that we eliminated any case-differences.
Sammich Lord #8
Posted 22 April 2013 - 06:19 AM
I am could and tired
Nice grammar :P/>
*Cold. That just shows how much I suck at spelling at 8AM :P/>
theoriginalbit #9
Posted 22 April 2013 - 06:20 AM
local function check(firstStr, secStr)
if firstStr ~= secStr then
  return false
end
return true
end
Everything is better with functions ^_^/>
Why not just

local function check(firstStr, secStr)
  return firstStr == secStr
end
One of my pet peeves is when people use an if statement to then only return a boolean, just return the if statements condition!!!
SuicidalSTDz #10
Posted 22 April 2013 - 06:32 AM
One of my pet peeves is when people use an if statement to then only return a boolean, just return the if statements condition!!!
Hehe, I'll have to remember that :P/> (Not to annoy you of course, I would never do that :)/>)

EDIT: The if statement was for the OP, so he wouldn't be confused