Author Topic: Unexplained if() Behaviour in MultiScript  (Read 19438 times)

AlanJB

  • VIP Member
  • *****
  • Posts: 436
  • VERY old Programmer
    • View Profile
Unexplained if() Behaviour in MultiScript
« on: June 28, 2016, 18:07:59 »
Can somebody please explain the behaviour as shown in the debugger output below (Jungle?  Mathias?  Anyone with a brain younger than mine...?)

The variables' values clearly show that execution should never enter the if().  And yet...


TIA

Alan

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4492
    • View Profile
    • Multi Commander
Re: Unexplained if() Behaviour in MultiScript
« Reply #1 on: June 28, 2016, 22:46:40 »
Well == works like string compare in c/c++ and other in that it return <0 if string1 is less than string2, 0 if they are same and > if string1 is greater then string2.
So if the result is 0 , there is no changes between the two.. like  (($str1 == $str2) == 0)
Been thinking about changing that.. But it would be a breaking change that would break working script..

But you can also use StrIsEqual( str1, str2 ) or StrIsEqualNoCase(str1,str2)  they return a 0/1 value

http://multicommander.com/docs/multiscript/functions/string
« Last Edit: June 28, 2016, 22:49:24 by Mathias (Author) »

AlanJB

  • VIP Member
  • *****
  • Posts: 436
  • VERY old Programmer
    • View Profile
Re: Unexplained if() Behaviour in MultiScript
« Reply #2 on: June 28, 2016, 23:54:24 »
Thanks for the reply Mathias.

Yeah - I remember C (never did much C++) and how == works.  Are you saying that the MultiScript equality operator actually compares the addresses of the two values, rather than their contents?

So I need to use StrIsEqual() (the equivalent of the C strcmp() function), yes?

 I understand if you don't want to change the behaviour - regression testing is a bitch and angry users are even worse  :o

I'm very familiar with the MultiScript string functions by now; I spent a couple of hours this morning checking and amending the documentation page for them  ;)

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4492
    • View Profile
    • Multi Commander
Re: Unexplained if() Behaviour in MultiScript
« Reply #3 on: June 29, 2016, 06:43:15 »
No, it does not compare addresses

== is the same a strcmp (c/c++)  it returns 0 is both strings are equal.  less then 0 or more then 0 depending on how much of the string that matches.
just like  http://www.cplusplus.com/reference/cstring/strcmp/
You can turn the == into a boolean response with "(($str1 == $str2) == 0)"   if the result of str1 == str2 is 0, then it is true (1), but using StrIsEqual is more readable

StrIsEqual() return a boolean.  0 (false) 1 (true)

AlanJB

  • VIP Member
  • *****
  • Posts: 436
  • VERY old Programmer
    • View Profile
Re: Unexplained if() Behaviour in MultiScript
« Reply #4 on: June 29, 2016, 10:03:09 »
Thanks for the explanation, Mathias.

I clearly had forgotten how strcmp() works after 40 years.  That's why I need you youngsters to jog my memory now and again :)