Post Reply 
Setting up a HVar inside a program, IF it doesn't already exist.
04-09-2022, 06:51 PM (This post was last modified: 04-09-2022 06:52 PM by matalog.)
Post: #1
Setting up a HVar inside a program, IF it doesn't already exist.
I am using the following code to set up a list Variable inside a program, that the program will use if it already exists, and will create a list, if it doesn't already exist.

Code:

prep2number:=size(HVars);
prep2:=HVars;
   IF prep2number>0 THEN
    FOR Z FROM 1 TO prep2number DO
     IF prep2(Z)="CL" THEN prep2check:=1;END;
    END;
   END;

   IF prep2check=0 THEN
    HVars("CL"):={"1","2"};
     FOR Z FROM 1 TO 30 DO
      CL(Z):=string(Z);
     END;
   END;

This leaves a syntax error on the line CL(Z):=string(Z);.

If I use HVars("CL"):={"1","2"} on the Home screen, it sets up the variable fine.

Is there a way to do what I want to do, as it seems this is not the way?
Find all posts by this user
Quote this message in a reply
04-10-2022, 06:47 AM (This post was last modified: 04-10-2022 06:49 AM by Tyann.)
Post: #2
RE: Setting up a HVar inside a program, IF it doesn't already exist.
Bonjour

Vous ne pouvez pas utiliser une variable HVars par son nom tant que celle-ci
n' a pas été créer.
Donc si vous créez et utilisez cette variable dans le même programme, vous devez
toujours utiliser la forme 'HVars("nom")'
Pour une liste vous ne pouvez pas utiliser la syntaxe 'HVars("nom")(i)', il faut passer alors
par une variable locale qui copie le contenu de HVars("nom") puis vous modifier la variable locale
et enfin HVars("nom"):=variable locale.
Espérant avoir aidé.

REM Pour savoir si vôtre variable existe vous pouvez utilisez
Code:
POS(HVars,"nom")

Hello

You can't use an HVars variable by its name until it has been
has not been created.
So if you create and use this variable in the same program, you must
always use the form 'HVars("name")'.
For a list you cannot use the syntax 'HVars("name")(i)', you have to use
through a local variable which copies the content of HVars("name") then you modify the local variable
and finally HVars("name"):=local variable.
Hoping to have helped.

REM To find out if your variable exists you can use
Code:
POS(HVars, "name").

Translated with http://www.DeepL.com/Translator (free version)

Sorry for my english
Find all posts by this user
Quote this message in a reply
04-14-2022, 09:01 PM
Post: #3
RE: Setting up a HVar inside a program, IF it doesn't already exist.
Thanks, that does work, but it doesn't allow me to update a non local List Variable, as it may not exist when the program is first Run.

If I wanted to use this method, then I would start by loading the local list variable with the non local one, but that isn't possible as it is potentially not created yet.

Is there a definite way to create a list variable within a program, that doesn't require the program being exited to have the List variable actually created and updatable.

A solution to this would be to run a small prep program before use of the main program, but that isn't user friendly for a new user of the program.
Find all posts by this user
Quote this message in a reply
04-14-2022, 09:51 PM
Post: #4
RE: Setting up a HVar inside a program, IF it doesn't already exist.
Does replacing this line:

CL(Z):=string(Z);

with:

CAS(EXPR("CL(" + Z + "):=string(" + Z + ")"));

do what you want?

Code:
EXPORT eraseme()
BEGIN
LOCAL prep2check, prep2, prep2number;
prep2number:=size(HVars);
prep2:=HVars;
   IF prep2number>0 THEN
    FOR Z FROM 1 TO prep2number DO
     IF prep2(Z)="CL" THEN prep2check:=1;END;
    END;
   END;

   IF prep2check=0 THEN
    HVars("CL"):={"1","2"};
     FOR Z FROM 1 TO 30 DO
      CAS(EXPR("CL("+Z+"):=string("+Z+")"));
     END;
   END;
END;
Find all posts by this user
Quote this message in a reply
04-15-2022, 04:59 AM
Post: #5
RE: Setting up a HVar inside a program, IF it doesn't already exist.
(04-14-2022 09:01 PM)matalog Wrote:  Thanks, that does work, but it doesn't allow me to update a non local List Variable, as it may not exist when the program is first Run.

If I wanted to use this method, then I would start by loading the local list variable with the non local one, but that isn't possible as it is potentially not created yet.

Is there a definite way to create a list variable within a program, that doesn't require the program being exited to have the List variable actually created and updatable.

A solution to this would be to run a small prep program before use of the main program, but that isn't user friendly for a new user of the program.

Bonjour,

Je ne comprends pas trop vôtre problème ?
Etape 1: tester si la HVars existe si oui Etape 2 sinon la créer (ici une liste vide)
Etape 2: Copier cette HVars dans une variable locale
Etape 3: Modifier la variable locale
Etape 4: Copier la variable locale dans la HVars.
C'est la méthode que j'utilise tout le temps et qui fonctionne pour moi.

Hello,

I don't really understand your problem ?
Step 1: test if the HVars exists if yes Step 2 if not create it (here an empty list)
Step 2: Copy this HVars in a local variable
Step 3: Modify the local variable
Step 4: Copy the local variable into the HVars.
This is the method I use all the time and it works for me.

Sorry for my english
Find all posts by this user
Quote this message in a reply
04-15-2022, 11:36 AM
Post: #6
RE: Setting up a HVar inside a program, IF it doesn't already exist.
Yes, I follow now, thanks. The only thing is that this will require 2 copies of the List, at least when the program is running, and I expect this list to get very big.
Find all posts by this user
Quote this message in a reply
04-16-2022, 11:25 AM
Post: #7
RE: Setting up a HVar inside a program, IF it doesn't already exist.
(04-15-2022 11:36 AM)matalog Wrote:  Yes, I follow now, thanks. The only thing is that this will require 2 copies of the List, at least when the program is running, and I expect this list to get very big.

Bonjour matalog

Dans les dernières versions du firware il y a 2 instuctions GET et PUT
qui permettent lire un élément ou de modifier un élément d'une variable
HVars de type liste.

GET(HVars("name"),pos) --> renvoie l'élément de position 'pos'
HVars("name"):=PUT(HVars("name"),value,pos) --> modifie l'élément de position 'pos' avec
la valeur 'value'
Vous pouvez également utiliser PUT(HVars("name"),value,pos)->HVars("name") (-> = sto>)
Ca fonctionne sur ma G2 en version 2.1.14588 (2021 05 05)

Hello matalog

In the last versions of firware there are 2 instuctions GET and PUT
which allow to read an element or to modify an element of a variable
HVars of type list.

GET(HVars("name"),pos) --> returns the element of position 'pos'.
HVars("name"):=PUT(HVars("name"),value,pos) --> modifies the element of position 'pos' with the with the value 'value'.
You can also use PUT(HVars("name"),value,pos)->HVars("name") (-> = sto>)
It works on my G2 in version 2.1.14588 (2021 05 05)

Translated with http://www.DeepL.com/Translator (free version)

Sorry for my english
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)