Longest string challenge: Difference between revisions

m
→‎{{header|Lua}}: missing </code>
No edit summary
m (→‎{{header|Lua}}: missing </code>)
Line 1,410:
print(output)</syntaxhighlight>
 
This solution is ispired by the Python one, but since in Lua even an empty string has a boolean true value, it had to be slightly altered. Testing whether a string is empty is done by searching for the Lua string ''pattern'' <code>'^$'</code>. If it is found, i.e. the examined string is empty, <code>string.find</code> returns the position of the match, or <code>nil</code> if it didn't match. Since in Lua any number is <code>true</code>, we just test for the boolean value of the result.
 
Note that the <code>longer</code> function returns <code>true</code> even if both strings have the same length, so the return value can either be <code>true</code> or <code>false</code> and we can avoid using a comparison or equality operator in interpreting this return value.
7,813

edits