Forum > Beginners

assigning value to variable variable

(1/2) > >>

toby:
Hello

i have a string variable crated with following code

i := 1;
str(i, si);

rfn := 'rand' + si;

and want to assign a string to  rfm -> rand1

in apl all i would do is 'unquote rfn'  which is now variable rand1 and i can assign a string to it

is there a way to do this in freepascal

MarkMLl:
What are you trying to do: choose by name between multiple variables at runtime?

The usual way would be by using a pointer, but I suspect that Pascal is more strongly typed than APL i.e. you'd normally need to define in advance what the type of the pointer's referent is.

Alternatively if you have a small number of variables to be handled this way you could use a case statement, alternatively you could use a sorted stringlist.

MarkMLl

MarkMLl:
So what do the files rand1, rand2 and so on actually contain?

MarkMLl

MarkMLl:

--- Quote from: toby on March 25, 2022, 09:57:08 pm ---
30000 randome chars from 'atgc'

thank you for your responses

i am going to use the
rand : array[1..100] of ansistring
so instead of rand1 it will be rand[1]

if you want/have time i would love to explore the use of pointers you suggested - but i am not a pointer guy - only used them in thread coding

again thank you

--- End quote ---

30,000 in each of the rand1 etc. files, or is there one number in each file?

Basing arrays on [ 0] is slightly more common in Pascal (while strings have characters based on [ 0]) but in practice when you write [ 1..100] or [ 0..99] it's just a subrange.

Strong type checking means that you don't have APL's flexibility about combining an array and a single-element scalar, or repeating an array to fill a bigger one.

But if you define your array as an explicit type, you can then overload standard operators to e.g. add two hundred-element arrays together while at the same time checking that one of them is some different size.

Pointers are not nearly as widely used at the application level as they used to be, since when one instantiates a class the result is referred to directly (i.e. without dereferencing using ^). But if you don't know the referrent at compilation time you have to use something like that.

MarkMLl

MarkMLl:

--- Quote from: toby on March 26, 2022, 07:17:39 pm ---there appears to be an error in your posting program

--- End quote ---

No, the error is you. Go back and use the forum's facilities to tag your code properly, otherwise it will look like garbage and be ignored.

Slightly later: see https://wiki.freepascal.org/Forum

I'm no great fan of animated icons etc., but understanding the code tag is absolutely essential.

MarkMLl

Navigation

[0] Message Index

[#] Next page

Go to full version