specify variable name with string variable

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

specify variable name with string variable

Zaaphod
This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?

For example:

Var
   MyVariable1 : Word;
   MyVariableName : String;

Procedure ShowVariable(Variablename);
Begin
Writeln(Variablename,' = $', inttohex(    (Somehow get the value of the variable specified by the name here ) ,4));
End;

Begin
MyVariableName:= 'MyVariable1';
ShowVariable(MyVariableName);
End.

Is such a thing possible?

James
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

zh loza
Hi all! It's my first answer to the mailing list, hopefully I won't
screw anything up.

On Sun, Jul 7, 2019 at 10:58 PM James Richters
<[hidden email]> wrote:
>
> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?

You might want to try using generic dictionaries. E.g. like this:

program Project1;
uses
  SysUtils, Generics.Collections;

type
  TVariableDictionary = specialize TDictionary<String, PWord>;

var
  MyDictionary: TVariableDictionary;
  MyVariable1: Word;

begin
  MyDictionary := TVariableDictionary.Create;
  MyDictionary.Add('MyVariable1', @MyVariable1);
  MyDictionary['MyVariable1']^ := 100;
  WriteLn(MyVariable1);
  WriteLn(MyDictionary['MyVariable1']^);
  FreeAndNil(MyDictionary);
end.

If you need to address variables of different type, try Variants (
https://wiki.freepascal.org/Variant ).
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

dmitry boyarintsev
In reply to this post by Zaaphod

associative array?


On Sunday, July 7, 2019, James Richters <[hidden email]> wrote:

This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?

For example:

Var
   MyVariable1 : Word;
   MyVariableName : String;

Procedure ShowVariable(Variablename);
Begin
Writeln(Variablename,' = $', inttohex(    (Somehow get the value of the variable specified by the name here ) ,4));
End;

Begin
MyVariableName:= 'MyVariable1';
ShowVariable(MyVariableName);
End.

Is such a thing possible?

James
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Ryan Joseph-2
In reply to this post by Zaaphod


> On Jul 7, 2019, at 3:58 PM, James Richters <[hidden email]> wrote:
>
> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?

Does you mean like RTTI so you can get a pointer to a local variable by name? I was actually curious about this recently also, does FPC have local variable RTTI? That would be useful to have actually. I know it exists for classes but not sure about locals.

Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Michael Van Canneyt
In reply to this post by Zaaphod


On Sun, 7 Jul 2019, James Richters wrote:

> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?
>
> For example:
>
> Var
>   MyVariable1 : Word;
>   MyVariableName : String;
>
> Procedure ShowVariable(Variablename);
> Begin
> Writeln(Variablename,' = $', inttohex(    (Somehow get the value of the variable specified by the name here ) ,4));
> End;
>
> Begin
> MyVariableName:= 'MyVariable1';
> ShowVariable(MyVariableName);
> End.
>
> Is such a thing possible?

No.
If you want that you need to declare them as published properties of a class.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Zaaphod
Is there an example of how declare them this way?  I only have about 50 variables I wish to do this with.

-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of Michael Van Canneyt
Sent: Sunday, July 7, 2019 6:20 PM
To: FPC-Pascal users discussions <[hidden email]>
Subject: Re: [fpc-pascal] specify variable name with string variable



On Sun, 7 Jul 2019, James Richters wrote:

> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?
>
> For example:
>
> Var
>   MyVariable1 : Word;
>   MyVariableName : String;
>
> Procedure ShowVariable(Variablename);
> Begin
> Writeln(Variablename,' = $', inttohex(    (Somehow get the value of the variable specified by the name here ) ,4));
> End;
>
> Begin
> MyVariableName:= 'MyVariable1';
> ShowVariable(MyVariableName);
> End.
>
> Is such a thing possible?

No.
If you want that you need to declare them as published properties of a class.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Zaaphod
In reply to this post by zh loza
Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try!

James
-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of zh loza
Sent: Sunday, July 7, 2019 4:34 PM
To: FPC-Pascal users discussions <[hidden email]>
Subject: Re: [fpc-pascal] specify variable name with string variable

Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up.

On Sun, Jul 7, 2019 at 10:58 PM James Richters <[hidden email]> wrote:
>
> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?

You might want to try using generic dictionaries. E.g. like this:

program Project1;
uses
  SysUtils, Generics.Collections;

type
  TVariableDictionary = specialize TDictionary<String, PWord>;

var
  MyDictionary: TVariableDictionary;
  MyVariable1: Word;

begin
  MyDictionary := TVariableDictionary.Create;
  MyDictionary.Add('MyVariable1', @MyVariable1);
  MyDictionary['MyVariable1']^ := 100;
  WriteLn(MyVariable1);
  WriteLn(MyDictionary['MyVariable1']^);
  FreeAndNil(MyDictionary);
end.

If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ).
_______________________________________________
fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Zaaphod
Generics.Collections seems to be something that was added after 3.0.4,  I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?

James
-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of James Richters
Sent: Monday, July 8, 2019 6:48 AM
To: 'FPC-Pascal users discussions' <[hidden email]>
Subject: Re: [fpc-pascal] specify variable name with string variable

Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try!

James
-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of zh loza
Sent: Sunday, July 7, 2019 4:34 PM
To: FPC-Pascal users discussions <[hidden email]>
Subject: Re: [fpc-pascal] specify variable name with string variable

Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up.

On Sun, Jul 7, 2019 at 10:58 PM James Richters <[hidden email]> wrote:
>
> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?

You might want to try using generic dictionaries. E.g. like this:

program Project1;
uses
  SysUtils, Generics.Collections;

type
  TVariableDictionary = specialize TDictionary<String, PWord>;

var
  MyDictionary: TVariableDictionary;
  MyVariable1: Word;

begin
  MyDictionary := TVariableDictionary.Create;
  MyDictionary.Add('MyVariable1', @MyVariable1);
  MyDictionary['MyVariable1']^ := 100;
  WriteLn(MyVariable1);
  WriteLn(MyDictionary['MyVariable1']^);
  FreeAndNil(MyDictionary);
end.

If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ).
_______________________________________________
fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Zaaphod
I'm on windows....  

Generics.Collections seems to be something that was added after 3.0.4,  I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?

James
-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of James Richters
Sent: Monday, July 8, 2019 6:48 AM
To: 'FPC-Pascal users discussions' <[hidden email]>
Subject: Re: [fpc-pascal] specify variable name with string variable

Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try!

James
-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of zh loza
Sent: Sunday, July 7, 2019 4:34 PM
To: FPC-Pascal users discussions <[hidden email]>
Subject: Re: [fpc-pascal] specify variable name with string variable

Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up.

On Sun, Jul 7, 2019 at 10:58 PM James Richters <[hidden email]> wrote:
>
> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?

You might want to try using generic dictionaries. E.g. like this:

program Project1;
uses
  SysUtils, Generics.Collections;

type
  TVariableDictionary = specialize TDictionary<String, PWord>;

var
  MyDictionary: TVariableDictionary;
  MyVariable1: Word;

begin
  MyDictionary := TVariableDictionary.Create;
  MyDictionary.Add('MyVariable1', @MyVariable1);
  MyDictionary['MyVariable1']^ := 100;
  WriteLn(MyVariable1);
  WriteLn(MyDictionary['MyVariable1']^);
  FreeAndNil(MyDictionary);
end.

If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ).
_______________________________________________
fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Martin Frb
On 08/07/2019 14:09, James Richters wrote:
> I'm on windows....
>
> Generics.Collections seems to be something that was added after 3.0.4,  I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?
>

If it is in 3.2-beta, you can get a 3.2-beta from here:
https://sourceforge.net/projects/lazarus-snapshots/files/
You can install as 2ndary install.

Otherwise fpcupdeluxe
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Martin Frb
In reply to this post by Zaaphod
On 08/07/2019 14:09, James Richters wrote:
> I'm on windows....
>
> Generics.Collections seems to be something that was added after 3.0.4,  I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?
>

If it is in 3.2-beta, you can get a 3.2-beta from here:
https://sourceforge.net/projects/lazarus-snapshots/files/
You can install as 2ndary install.

Otherwise fpcupdeluxe
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Stefan V. Pantazi
In reply to this post by Zaaphod
You can use the TFPGMap class in fgl unit to implement the dictionary.
fgl should be available in 3.0.4. Try to replace the
Generics.Collections with fgl and the type definition in the example
code from zh loza. The rest should be identical.

[...]

uses fgl;

type

TVariableDictionary = specialize TFPGMap<String, PWord>;

[...]


On 7/8/19 8:09 AM, James Richters wrote:

> I'm on windows....
>
> Generics.Collections seems to be something that was added after 3.0.4,  I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?
>
> James
> -----Original Message-----
> From: fpc-pascal <[hidden email]> On Behalf Of James Richters
> Sent: Monday, July 8, 2019 6:48 AM
> To: 'FPC-Pascal users discussions' <[hidden email]>
> Subject: Re: [fpc-pascal] specify variable name with string variable
>
> Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try!
>
> James
> -----Original Message-----
> From: fpc-pascal <[hidden email]> On Behalf Of zh loza
> Sent: Sunday, July 7, 2019 4:34 PM
> To: FPC-Pascal users discussions <[hidden email]>
> Subject: Re: [fpc-pascal] specify variable name with string variable
>
> Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up.
>
> On Sun, Jul 7, 2019 at 10:58 PM James Richters <[hidden email]> wrote:
>>
>> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?
>
> You might want to try using generic dictionaries. E.g. like this:
>
> program Project1;
> uses
>    SysUtils, Generics.Collections;
>
> type
>    TVariableDictionary = specialize TDictionary<String, PWord>;
>
> var
>    MyDictionary: TVariableDictionary;
>    MyVariable1: Word;
>
> begin
>    MyDictionary := TVariableDictionary.Create;
>    MyDictionary.Add('MyVariable1', @MyVariable1);
>    MyDictionary['MyVariable1']^ := 100;
>    WriteLn(MyVariable1);
>    WriteLn(MyDictionary['MyVariable1']^);
>    FreeAndNil(MyDictionary);
> end.
>
> If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ).
> _______________________________________________
> fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

--
_______________________________________________________
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Zaaphod
Thank you, using fgl;  did work.

Am I correct in assuming that if I want other types of Variables I would change PWord in
specialize TFPGMap<String, PWord>;
to something else, maybe PString, PDouble, PLongint.... etc?


James

-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of Stefan V. Pantazi
Sent: Monday, July 8, 2019 10:54 AM
To: [hidden email]
Subject: Re: [fpc-pascal] specify variable name with string variable

You can use the TFPGMap class in fgl unit to implement the dictionary.
fgl should be available in 3.0.4. Try to replace the Generics.Collections with fgl and the type definition in the example code from zh loza. The rest should be identical.

[...]

uses fgl;

type

TVariableDictionary = specialize TFPGMap<String, PWord>;

[...]


On 7/8/19 8:09 AM, James Richters wrote:

> I'm on windows....
>
> Generics.Collections seems to be something that was added after 3.0.4,  I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?
>
> James
> -----Original Message-----
> From: fpc-pascal <[hidden email]> On Behalf
> Of James Richters
> Sent: Monday, July 8, 2019 6:48 AM
> To: 'FPC-Pascal users discussions' <[hidden email]>
> Subject: Re: [fpc-pascal] specify variable name with string variable
>
> Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try!
>
> James
> -----Original Message-----
> From: fpc-pascal <[hidden email]> On Behalf
> Of zh loza
> Sent: Sunday, July 7, 2019 4:34 PM
> To: FPC-Pascal users discussions <[hidden email]>
> Subject: Re: [fpc-pascal] specify variable name with string variable
>
> Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up.
>
> On Sun, Jul 7, 2019 at 10:58 PM James Richters <[hidden email]> wrote:
>>
>> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?
>
> You might want to try using generic dictionaries. E.g. like this:
>
> program Project1;
> uses
>    SysUtils, Generics.Collections;
>
> type
>    TVariableDictionary = specialize TDictionary<String, PWord>;
>
> var
>    MyDictionary: TVariableDictionary;
>    MyVariable1: Word;
>
> begin
>    MyDictionary := TVariableDictionary.Create;
>    MyDictionary.Add('MyVariable1', @MyVariable1);
>    MyDictionary['MyVariable1']^ := 100;
>    WriteLn(MyVariable1);
>    WriteLn(MyDictionary['MyVariable1']^);
>    FreeAndNil(MyDictionary);
> end.
>
> If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ).
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

--
_______________________________________________________
_______________________________________________
fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Darius Blaszyk-2
In reply to this post by Zaaphod

This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?

Yes it certainly is. I wrote a unit that can handle arbitrary variable types for my project pmake. Pmake it a build tool for pascal.


To set a variable, simply use: set_(“myvar”, 3.141). To read it, use: writeln(val_(“myvar”)).

Rgds, Darius


_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Stefan V. Pantazi
In reply to this post by Zaaphod
sure, but you end up with a separate dictionary for each type. Or, you
can try variants, as originally suggested, but then you need to declare
all your variables as a Variant type.

uses fgl, variants;

type
   TVariableDictionary = specialize TFPGMap<String, PVariant>;



On 7/8/19 11:20 AM, James Richters wrote:

> Thank you, using fgl;  did work.
>
> Am I correct in assuming that if I want other types of Variables I would change PWord in
> specialize TFPGMap<String, PWord>;
> to something else, maybe PString, PDouble, PLongint.... etc?
>
>
> James
>
> -----Original Message-----
> From: fpc-pascal <[hidden email]> On Behalf Of Stefan V. Pantazi
> Sent: Monday, July 8, 2019 10:54 AM
> To: [hidden email]
> Subject: Re: [fpc-pascal] specify variable name with string variable
>
> You can use the TFPGMap class in fgl unit to implement the dictionary.
> fgl should be available in 3.0.4. Try to replace the Generics.Collections with fgl and the type definition in the example code from zh loza. The rest should be identical.
>
> [...]
>
> uses fgl;
>
> type
>
> TVariableDictionary = specialize TFPGMap<String, PWord>;
>
> [...]
>
>
> On 7/8/19 8:09 AM, James Richters wrote:
>> I'm on windows....
>>
>> Generics.Collections seems to be something that was added after 3.0.4,  I've downloaded the current FPC source code, but I’m not sure how to build it.. can someone point me in the right direction on how to compile the current source code?
>>
>> James
>> -----Original Message-----
>> From: fpc-pascal <[hidden email]> On Behalf
>> Of James Richters
>> Sent: Monday, July 8, 2019 6:48 AM
>> To: 'FPC-Pascal users discussions' <[hidden email]>
>> Subject: Re: [fpc-pascal] specify variable name with string variable
>>
>> Thank you for the answer and the example... I didn't know there was such a thing as a variable dictionary, but It looks like that may work for me, I'll give it a try!
>>
>> James
>> -----Original Message-----
>> From: fpc-pascal <[hidden email]> On Behalf
>> Of zh loza
>> Sent: Sunday, July 7, 2019 4:34 PM
>> To: FPC-Pascal users discussions <[hidden email]>
>> Subject: Re: [fpc-pascal] specify variable name with string variable
>>
>> Hi all! It's my first answer to the mailing list, hopefully I won't screw anything up.
>>
>> On Sun, Jul 7, 2019 at 10:58 PM James Richters <[hidden email]> wrote:
>>>
>>> This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?
>>
>> You might want to try using generic dictionaries. E.g. like this:
>>
>> program Project1;
>> uses
>>     SysUtils, Generics.Collections;
>>
>> type
>>     TVariableDictionary = specialize TDictionary<String, PWord>;
>>
>> var
>>     MyDictionary: TVariableDictionary;
>>     MyVariable1: Word;
>>
>> begin
>>     MyDictionary := TVariableDictionary.Create;
>>     MyDictionary.Add('MyVariable1', @MyVariable1);
>>     MyDictionary['MyVariable1']^ := 100;
>>     WriteLn(MyVariable1);
>>     WriteLn(MyDictionary['MyVariable1']^);
>>     FreeAndNil(MyDictionary);
>> end.
>>
>> If you need to address variables of different type, try Variants ( https://wiki.freepascal.org/Variant ).
>> _______________________________________________
>> fpc-pascal maillist  -  [hidden email]
>> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>> _______________________________________________
>> fpc-pascal maillist  -  [hidden email]
>> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>> _______________________________________________
>> fpc-pascal maillist  -  [hidden email]
>> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>> _______________________________________________
>> fpc-pascal maillist  -  [hidden email]
>> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>>
>
> --
> _______________________________________________________
> _______________________________________________
> fpc-pascal maillist  -  [hidden email] https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

--
_______________________________________________________
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Santiago A.
In reply to this post by Zaaphod
El 07/07/2019 a las 21:58, James Richters escribió:
This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?

For example:

Var
   MyVariable1 : Word;
   MyVariableName : String;

Procedure ShowVariable(Variablename);
Begin
Writeln(Variablename,' = $', inttohex(    (Somehow get the value of the variable specified by the name here ) ,4));
End;

Begin 
MyVariableName:= 'MyVariable1';
ShowVariable(MyVariableName);
End.

Is such a thing possible?

What is it for?

What about:
procedure ShowVariable(const Variablename:string; const value:word);
begin
 Writeln(Variablename,' = $'+inttohex(value ,4));
end;

ShowVariable('myVar',myVar);

And as far as I understand, you want to call ShowVariable('myVar',myVar), but not wasting time writing the name enclosed in quotes, let the program get the identifier name in runtime, or the other way around, let the program get the value from a name passed in runtime.

Bad luck, Pascal is not a script language, it is not aware of identifier names in runtime there is no "eval". Thanks to that it gets rids of all the burden of having to keep in runtime structures to know the name, type and value of variable. It generate fast native programs.

Nevertheless, there are ways, you can program that in Freepascal  using classes, properties, RTII etc. It is more flexible and powerful. But obviously it has a price in complexity. Instead or using the standard operators and reference a simple variable, you must use a more convoluted syntax.

As far as I see, you don't intend to let the program create new vars in runtime, you just want to write the identifiers of the vars you have hardcoded.  Is it worth?

(I hope it is not just for debugging)

-- 
Saludos

Santiago A.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Zaaphod

 

Well what it’s for, is not just debugging,  I’m trying to manage outputs to ports that are defined by port numbers and pins in an ini file, the ports and pins can really be anything but I need to keep track of the output states of all the pins by specific port value because I can only output all 8 bits of the port at one time.  I can just read back the port and then use AND or OR with what I want and write it back out, but that’s slow, and it also is not necessarily accurate because some ports will give what’s on the pin itself, not what output it is trying to send out… you can be outputting a 1 and the user can force it to 0 and it will read in a 0 now when you write to it,  it’s outputting a 0 and if the user removes the external condition forcing it to 0 it will not go back to 1.. the ports are 1/O ports and are designed to be operated in this manner.   Anyway I made an array of ports and the data each should contain, and then I also have an array of each I/O pin (defined with a string of the name of the variable that holds the pin number) and the port it is looking for…  and then I have procedures and functions that use these arrays to set the stored value of each port correctly, then I just output the entire 8 bits from the storage array.   It’s working well, but I’m tired of doing things like this:

 

               SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', Ini_Opto_Enable_Positive_Pin,True);  Which calls a procedure like this:

 

Procedure SetStoragePinbyname(SSPBN_Var_Name:String; setpinnum:Byte; Pin_State:Boolean);

 

So in quotes I have the name of the variable that contains the name of the variable in the array, so it can figure out the position in the port array, then the actual contents of the actual variable being used… so I know what pin number I am outputting to, and then the state I want to set…   So I have a TON of  exactly what you describe,  calls to functions and procedures with the name of the variable in quotes AND the variable itself.  Variable names are not assigned at runtime, they are pre-defined.  I did not need to use the name of the variable in my pin-list array, but it makes it less confusing if the variable name is what I search for in the array than some kind of cryptic code that would be difficult to maintain.

 

So yes, the only reason to want to do this is so I could hopefully shorted the call to just

 

               SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', True) ;

And it would somehow get the value of 'Ini_Opto_Enable_Positive_Pin'

 

Hmmmm now that I’m typing this all out, I think I can just put the value of the pin in another array also referenced by the same string and then I could make my calls like that.

 

James.

 

 

From: fpc-pascal <[hidden email]> On Behalf Of Santiago A.
Sent: Monday, July 8, 2019 3:40 PM
To: [hidden email]
Subject: Re: [fpc-pascal] specify variable name with string variable

 

El 07/07/2019 a las 21:58, James Richters escribió:

This might sound silly,  but is it possible to somehow specify a variable with a string containing the name of the variable?
 
For example:
 
Var
   MyVariable1 : Word;
   MyVariableName : String;
 
Procedure ShowVariable(Variablename);
Begin
Writeln(Variablename,' = $', inttohex(    (Somehow get the value of the variable specified by the name here ) ,4));
End;
 
Begin 
MyVariableName:= 'MyVariable1';
ShowVariable(MyVariableName);
End.
 
Is such a thing possible?


What is it for?

What about:

procedure ShowVariable(const Variablename:string; const value:word);
begin
 Writeln(Variablename,' = $'+inttohex(value ,4));
end;
 
ShowVariable('myVar',myVar);


And as far as I understand, you want to call ShowVariable('myVar',myVar), but not wasting time writing the name enclosed in quotes, let the program get the identifier name in runtime, or the other way around, let the program get the value from a name passed in runtime.

Bad luck, Pascal is not a script language, it is not aware of identifier names in runtime there is no "eval". Thanks to that it gets rids of all the burden of having to keep in runtime structures to know the name, type and value of variable. It generate fast native programs.

Nevertheless, there are ways, you can program that in Freepascal  using classes, properties, RTII etc. It is more flexible and powerful. But obviously it has a price in complexity. Instead or using the standard operators and reference a simple variable, you must use a more convoluted syntax.

As far as I see, you don't intend to let the program create new vars in runtime, you just want to write the identifiers of the vars you have hardcoded.  Is it worth?

(I hope it is not just for debugging)


-- 
Saludos
 
Santiago A.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Free Pascal - General mailing list
James Richters <[hidden email]> schrieb am Mo., 8. Juli 2019, 22:44:

SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', True) ;

And it would somehow get the value of 'Ini_Opto_Enable_Positive_Pin


That indeed sounds like something where you could use classes together with published properties. At least if you're willing to spend a bit of effort. 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Zaaphod

I’m curious how use classes together with published properties would work,  is there a sample of how to use this somewhere?

James

 

From: fpc-pascal <[hidden email]> On Behalf Of Sven Barth via fpc-pascal
Sent: Monday, July 8, 2019 5:31 PM
To: FPC-Pascal users discussions <[hidden email]>
Cc: Sven Barth <[hidden email]>
Subject: Re: [fpc-pascal] specify variable name with string variable

 

James Richters <[hidden email]> schrieb am Mo., 8. Juli 2019, 22:44:

SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', True) ;

And it would somehow get the value of 'Ini_Opto_Enable_Positive_Pin

 

That indeed sounds like something where you could use classes together with published properties. At least if you're willing to spend a bit of effort. 

 

Regards, 

Sven 


_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: specify variable name with string variable

Stefan V. Pantazi
Haev a look at rtti and the functionality available in the typinfo unit.
The tests included in fpc are a good resource. Have a look at the
trtti*.pp tests:
https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/tests/test/

Start with trtti4.pp and work your way up to more complex examples.


On 7/8/19 6:04 PM, James Richters wrote:

> I’m curious how use classes together with published properties would work,  is there a sample of how to use this somewhere?
>
> James
>
>  
>
> From: fpc-pascal <[hidden email]> On Behalf Of Sven Barth via fpc-pascal
> Sent: Monday, July 8, 2019 5:31 PM
> To: FPC-Pascal users discussions <[hidden email]>
> Cc: Sven Barth <[hidden email]>
> Subject: Re: [fpc-pascal] specify variable name with string variable
>
>  
>
> James Richters <[hidden email] <mailto:[hidden email]> > schrieb am Mo., 8. Juli 2019, 22:44:
>
> SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', True) ;
>
> And it would somehow get the value of 'Ini_Opto_Enable_Positive_Pin
>
>  
>
> That indeed sounds like something where you could use classes together with published properties. At least if you're willing to spend a bit of effort.
>
>  
>
> Regards,
>
> Sven
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>

--
_______________________________________________________
_______________________________________________
fpc-pascal maillist  -  [hidden email]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12