Results 1 to 24 of 24

Thread: Changing Variables Externally

  1. #1
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Changing Variables Externally

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

    Ex.

    SCAR Code:
    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.

  2. #2
    Join Date
    Mar 2007
    Posts
    3,042
    Mentioned
    1 Post(s)
    Quoted
    14 Post(s)

    Default

    SCAR Code:
    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).
    :-)

  3. #3
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    No I know how to do that already.

    I mean change the variable via string...

    SCAR Code:
    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.

  4. #4
    Join Date
    Mar 2007
    Posts
    3,042
    Mentioned
    1 Post(s)
    Quoted
    14 Post(s)

    Default

    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.
    :-)

  5. #5
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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.

  6. #6
    Join Date
    Mar 2007
    Posts
    3,042
    Mentioned
    1 Post(s)
    Quoted
    14 Post(s)

    Default

    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'.
    :-)

  7. #7
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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
    SCAR Code:
    procedure setvar(varname : string);
    begin
    s := 'hey';
    end;

    then this would be in main loop

    SCAR Code:
    setvar(s);

  8. #8
    Join Date
    Mar 2007
    Posts
    3,042
    Mentioned
    1 Post(s)
    Quoted
    14 Post(s)

    Default

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

    SCAR Code:
    program New;

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

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

    Code:
    Successfully compiled (31 ms)
    hey
    Successfully executed
    :-)

  9. #9
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  10. #10
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    what he means is.
    SCAR Code:
    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
    SCAR Code:
    setvar(tobechanged,integer)

    Am i right da owner?
    I do visit every 2-6 months

  11. #11
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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.

  12. #12
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

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

  13. #13
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  14. #14
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

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

    s := 'hi';


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

    Begin
      s := toWhat;
    End;

  15. #15
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    i figured out what he wants.

    SCAR Code:
    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.
    I do visit every 2-6 months

  16. #16
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

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

    SCAR Code:
    S: String;
    VarToChange: TVariant;

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

    Begin
      Change := To;
    End;

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

  17. #17
    Join Date
    Oct 2007
    Location
    http://ushort.us/oqmd65
    Posts
    2,605
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    You get what he means now n3ss3s???
    I do visit every 2-6 months

  18. #18
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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.

  19. #19
    Join Date
    Mar 2007
    Posts
    1,700
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    Something like this will work fine..
    SCAR Code:
    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?

  20. #20
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

    SCAR Code:
    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.

  21. #21
    Join Date
    Dec 2006
    Location
    Copy pastin to my C#
    Posts
    3,788
    Mentioned
    8 Post(s)
    Quoted
    29 Post(s)

    Default

    Now he pmed me I know what he means


    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...

  22. #22
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Read my post before yours.

  23. #23
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    SCAR Code:
    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.
    Verrekte Koekwous

  24. #24
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Changing many different things at once?
    By Sandstorm in forum OSR Help
    Replies: 9
    Last Post: 02-22-2009, 11:22 PM
  2. Changing IP
    By Home in forum Computer Help and Tutorials
    Replies: 21
    Last Post: 02-22-2009, 09:46 PM
  3. IP Changing
    By Hey321 in forum SRL Site Discussion
    Replies: 7
    Last Post: 03-27-2007, 11:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •