Home  
Login
Username:

Password:

Remember me



Lost Password?

Register now!
Sections
Who's Online
73 user(s) are online (63 user(s) are browsing Forums)

Members: 1
Guests: 72

Severin, more...
Support us!
Recent OS4 Files
OS4Depot.net





Unusual AREXX behaviour
Quite a regular
Joined:
2009/4/28 3:57
From Adelaide, Australia
Posts: 810
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.

   Report Go to top

Re: Unusual AREXX behaviour
Amigans Defender
Joined:
2006/11/17 22:40
From England
Posts: 3141
@MickJT

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

ie.
Test=1



   Report Go to top

Re: Unusual AREXX behaviour
Quite a regular
Joined:
2009/4/28 3:57
From Adelaide, Australia
Posts: 810
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.

   Report Go to top

Re: Unusual AREXX behaviour
Quite a regular
Joined:
2009/4/28 3:57
From Adelaide, Australia
Posts: 810
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

   Report Go to top

Re: Unusual AREXX behaviour
Just can't stay away
Joined:
2006/12/9 10:44
From Lyon, France
Posts: 1302
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
   Report Go to top

Re: Unusual AREXX behaviour
Quite a regular
Joined:
2009/4/28 3:57
From Adelaide, Australia
Posts: 810
@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 18:29:16
Edited by MickJT on 2017/4/25 18:30:17
Edited by MickJT on 2017/4/25 20:07:21
   Report Go to top

Re: Unusual AREXX behaviour
Just can't stay away
Joined:
2006/12/9 10:44
From Lyon, France
Posts: 1302
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
   Report Go to top

Re: Unusual AREXX behaviour
Quite a regular
Joined:
2009/4/28 3:57
From Adelaide, Australia
Posts: 810
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.

   Report Go to top

Re: Unusual AREXX behaviour
Not too shy to talk
Joined:
2006/11/26 17:45
Posts: 263

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.


   Report Go to top

Re: Unusual AREXX behaviour
Quite a regular
Joined:
2009/4/28 3:57
From Adelaide, Australia
Posts: 810
I knew someone would think of a better way. Thanks thomas.

   Report Go to top

Re: Unusual AREXX behaviour
Just can't stay away
Joined:
2006/11/24 18:52
From Gloucestershire, UK.
Posts: 1107
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 0:26:09
Edited by Severin on 2017/4/27 0:29:11
Edited by Severin on 2017/4/27 0:32:05
_________________
Amiga user since 1985
AOS4, A-EON, IBrowse & Alinea Betatester

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





[Advanced Search]


Powered by XOOPS 2.0 © 2001-2016 The XOOPS Project