Compound variables

NeoBook tips, tricks, code samples and more...

Moderator: Neosoft Support

Locked
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Compound variables

Post by dpayer »

I have found several times that I want to turn a list of items into a list of variables with the names of those items (common when dealing with file names).

The way I have found to address this is to create a compound variable (my term) where the item you have selected or obtained via process and stored as a variable is then listed as a variable by putting square bracket quotes around the first set of square bracket quotes. Example: [myvar] --> [[myvar]]

Initially I was concerned that the variable name would contaminate old data with new data that is initially obtained using the same variable name. As it turns out, you can prove that if you structure it correctly, each variable retains its integrity. You can prove this for yourself by creating a sample pub with a button and a ListBox (named Listbox1). Add the following as the button's action when clicked:

Code: Select all

FileOpenBox "Select File" "Any File|*.*" "c:\" "[selectedfile]" ""
ExtractFileName "[selectedfile]" "[selectedfile]"
Random "1000" "[Rnumber]"
SetVar "[[selectedfile]]" "[Rnumber]"
ListBoxAddItem "ListBox1" "0" "[[selectedfile]]"
This pub selects a file, extracts the name of the file from the path and sets that filename as a variable name. You then assign a random number as the value of the variable and then store it in the listbox.

You will see in the debug screen that the variable IS CREATED AND NAMED by the action of selecting the file. As you select new files, new variables are created, each with their distinct name and assigned a distinct value. Of course if you select the same file twice you will overwrite the original value.

This is a helpful process to gather and display data about items but you don't know how many or what names you want to assign before the actual program is used.

Comments welcome.
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Post by Gaev »

DavidP:

Yes, I discovered (to my delight) that NeoBook did this kind of "nested variable resolution" many years ago.

To my knowledge, this is the only scripting language that provides this very powerful facility on a PC ... though back in the 80's I worked with Tandem's TACL (Tandem Advanced Command Language) facility that provided a similar feature.

Other combinations that can be equally useful are ...

[xyz[loopCount]]
[[prefix]xyz]
[[prefix]pqr[counter]]

... the "variable resolution" is done from the inside out ... so if ...

[prefix] = David
[counter] = 78

... [[prefix]pqr[counter]] is first resolved to [Davidpqr78] ... and then (depending on usage), the value of [Davidpqr78] is either read or written.

If you are using it in a "read context", all you need to make sure is that after the "inner resolution", the resulting variable exists ... otherwise, the fetched value will be null/blank

If you are using it in a "write contrext", a new variable will be created if none exists; else the value of an existing variable will be replaced.


As for beneficial Applications ... if you have multiple objects that have similar behaviour ... and you want to store such behavioural properties for each object ... instead of writing event code multiple times, you can have a GoSub to a common event handler ... where you can deploy something like ...

[[self]ClickCount]
[[self]ImageDescription]
etc.

... and if you named this collection of objects something like ...

ABCD1
ABCD2
ABCD3

... you can loop through the properties using something like ...

[ABCD[counter]ClickCount]
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Post by dpayer »

Gaev wrote:DavidP:

Yes, I discovered (to my delight) that NeoBook did this kind of "nested variable resolution" many years ago.
This also allows you to create quite complex arrays:

[[Column[X]][Row[Y]][Depth[Z]]]

But then my head starts to hurt. :)

D
yanzco
Posts: 192
Joined: Sun Jul 20, 2014 4:07 am

Re: Compound variables

Post by yanzco »

does this still work?.. tried it but, does not work...

setvar [aaa] = 111
setvar [bbb] = 222

text : [[aaa][bbb]]
just displays blank
User avatar
virger
Posts: 540
Joined: Mon Sep 18, 2006 12:21 pm
Location: Costa Rica, America Central

Re: Compound variables

Post by virger »

You are right, but try this...

Code: Select all

setvar "[aaa]" "111"
setvar "[bbb]" "222"
setvar "[111222]" "Hello"
setvar "[text]"  "[[aaa][bbb]]"
Look:
setvar "[111222]" "Hello"
COSTA RICA
PURA VIDA
yanzco
Posts: 192
Joined: Sun Jul 20, 2014 4:07 am

Re: Compound variables

Post by yanzco »

i get it now.... thanks virger.. :D
Locked