SRL Archive - News and General

Changing Variables Externally


Da 0wner

2009-02-04 00:00:26

Is there a way to change a variable that is named with a string?

Ex.


program new;
var
s : string;

procedure ChangeVar(var varname : variant; changeto : variant);
begin
varname := changeto;
end;

begin
ChangeVar('s', 'likethis..but it dosent work.');
end.

Method

2009-02-04 00:05:39

program New;

procedure ChangeString(var toReplace: string);
begin
toReplace := 'changed';
end;

var
str: string;

begin
str := 'lalalala';
WriteLn(str);
ChangeString(str);
WriteLn(str);
end.

The same works for any other variable type (you had the idea right anyways).

Da 0wner

2009-02-04 00:10:28

No I know how to do that already.

I mean change the variable via string…


program New;

var
str : string;

procedure ChangeVar(var varname : string; changeto : string);
begin
varname := changeto;
end;

begin
str := 'hey'
writeln(str);
changevar('str', 'hey1';
writeln(str);
end.

That, in theory, would change varname to changeto.
But technically, it dosen't.

Method

2009-02-04 00:13:53

That's because you're not feeding in the str variable, you're giving it the string 'str' instead. Remove the quotes and it'll work fine.

Da 0wner

2009-02-04 00:17:59

I understand that. I want it to be able to dynamically change them for my DaInclude SetVar and GetVar rc commands. I think I almost found the solution but most likely, it won't work.

Method

2009-02-04 00:26:30

You can change variables through procedures like that just how I had it. You just have to make sure you're feeding a variable for the parameter that takes a var name: type instead of just a piece of data like 'str'.

Da 0wner

2009-02-04 00:33:34

I don't think you understand what I am saying…

Yes that woulds woohoo but I knew how to do that already.

Let's take your procedure.

ToReplace has to be static. You cant set it by string. That's what I want.
So that you can set a variable via string like

procedure setvar(varname : string);
begin
s := 'hey';
end;

then this would be in main loop


setvar(s);

Method

2009-02-04 00:42:31

The code you just pasted works fine. I don't know what you're trying to do if it's not that.

program New;

var
str: string;

procedure setVar(var s: string);
begin
s := 'hey';
end;

begin
setVar(str);
WriteLn(str);
end.

Successfully compiled (31 ms)
hey
Successfully executed

Da 0wner

2009-02-04 00:44:35

Alright, go on msn Method.
And add me [email]kylewollaston@hotmail.com[/email]
It takes to long to talk on this.

ZaSz

2009-02-04 00:52:52

what he means is.

var
tobechanged:string;

setvar(currentvar,newvartype):variant


tobechanged= string
he wants to change tobechanged from a string to a new var like an integer.

so he wants to
 setvar(tobechanged,integer) 

Am i right da owner?

Da 0wner

2009-02-04 01:10:45

Heh, nope. It's kinda hard to explain..

I actually want it to set any var that is specified in the parameters of a function with a string.

n3ss3s

2009-02-04 01:26:06

Why would you want to change the var's name in the first place?

Da 0wner

2009-02-04 01:28:48

I don't want to change the variables name. I want to change it's contents.

n3ss3s

2009-02-04 02:13:17

Like wtf, this is how you change a var (this is a string example)

s := 'hi';



procedure change(var s: String; toWhat: String);

Begin
s := toWhat;
End;

ZaSz

2009-02-04 02:18:27

i figured out what he wants.


var
zasz:string; // this = what to input in VARTOEDIT part.

procedure edit(vartoedit,text:string);
begin
vartoedit := text; // zasz = me!
end;

begin
edit(zasz,'me!');
end.

I know this is what he means now.

n3ss3s

2009-02-04 02:21:46

You can use variants… You can't represent an identifier with a string, e.g "turn the string into the var"…



S: String;
VarToChange: TVariant;

Procedure change(Var Change: TVariant; To: String);

Begin
Change := To;
End;

Begin
S := '2';
VarToChange := S;
Change(VarToChange, '3');
End.

ZaSz

2009-02-04 02:24:04

You get what he means now n3ss3s???

Da 0wner

2009-02-04 02:27:17

Nvm guys, you just don't get it.

n3ss3s and zasz, you guys are wrong..

zasz has got the point of what i want though.

lordsaturn

2009-02-04 02:53:54

Something like this will work fine..

var
s: string;

procedure edit(var variable: string; text: string);
begin
variable := text;
end;

begin
edit(s, '1');
writeln(s);
edit(s,'2');
writeln(s);
end.
Or did I not understand you correctly?

Da 0wner

2009-02-04 03:04:55

Nope it dosen't work the way I want.
This does though.
Just requires a little extra work and takes longer tho :/.


Program New;

type
variable = record
name : string;
contents : variant;
end;

var
variables : array [0..1] of variable;

procedure SetVars;
begin
variables[0].name := 'var1';
variables[0].contents := 'var1contentsbefore';
variables[1].name := 'var2';
variables[1].contents := 'var2contentsbefore';
end;

procedure SetVar(name : string; towhat : variant);
var
i : integer;
begin
for i := 0 to high(variables) do
begin
if variables[i].name = name then
begin
writeln('Before: ' + variables[i].contents);
variables[i].contents := towhat;
writeln('After: ' + variables[i].contents);
exit;
end;
end;
end;

begin
setvars;
setvar('var2', 'var2contentsafter');
writeln('');
setvar('var1', 'var1contentsafter');
end.

n3ss3s

2009-02-04 05:13:34

Now he pmed me I know what he means :p


He wants to represent an identifier with a string. Identifiers are just 'addresses' for the values in RAM, representing an identifier with something else means representation of presentation…

Tell me what use do you have for it and maybe we can try to think of an efficient solution…

Da 0wner

2009-02-04 05:30:05

Read my post before yours.

mastaraymond

2009-02-04 08:44:11


Program New;


var
variables : array[0..1] of string;

procedure SetVars;
begin
variables[0]:= 'var1contentsbefore';
variables[1] := 'var2contentsbefore';
end;

procedure WritelnVars;
begin;
Writeln(Variables[0]);
Writeln(Variables[1]);
end;

procedure SetVar(name : string; towhat : variant);
var
i : integer;
begin
case name of
'var1' : Variables[0] := ToWhat;
'var2' : Variables[1] := ToWhat;
end;
end;

begin
setvars;
writeln('Before : ');
WritelnVars;
setvar('var1', 'var1contentsafter');
setvar('var2', 'var2contentsafter');
Writeln('After: ');
WritelnVars;
end.

Da 0wner

2009-02-04 08:54:44

Yeah, that's basically what I had without the record and neater :p.