English Amiga Board


Go Back   English Amiga Board > Coders > Coders. System > Coders. Scripting

 
 
Thread Tools
Old 12 January 2019, 22:39   #1
solarmon
Registered User
 
solarmon's Avatar
 
Join Date: Dec 2018
Location: UK
Posts: 1,715
ARexx - compound substitution test

Hi,

Another hurdle in my ARexx journey....

I've been learning about ARexx's use of compound variables. I was actually looking/hoping for list/array type structures as in other languages.

I'm aware of the way that ARexx does variable substitution and was hoping to make use of this.

However, this bit of test code has me confused as to why it does not work in the way I think/expect it to work.

The test code is below:

Code:
/* Compound subtitution test */
test.blue = 1

keyword = "blue"

say "keyword:" keyword
say "test.keyword:" test.keyword
say "test.blue:" test.blue
say ""

if test.keyword == 1 then
	say keyword "exists"
else
	say keyword "does not exist"
And I would expect the end result to be:

blue exists

since I am expecting:

TEST.blue == 1

There seems to be some kind of substitution since 'keyword' is replace with 'blue' on the output results. But the test for "test.keyword == 1" does not seem to work the way I think/expect it to work. I'm expecting this to be 'true' since the compound variable test.blue is set to 1.

Can anybody tell me what I'm doing wrong, or whether I've mistunderstood how compounds and subsitution works in ARexx/Rexx?

Cheers!
Attached Thumbnails
Click image for larger version

Name:	Capture.PNG
Views:	195
Size:	2.4 KB
ID:	61565  
solarmon is offline  
Old 12 January 2019, 23:29   #2
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,985
Every word behind the dot is considered a variable and substituted by its value.

test.blue = 1

in this instruction blue is replaced by the value of the variable blue. As this variable has not been set before, its value is its name in upper case letters. This means the instruction actually is

test.BLUE = 1

now you set the variable keyword to the value "blue" in lower case letters. So test.keyword is "test.blue", but this is different from test.BLUE. Case matters.
thomas is offline  
Old 13 January 2019, 01:51   #3
solarmon
Registered User
 
solarmon's Avatar
 
Join Date: Dec 2018
Location: UK
Posts: 1,715
Quote:
Originally Posted by thomas View Post
Every word behind the dot is considered a variable and substituted by its value.

test.blue = 1

in this instruction blue is replaced by the value of the variable blue. As this variable has not been set before, its value is its name in upper case letters. This means the instruction actually is

test.BLUE = 1

now you set the variable keyword to the value "blue" in lower case letters. So test.keyword is "test.blue", but this is different from test.BLUE. Case matters.
Thanks for the response. I understand your explanation and, indeed, if I change keyword to 'BLUE' it matches.


However, then this means I cannot use this method to check if a non integer 'index' exists.

I was hoping use this method to create a list of 'words' and check with that word exists in the list.

For example, a list of colours:

('red', 'green', 'blue')

And I want to check whether the text/string 'blue' (or any other colour) is in this list.

I know I can do it by creating a compound 'list' using numbered 'indexes', for example:

Code:
colour.0 = 3
colour.1 = "red"
colour.2 = "green"
colour.3 = "blue"

keyword = "blue"

exists = 0
do i = 1 to colour.0
	if colour.i == keyword then
		exists = 1
end	

if exists then
	say "Colour" keyword "is in list"
else
	say "Colour" keyword "is not in list"
But the problems I see/have with this method is that:

1. You need to increment the index when creating the list.
2. You need to iterate through the compound list.
3. You need to specify and know the size/number of items.

Rexx/ARexx just don't seem to have a user friendly 'associative' array. This basically sums up with I'm looking for and the issues I have with it:

https://stackoverflow.com/questions/...arrays-in-rexx

I get it being case sensitive, but what I don't understand/get is it changing uninitialised variables to UPPERCASE.

I thought the Symbol() function might be a to check if a variable has been set, but some quick tests showed some 'funny' results depending on what case you give it.
solarmon is offline  
Old 13 January 2019, 11:31   #4
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,985
Quote:
Originally Posted by solarmon View Post
However, then this means I cannot use this method to check if a non integer 'index' exists.
Yes, you can. Just be aware that the index is case sensitive.

instead of

test.blue = 1

use

x = "blue"; text.x = 1

You can even do

blue = "blue"; test.blue = 1


Or in your new example you could initialise the index like this:

Code:
test. = 0
do i=1 to colour.0
   colour = colour.i
   test.colour = 1
end
thomas is offline  
Old 13 January 2019, 15:29   #5
solarmon
Registered User
 
solarmon's Avatar
 
Join Date: Dec 2018
Location: UK
Posts: 1,715
@thomas,

Thanks. I understand your example and have tested it and it does work.

However, it is not that 'easy' to use - for the beginner. It may be OK for relatively small static lists, but not so easy for larger, and dynamically generated, lists.

I appreciate your help, and my knowledge of ARexx has improved a bit more!

Cheers!
solarmon is offline  
Old 13 January 2019, 16:38   #6
idrougge
Registered User
 
Join Date: Sep 2007
Location: Stockholm
Posts: 4,332
I think it works much better for dynamically generated lists than for small and static lists, as can be seen above. Dynamically generated lists will generally be "indexed" by variables and not by symbols typed into the script.
idrougge is offline  
Old 13 January 2019, 16:54   #7
solarmon
Registered User
 
solarmon's Avatar
 
Join Date: Dec 2018
Location: UK
Posts: 1,715
Quote:
Originally Posted by idrougge View Post
I think it works much better for dynamically generated lists than for small and static lists, as can be seen above. Dynamically generated lists will generally be "indexed" by variables and not by symbols typed into the script.
Maybe it is just my lack of understanding of the ARexx language, but these are challenges facing me at the moment, as a beginner. I don't want to jump through hoops to just use 'lists' and 'arrays'.
solarmon is offline  
Old 13 January 2019, 17:36   #8
Ami
Registered User
 
Ami's Avatar
 
Join Date: Sep 2014
Location: Poland
Posts: 175
RosettaCode has good amount of search/sort algorithms examples written in REXX - https://rosettacode.org/wiki/Category:REXX

https://rosettacode.org/wiki/Search_a_list#REXX
https://rosettacode.org/wiki/Search_...f_records#REXX
Ami is offline  
Old 13 January 2019, 20:59   #9
idrougge
Registered User
 
Join Date: Sep 2007
Location: Stockholm
Posts: 4,332
Quote:
Originally Posted by solarmon View Post
Maybe it is just my lack of understanding of the ARexx language, but these are challenges facing me at the moment, as a beginner. I don't want to jump through hoops to just use 'lists' and 'arrays'.
Sorry, you don't have "lists" or "arrays". No such thing. Learn to live without them or fake them.

In many cases, QUEUE, PUSH and PULL can be used as a substitute for lists, as long as your list is not too long.

Quote:
Originally Posted by Ami View Post
RosettaCode has good amount of search/sort algorithms examples written in REXX - https://rosettacode.org/wiki/Category:REXX
Unfortunately, most REXX examples on Rosettacode were written by a lunatic with a… "special" coding style.
idrougge is offline  
Old 15 January 2019, 10:43   #10
Daedalus
Registered User
 
Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
Quote:
Originally Posted by solarmon View Post
I get it being case sensitive, but what I don't understand/get is it changing uninitialised variables to UPPERCASE.
That's an ARexx quirk: it's case-sensitive, but also converts everything that isn't in quotes to uppercase internally - strings, variable names, keywords, commands... That's why almost all commands and port names for almost any program are given in capitals, because that removes some of the case-sensitivity and makes quote requirements more flexible. For example:

address workbench

works, as do

ADDRESS WORKBENCH

and

ADDRESS "WORKBENCH"

However,

address "Workbench"

won't find the WORKBENCH port, since putting the string in quotes means it's no longer converted to uppercase internally. Many examples use uppercase for much of the script for these reasons - it's a way to tell which parts were intended to be uppercase and which should be in quotes.

With that in mind, things might be simpler to visualise if you use all upper-case variable names, at least to start with.

Just bear in mind that any variable that isn't initialised is just a string, and because it's not in quotes, it's converted to uppercase.

Symbol() is probably giving you funny results because of the case too - since you're giving it a string (presumably in quotes), the case has to match, and as has been pointed out, the variable name is actually uppercase internally.

I like ARexx, but I also think in some ways it's quite different conceptually from other common languages. Once you start to get a feel for the different ways of doing things, it will make a lot more sense.
Daedalus is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
AREXX Help Zetr0 support.Other 2 26 January 2011 23:09
arexx help jimbobrocks92 Coders. General 4 19 January 2011 12:50
CD32 Frog Feast test available. Test out the final! cdoty News 42 01 April 2008 16:20
Arexx redblade request.Apps 2 30 August 2006 11:51
Arexx Seti Coders. General 2 05 August 2003 18:59

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:21.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.08127 seconds with 14 queries