Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
94 user(s) are online (84 user(s) are browsing Forums)

Members: 2
Guests: 92

hlt, nbache, more...

Support us!

Headlines

 
  Register To Post  

Unusual AREXX behaviour / AREXX bug
Just can't stay away
Just can't stay away


See User information
Perhaps this is a documented feature, but I have no idea why this is happening. Here's an example script:

/**/
Test="1"
Test2=Translate(Test,"01","10")
SAY Test2
SAY 0+Test2
Exit

What would you expect to be output on the shell?

0
0

Obviously. And that's exactly what happens.

But add one line to the script like so:

/**/
Test="1"
SomeUnusedVar=Test+26
Test2=Translate(Test,"01","10")
SAY Test2
SAY 0+Test2
Exit

What now? It outputs:

0
1

Why's that? If I use DataType() to check Test2, it's a number, and that number is 0. 0 plus 0 should be 0.

Edit: Thread title change to make it easier for me to find in the future.


Edited by MickJT on 2017/10/16 8:01:48
Go to top
Re: Unusual AREXX behaviour
Amigans Defender
Amigans Defender


See User information
@MickJT

Does it do the same if you remove the double quotes from your original Test assignment?

ie.
Test=1



Go to top
Re: Unusual AREXX behaviour
Just can't stay away
Just can't stay away


See User information
Yes, it does.

I have tested this on Regina REXX as well, and the 2nd example prints 0 both times as expected, even with AREXX_BIFS & AREXX_SEMANTICS enabled.

Go to top
Re: Unusual AREXX behaviour
Just can't stay away
Just can't stay away


See User information
This also causes the same issue:

/**/
Test=1
IF Test=1 THEN NOP /* Or any DO ... END statement */
Test2=Translate(Test,"01","10")
SAY Test2
SAY 0+Test2
Exit

Go to top
Re: Unusual AREXX behaviour
Just can't stay away
Just can't stay away


See User information
Probably because Translate() returns a string and the "+" is fooled.
This code:
/**/
Test="1"
Test2=Translate(Test,"01","10")
say datatype(Test)
say datatype(Tes2)

will output:
NUM
CHAR

And if you add Trunc() to your original code, like:
Test2=Trunc(Translate(Test,"01","10"))
you have the expected behaviour.

Philippe 'Elwood' FERRUCCI
Sam460ex 1.10 Ghz
http://elwoodb.free.fr
Go to top
Re: Unusual AREXX behaviour
Just can't stay away
Just can't stay away


See User information
@Elwood

Look at the two examples in the very first post, and notice that the only difference is adding SomeUnusedVar=Test+26 (or an IF statement using the Test variable).

At the bottom of the first post I also noted that I checked with DataType() already to verify that the Test2 variable is NUM.

The reason your example outputs CHAR is because of a typo. The variable "Tes2" does not exist. Test2 is NUM.

If I use Trunc, I get 1 and 1 as the output if I add an IF statement that shouldn't do anything.

/**/
Test=1
IF Test=1 THEN NOP /* Or any DO ... END statement */
Test2=Trunc(Translate(Test,"01","10"))
SAY Test2
SAY 0+Test2
Exit

Output:

1
1

The output from Trunc is 1 when it should be 0 (the Translate function swaps 1 to 0). If I remove IF Test=1 THEN NOP, a line which shouldn't do anything, then I get the expected output of 0 and 0.

Edit: Corrections.


Edited by MickJT on 2017/4/25 19:29:16
Edited by MickJT on 2017/4/25 19:30:17
Edited by MickJT on 2017/4/25 21:07:21
Go to top
Re: Unusual AREXX behaviour
Just can't stay away
Just can't stay away


See User information
Oops, you're right.

I just tested in OS3.9 in E-UAE and I still have the same wrong output with the original code:
0
1

so this behaviour/bug is here since day one.

Philippe 'Elwood' FERRUCCI
Sam460ex 1.10 Ghz
http://elwoodb.free.fr
Go to top
Re: Unusual AREXX behaviour
Just can't stay away
Just can't stay away


See User information
Yeah, it's strange. I wanted to use SubStr() and if a particular user setting was 0, it would need to read one more byte. My way which I thought was clever was to just use Translate() to swap 0 and 1 around. For example: Test=SubStr(Output,Length+Translate(Setting,"01","10"))

That works unless the Setting variable has been checked with IF in the past.

I could rename the setting to do its opposite and then avoid using Translate(), but it wouldn't be as intuitive. Easy enough to work around though.

Thanks for testing it with OS3.9. I hadn't tried that.

Go to top
Re: Unusual AREXX behaviour
Not too shy to talk
Not too shy to talk


See User information

If Setting is always 0 or 1 you could use 1-Setting to swap it.

Length + 1 - Setting makes more sense anyway IMHO because it needs to translate between string and number only once.


Go to top
Re: Unusual AREXX behaviour
Just can't stay away
Just can't stay away


See User information
I knew someone would think of a better way. Thanks thomas.

Go to top
Re: Unusual AREXX behaviour
Just can't stay away
Just can't stay away


See User information
if you want to use the variable to hold the number of iterations as well you can use:

/**/
do 1 to 10
  
if x//2 = 0 then do:
    
say x " is even"
  
else
    
say x " is odd"
  
endif
end


the remainder (//) when divided by two will always return 0 or 1, problem solved


Edited by Severin on 2017/4/27 1:26:09
Edited by Severin on 2017/4/27 1:29:11
Edited by Severin on 2017/4/27 1:32:05
Amiga user since 1985
AOS4, A-EON, IBrowse & Alinea Betatester

Ps. I hate the new amigans website. <shudder>
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 3 ( 0 members and 3 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project