Thread: Evaluating strings

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    Question Evaluating strings

    Hi!

    Am very new to this board ... have checked out the FAQs etc by the way before posting this questions as i can't find what i'm after anywhere - so somone might be able to help

    Having a Coldfusion background, there was a very handy function called Evaluate(), and i am looking for the C equivalent of it (if it exists). As an example of what it does (explanation after code sample):

    <cfset variable1 = 10>
    <cfset i = 1>
    <cfset myvariable = Evaluate("variable" & i)>

    Variables variable1 and i are defined,
    myvariable is set to the value of variable1 (10) by evaluating the string in the Evaluate() function - the string being the text "variable" concatenated with the value of variable i.

    I hope this is clear - i have done a little C programming before, but knowing if this could be achieved would be great ...

    Thanks in advance ....

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You can use the function strcat(), you will need to include string.h for this.

    The declaration of strcat() is:

    char *strcat (char * restrict s1, const char * restrict s2);

    It puts the string pointed to by s2 after s1. So if s1 is the string "Hello " and s2 is the string "World", then strcat (s1, s2) would result in s1 becoming "Hello World".

    [edit]
    More information on string-functions and other standard functions can be found here.

    http://www.f.kth.se/~f95-pax/refs/C%20Library%20Ref/
    [/edit]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Shiro, that is not what evaluate does. What he wants is something not possible in C, as far as I know. All variable names becomes address at compile/link time. Be able to refer to a variable by it's name in a string is only possible in interpreted languages. C does not have this capability.

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Evaluating strings

    Originally posted by whitel1977
    Hi!

    Am very new to this board ... have checked out the FAQs etc by the way before posting this questions as i can't find what i'm after anywhere - so somone might be able to help

    Having a Coldfusion background, there was a very handy function called Evaluate(), and i am looking for the C equivalent of it (if it exists). As an example of what it does (explanation after code sample):

    <cfset variable1 = 10>
    <cfset i = 1>
    <cfset myvariable = Evaluate("variable" & i)>

    Variables variable1 and i are defined,
    myvariable is set to the value of variable1 (10) by evaluating the string in the Evaluate() function - the string being the text "variable" concatenated with the value of variable i.

    I hope this is clear - i have done a little C programming before, but knowing if this could be achieved would be great ...

    Thanks in advance ....
    C is a compiled, not interpreted language.
    hello, internet!

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    After rereading the post and reading some info on the web I think whitel1977 means something different. The argument to Evaluate evaluates to an expression.

    http://www.houseoffusion.com/cfdocs1...on/dwa07_2.htm

    In the example of whitel1977, the name of a variabele is being build. Concatenating the string "variable" with the value 1 results in variable1. It makes me think of hash-tables, like Perl implements them:

    $h{"a"} = 1;
    $h{"b"} = 2;
    print $h{"a"}, "\n"; # prints 1
    print $h{"b"}, "\n"; # prints 2

    Such hash-tables can be implemented in C, but not in the easy way like in ColdFusion or Perl. A very basic way is to implement a linked list where each node has a unique string which identifies the node.
    Last edited by Shiro; 11-23-2002 at 12:32 PM.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    I think you are misunderstanding Shiro. What Evaluate does is, among other things, takes the string you give it, and then returns the value of the variable named that string.

    myass = 10;

    mypantsize = Evaluate("myass");

    Would make mypantsize be equal to 10. If you can do this in C then be sure to tell us all how it's done.

  7. #7
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    i've seen it done in C, sort of:
    Code:
    int foo, bar, bed, grok;
    
    typedef struct
    {
      int *pi;
      const char *name;
    } fooentry_t;
    
    const int numentries = 4;
    
    fooentry_t fooentries[4] = {
    {&foo, "foo"},
    {&bar, "bar"},
    {&bed, "bed"},
    {&grok, "grok"}
    };
    
    int setfoo (int val, const char *name)
    { // attempts to set foo with name name to val val
      int i;
      for (i = 0; i < numentries; i++)
      {
        if (strcmp (name, fooentries[i].name) == 0)
        {
          *(fooentries[i].pi) = val;
          return 0;
        }
      }
      return 1; // fail
    }
    start with somethng like that
    hello, internet!

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Oh, ickie! hehhee

    Why don't we all just edit C so it allows this, huh? I'm sure the ISO and ANSI groups will appreciate it. I'll start working on my gcc patch ASAP.

    Your code is theortically sound, but I it lacks the runtime necesity. Also lacks the ability to deal with arrays, etc. But that would be too much work for a post and all. Anyways, This is just my personal opinion, but I think any code which requires eomthing like 'Evaluate' is probably rank anyways and needs a rewrite.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    Question

    Phew .... didn't expect to get so many replies! - thanks ....
    well, that clears one thing up at least, knowing that there isn't a predefined function to do that,..... but you do have point of what i was looking for ...

    Basically what I am trying to do (which i can achieve with alot of repetitive code) is that i have a whole lot of variable pairs specifying width/height
    eg

    dk1_WIDTH = 10;
    dk1_HEIGHT = 2;
    dk2_WIDTH = 20;
    dk2_HEIGHT = 5;

    etc...

    and the idea behind requiring Evaluate is that i have another variable (like an index), and if it is set to

    index = 1;

    then i want to set a generic width/height variable pair to that specified by the index

    eg (partly in C and pseudo-ish code)

    WIDTH = 0;
    HEIGHT = 0;

    WIDTH = Evaluate("dk" & index & "_WIDTH");
    HEIGHT = Evaluate("dk" & index & "_HEIGHT");

    therefore, index = 1 means that WIDTH = 10 and HEIGHT = 2 (based on the values set earlier) .....

    is there another way to do this (i currently am using a whole bunch of case statements containing LOTS of repetitive code!)

  10. #10
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    what you want in this case is arrays.
    hello, internet!

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    thanks

    I think i see what you mean - store the value pairs in each index of the array and extract the values as need be ....

    Thanks again ... been very helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. evaluating strings
    By tdean in forum C Programming
    Replies: 3
    Last Post: 02-03-2003, 01:37 PM
  5. evaluating strings
    By steve8820 in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 02:15 PM