Source language: Translate to:

Compound variables

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

Moderator: Neosoft Support

Compound variables

Postby dpayer » Thu Jul 12, 2012 11:10 am

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
dpayer
 
Posts: 1362
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Postby Gaev » Thu Jul 12, 2012 12:11 pm

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
Gaev
 
Posts: 3667
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby dpayer » Thu Jul 12, 2012 1:04 pm

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
User avatar
dpayer
 
Posts: 1362
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: Compound variables

Postby yanzco » Tue Jan 31, 2017 11:29 pm

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

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

text : [[aaa][bbb]]
just displays blank
yanzco
 
Posts: 174
Joined: Sun Jul 20, 2014 4:07 am

Re: Compound variables

Postby virger » Wed Feb 01, 2017 6:39 am

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
User avatar
virger
 
Posts: 457
Joined: Mon Sep 18, 2006 12:21 pm
Location: Costa Rica, America Central

Re: Compound variables

Postby yanzco » Wed Feb 01, 2017 7:11 am

i get it now.... thanks virger.. :D
yanzco
 
Posts: 174
Joined: Sun Jul 20, 2014 4:07 am


Return to NeoBook Knowledgebase

Who is online

Users browsing this forum: No registered users and 1 guest