#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Variables in a text file


    Hello All!

    Here is the situation I am struggling with:

    I have a text file with four variables in it (eg. AA, BB, CC and DD - AA and BB are on the same line separated by a space and CC and DD are on the next line separated by a space).

    In the body of my code, I want to define the variables as some kind of text (eg. AA=store, BB=bank, CC=gym and DD=park).

    Also in the body of my code, I have four sentences. (eg. The man went to the <insert variable AA>, The woman went to the <insert variable BB>, The girl went to the <insert variable CC>, and The boy went to the <insert variable DD>).

    The ultimate goal is to define a number of variables in the body of the code and only have to edit the text file (add new variables EE, FF, GG, HH, etc. following the same pattern as above), change the <insert variable name> as desired and have everything outputted to a new text file. I do not know how to call the variables from the text file and have them run properly in the code though. Can anyone help point me in the right direction? Thanks.

    -jjiimmyy101
  2. #2
  3. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1107
    I don't think the contents of the text file can be called "Variables".

    Use four String variables in your code, in your case AA,BB,CC,DD. Now before you print the statement, just make sure that you strcpy() the contents of the file in the respective variable. It can be arranged so that 1 variable is used per line. So load the first line of text from the file to AA and so on. Now print the statements, something like:
    Code:
    printf("The man went to the %s",AA);
    Post a code and show us what you've tried if you want further help.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    C or C++?
    It is a lot easier to do it in C++, especially since you have the string object.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    m4 or TeX are good macro languages for the task---except that these would require rewriting the dictionary.
    I'd use gawk since it has associative arrays built in.
    This gawk program substitutes on each line from left to right rescanning the full line. Therefor nested substitutions and infinite loops are possible.
    Code:
    # store this program in file dwl.gawk
    # use: gawk -f dwl.gawk dictionary template
    # example:
    #
    #  dictionary
    #AA=store
    #BB=bank
    #CC=gym
    #DD=park
    #
    #  template
    #Also in the body of my code, I have four sentences. (eg. The man went to the <insert variable AA>, The woman went to the <insert variable BB>, The girl went to the <insert variable CC>, and The boy went to the <insert variable DD>
    #
    #  result
    #$ gawk -f dwl.gawk dict template
    #Also in the body of my code, I have four sentences. (eg. The man went to the store, The woman went to the bank, The girl went to the gym, and The boy went to the park
    
    {
        if (FILENAME==ARGV[1]) {
    	if (2 == split($0,a,/=/)) {
    	    dict[a[1]] = a[2]
    	}
        } else {
    	for (i = match($0,/<insert variable [[:alpha:]]+>/); i ; i = match($0,/<insert variable [[:alpha:]]+>/)) {
    	    x = substr($0,1,i-1) # the text up to next <
    	    j = match(substr($0,i),/>/)
    	    y = dict[substr($0,i+17,j-18)] # the dictionary substitution
    	    z = substr($0,i+j)	   # the text following >
    	    $0 = x y z
    	}
    	print
        }
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    I'm having a lot of trouble understanding your problem, especially the roles of the "text file" and "body of code." Anyway, here's my solution to an approximation of your problem (in PHP). [php=example.php]<?php $AA = "store"; $BB = "bank"; $CC = "gym"; $DD = "park"; ?>
    The man went to the <?php echo $AA?>,
    the woman went to the <?php echo $BB?>,
    the girl went to the <?php echo $CC?>,
    and the boy went to the <?php echo $DD?>.
    [/php](run with: php example.php)

IMN logo majestic logo threadwatch logo seochat tools logo