SRL Archive - Help

Type mismatch


rj

2013-02-05 17:33:41

I get this error:
[Error] C:\Simba\Scripts\Responder.simba(26:34): Type mismatch at line 25

when I try to use this


If Text = (['Hi','Hey','Sup']) Then 

I don't get what im doing wrong

Le Jingle

2013-02-05 18:46:31


var
text : string;
begin
text := 'hi';
if (text = 'hi') then
writeln('these strings match');
end.

OT: you can't compare a string to a string array without using a loop.


var
text : string;
i : integer;
texts : TStringArray;
begin
text := 'hi';
texts := ['ha', 'he', 'hi'];
for i := 0 to 2 do
if (text = texts[i]) then
writeln('these strings match'); // could break out too
end.

DannyRS

2013-02-05 21:07:20

I believe the brackets must be around the entire if statement, because brackets normally imply that whatever's inside them must be calculated first, when doing math for example 3x3x3 vs (3x3)x3, and if you do it the way you did it, I believe the interpreter thinks the array is a bool, so you are trying to conpare a string to a bool, it may work without any brackets, but like Jingle did, Put the brackets around the entire statement and its fine ;)

If that makes any sense :p 3am

i luffs yeww

2013-02-05 21:16:09

If you're trying to see check if Text (which I assume is a string) is one of those three strings, you can use StrInArr. Does just what Jingle said, but it's easier to use.

EDIT:

From: DannyRS
I believe the brackets must be around the entire if statement, because brackets normally imply that whatever's inside them must be calculated first, when doing math for example 3x3x3 vs (3x3)x3, and if you do it the way you did it, I believe the interpreter thinks the array is a bool, so you are trying to conpare a string to a bool, it may work without any brackets…


This isn't correct. Parentheses don't do any type conversion or anything, it just affects precedence. He's getting the error because he's trying to compare a string to an array of string.

Just to show you a comparison, this works fine:


begin
if [5] = ([6]) then
Writeln('wat')
else
Writeln('ok');
end.

DannyRS

2013-02-05 22:51:53

From: i luffs yeww



This isn't correct. Parentheses don't do any type conversion or anything, it just affects precedence. He's getting the error because he's trying to compare a string to an array of string.

Just to show you a comparison, this works fine:


begin
if [5] = ([6]) then
Writeln('wat')
else
Writeln('ok');
end.


Ah my mistake, didn't see OP was an array, but i'm sure something along the lines of this gave me errors before


If string = (string) Then

Maybe it was using not


If Not string = string Then

Something with a string requires correct brackets, I just can't remember what lol

i luffs yeww

2013-02-05 23:17:38

From: DannyRS
Ah my mistake, didn't see OP was an array, but i'm sure something along the lines of this gave me errors before


If string = (string) Then

Maybe it was using not


If Not string = string Then

Something with a string requires correct brackets, I just can't remember what lol


It must've been something else. Like I said, parentheses by themselves will never change the type of what's inside.


if 'hi' = ('hi') then
Writeln('true');

All that parentheses are used for is precedence. :)

riwu

2013-02-05 23:53:00

From: DannyRS


If Not string = string Then

Something with a string requires correct brackets, I just can't remember what lol

Yeah that's probably the cause of your error, since the 'not' keyword takes precedence over all other Boolean operators, which in turn take precedence over all arithmetic operators, hence giving an error as Boolean operators cannot be used on strings in Pascal. (Hence the use of parenthesis to override these orders)

rj

2013-02-06 16:28:41

thanks