variable contents to variable name 
Author Message
 variable contents to variable name

Is it possible to turn the contents of a variable into a variable name?

I have a text file in the following format:

key_a: keyvalue_1
key_b: keyvalue_2
key_c: keyvalue_3
[blank line]
key_a: keyvalue_4
key_b: keyvalue_5
key_c: keyvalue_6
[blank line]

1. I want to read each line into a variable named "line";
2. use PARSE VALUE line WITH key ':' keyValue;
3. create a variable with the name of the value contained
   in the variable named "key";
4. assign the new variable the value of the associated keyValue;
5. when hitting a blank line, write a string containing the
   values to a file, looking like:
      '09'x || key_b || ',' key_a || '09'x || key_c

I'm stuck on step 3. If you have any helpful hints, please pass them my
way and I'll be very grateful. :)

--
I RARELY check the my-deja e-mailbox. Please use the munged address
below to contact me.


Sent via Deja.com
http://www.*-*-*.com/



Mon, 02 Jun 2003 16:46:22 GMT  
 variable contents to variable name

Quote:

> Is it possible to turn the contents of a variable into a variable name?

> I have a text file in the following format:

> key_a: keyvalue_1
> key_b: keyvalue_2
> key_c: keyvalue_3
> [blank line]
> key_a: keyvalue_4
> key_b: keyvalue_5
> key_c: keyvalue_6
> [blank line]

> 1. I want to read each line into a variable named "line";
> 2. use PARSE VALUE line WITH key ':' keyValue;
> 3. create a variable with the name of the value contained
>    in the variable named "key";
> 4. assign the new variable the value of the associated keyValue;
> 5. when hitting a blank line, write a string containing the
>    values to a file, looking like:
>       '09'x || key_b || ',' key_a || '09'x || key_c

> I'm stuck on step 3. If you have any helpful hints, please pass them my
> way and I'll be very grateful. :)

Try the following:

element = "key_a"
Keys.element = keyvalue_1

While the above works, it is not always easy to know which elements
exist. (If, for example, you would like to list all the Keys.) You may
need a second list that catalogs the key names.



Mon, 02 Jun 2003 22:04:20 GMT  
 variable contents to variable name
On Thu, 14 Dec 2000 08:46:22 GMT, Michael DeBusk

Quote:

>key_a: keyvalue_1
>key_b: keyvalue_2
>key_c: keyvalue_3
>[blank line]
>key_a: keyvalue_4
>key_b: keyvalue_5
>key_c: keyvalue_6
>[blank line]

>1. I want to read each line into a variable named "line";
>2. use PARSE VALUE line WITH key ':' keyValue;
>3. create a variable with the name of the value contained
>   in the variable named "key";
>4. assign the new variable the value of the associated keyValue;
>5. when hitting a blank line, write a string containing the
>   values to a file, looking like:
>      '09'x || key_b || ',' key_a || '09'x || key_c

/* variable "key"    contains "key_b"
   variable "keyval" contains "keyvalue_5"      */

$z$     = Value(key,keyval)  /* load keyval to key    */

/* variable "key_b" now contains "keyvalue_5"   */

Frank Clarke
Tampa Area REXX Programmers' Alliance
REXX Language Assn
Join us at www.rexxla.org



Mon, 02 Jun 2003 22:38:53 GMT  
 variable contents to variable name


Quote:
>Is it possible to turn the contents of a variable into a variable name?

Use the value function:

   call value 'BOB', 'SAM'
   say bob

instead of a string, BOB could have been a variable or function call returning
the name of the variable, as desired.
--

Patrick TJ McPhee
East York  Canada



Tue, 03 Jun 2003 04:19:49 GMT  
 variable contents to variable name


Quote:
> If you have any helpful hints, please pass them my
> way and I'll be very grateful. :)

And indeed I *am* grateful for your assistance. Clueless REXX newbie
that I am, though, I couldn't manage to make any of them work.

I finally decided to use a long SELECT block, and it does the job.

I wrote a REXX script that converts Netscape Communicator address
books, exported to LDIF, to a Pine address book. As much unnecessary
head-banging as I did on the script, though, I could have entered all
the addresses by hand. <g>

If anyone wants the script, anti-anti-spam the anti-spammed e-mail
address in my sig and let me know. It's crude (not much in the way of
error-trapping, and it doesn't yet support mailing lists as I don't use
them) but it does the job.

Thanks again.

--
I RARELY check the my-deja e-mailbox. Please use the munged address
below to contact me.


Sent via Deja.com
http://www.deja.com/



Wed, 04 Jun 2003 15:14:39 GMT  
 variable contents to variable name

Quote:



> > If you have any helpful hints, please pass them my
> > way and I'll be very grateful. :)

> And indeed I *am* grateful for your assistance. Clueless REXX newbie
> that I am, though, I couldn't manage to make any of them work.

> I finally decided to use a long SELECT block, and it does the job.

> I wrote a REXX script that converts Netscape Communicator address
> books, exported to LDIF, to a Pine address book. As much unnecessary
> head-banging as I did on the script, though, I could have entered all
> the addresses by hand. <g>

> If anyone wants the script, anti-anti-spam the anti-spammed e-mail
> address in my sig and let me know. It's crude (not much in the way of
> error-trapping, and it doesn't yet support mailing lists as I don't use
> them) but it does the job.

> Thanks again.

I'm not sure which is better, and elegant program that sort-of works,
or a klunk that does exactly what you need. But, it was an educational
experience. Next time you'll do better.

I often wonder why I spent so much time fussing with a one shot
program, however, if I perfected a new (to me) technique, next time a
similar task will be a no brainer.



Thu, 05 Jun 2003 01:59:43 GMT  
 variable contents to variable name


Quote:
> I'm not sure which is better, and elegant program that sort-of works,
> or a klunk that does exactly what you need.

Don't I know it! My first shot at it was crude and ugly and did exactly
what I needed it to do. It worked perfectly the first time I ran it,
too... which, as I understand, breaks some rule of good programming
practice. ;)

I decided to go in and improve it a couple of reasons. One, because I
couldn't find anything on Hobbes that would do the job and I couldn't
be the ONLY person who needed it done, I thought perhaps someone else
might want to use it too, so it'd better be more flexible and cleaner
and at least have some "help" message. Two, because this is a learning
experience for me, and I know that if I got it right the first time, I
didn't learn anything. :)

Quote:
> Next time you'll do better.

Next time I'll choose a softer wall against which to bang my head. ;)

--
I RARELY check the my-deja e-mailbox. Please use the munged address
below to contact me.


Sent via Deja.com
http://www.deja.com/



Thu, 05 Jun 2003 02:47:41 GMT  
 variable contents to variable name


Quote:


> > If you have any helpful hints, please pass them my
> > way and I'll be very grateful. :)
> And indeed I *am* grateful for your assistance. Clueless REXX newbie
> that I am, though, I couldn't manage to make any of them work.
> I finally decided to use a long SELECT block, and it does the job.

Fine, you got a solution, but it worries me that what people suggested
didn't work for you.  Was this because the suggestions were wrong (they
looked ok to me and I've done similar things often enough), or because
whatever you actually tried wasn't what the suggester's meant - or did
you not explain the problem clearly enough?  

Your problem - as described - shows that the file contained groups of 3
lines of data at a time, followed by a blank line.  Each group uses the
same three "key" values, followed by colon and then data values and each
group only contains three records.    I'm assuming that two test files:

File1:  "abc: this is"
        "def: some"
        "ghi: data"

        "abc: and here"
        "def: is"
        "ghi: more"

File2:  "abc: this is"
        "def: some"
        "ghi: data"

        "pqr: and here"
        "stu: is"
        "vwx: more"

would both be expected to produce the same output (ie it doesn't matter
to you that the values of the pre-colon "key" texts are different in the
second group in the second file).  You would expect to get:

        '09'x || 'some' || ',' 'this is'  || '09'x || 'data'
        '09'x || 'is'   || ',' 'and here' || '09'x || 'more'

as the output from reading either of these files?   If this is so, then
there's no need to pay any attention to the values "abc" "ghi" "stu" etc
of the pre-colon texts.  All you need to do is parse the data values on
three successive lines into three vars of known name and then output them
when you see the blank line.  This depends to some extent on whether you
are reading the source lines one at a time or if you have them all (eg)
in a stem array before you start processing them.  You could use code
like:

     do until no-more-data
        parse pull line ; parse var line ':'data1  
        parse pull line ; parse var line ':'data2
        parse pull line ; parse var line ':'data3

        parse pull blnk
        say '09'x || data2 || ',' data1 || '09'x || data3
     end

or if one at a time:

     store = 0
     do until no-more-data
        parse pull line ; parse var line ':'data

        store      = store + 1
        keep.store = data

        if store == 4 then do
           say '09'x || keep.2 || ',' keep.1 || '09'x || keep.3
           store = 0
        end  
     end

or (the value method):

     do until no-more-data
        parse pull line ; parse var line key':'data

        if strip(line) == "" then do
           say '09'x || def || ',' abc || '09'x || ghi
        end
        else do
           newvarname = key                 /* eg: "def"         */
           newvarvalu = data                /* eg: "some"        */

           assign     = value(newvarname,newvarvalu)

        /* previous line created a var named eg, "def", whose   */
        /* value will be whatever data was introduced by "def:" */
        /* Instead of three lines of rexx this could be written */
        /* as: assign = value(key,data)                         */
        /* but I find it better (in many instances) to make it  */
        /* clear that I am assigning a newvalue to a var whose  */
        /* variable name is whatever is in "newvarname"         */

        end    
     end

Notice that this only works if the three record "key" values are fixed
(ie you know them when you write the program, rather than find them out
for the first time when you read the file).  Also this code would work
for file1 but not file2 (because keys change in file2).   Putting it
another way, this code uses the value function to assign unknown data
values to one of three variables (which MUST be called "abc" "def" and
"ghi").  Why must they?  Because they have to be named explicitly on the
output line.   This was implied by your example file, but might not have
been the case for your real files.

Also note that the value() function returns the value that the named
variable (here: newvarname) had before you updated it.  The returned
value gets put into the var named "assign" because it has to go some
place (so the rexx exec doesn't have a syntax error).    Most people
are only aware of value() when it is used to look-up the value of an
existing variable whose name isn't known by the programmer or when a
neater piece of code is needed (eg: if you wanted to see the values
of a few vars with known names myvar1 myvar2 etc you might code:

     say "myvar1 is:" myvar1
     say "myvar2 is:" myvar2
     ...

But if there were hundreds of these you might write:

     do vv = 1 to 600                         /* eg: 7        */
        varname = "myvar" || vv               /* eg: "myvar7" */
        say varname "is:" value(varname)    
     end  

If you have files where the "key" values are unknown as well (but there's
always three of them) you could do this:

     seenkeys = ""
     do until no-more-data
        parse pull line ; parse var line key':'data

        seenkeys = seenkeys key

        newvarname = key                          /* eg: "def"  */
        newvarvalu = data                         /* eg: "some" */
        assign     = value(newvarname,newvarvalu)

        if strip(line) == "" then do
           say '09'x || value(word(seenkeys,2)) || ','     ,
                        value(word(seenkeys,1)) ||         ,
               '09'x || value(word(seenkeys,3))

           seenkeys = ""
        end    
     end

This works because each time you save a set of 3 data-values in three
variables (whose names we don't know now) you also store the names of
those three variables so that you can look up their values later with
the value() function.  However doing this is pointless if you are not
going to use the names of these variables in any other way.

The code above only works because we know that there will be 3 records
in each group of data lines.  If there were varying numbers of records
we'd need cleverer code at the 'say' statement.   But because we know
that records come in 3s we'd have been better off using the sample code
that stored data-values in data1 data2 and data3.

If the keys could contain spaces, for example a source file like:

File3:  "abc: this is"
        "d f: some"
        "  i: data"

        "i  : and here"
        " i : is"
        "  i: more"

then keeping track of key-values as well as data-values is trickier (as
after the second group of records have been read you'd have stored the
data values of all three records in one variable named "i" (I think)).
I'm not sure if rexx allows a variable named " i " or if it would treat
all of these as "i".  

Even if they were in 3 separate variables the seenkeys variable would
look like "i    i    i" and attempts to retrieve values of the three
stored records would all look up the value of the variable named "i"
which would either fail completely or give the same result 3 times.

You'd really need to keep the separate keys in three separate vars, eg
key1 key2 key3 (or key.1 key.2 and key.3) and you'd have extra logic to
do that too.

Another difficulty is that rexx won't allow a var named "123" so a file
line like "123: one two three" would fail in general.  The way to get
around that is to store everything in a stem array eg mydata.key because
rexx will allow any value at all in the tail of the stem.  Thus

          "123: one two three"   ->     mydata.123   is "one two three"
          "/ \: see?"            ->     mydata./ \   is "see?"  

See how something that seemed simple when you described it, becomes quite
complicated when you try it...        

data that is then written out  

In your example you appear to write the "key" values out rather than the
data values (is that really what you wanted?).  

which might be the same in each file or not - we can't tell.  Nor can we
tell if each file has 3 data lines per group.  Despite the explanation
it's not clear why you want to do this, after all:

 What went wrong with the solution that uses the value function for
example?

--
Jeremy C B Nicoll - my opinions are my own.



Fri, 06 Jun 2003 09:45:13 GMT  
 variable contents to variable name


Quote:
> Fine, you got a solution, but it worries me that what people
> suggested didn't work for you.  Was this because the suggestions were
> wrong (they looked ok to me and I've done similar things often
> enough), or because whatever you actually tried wasn't what the
> suggester's meant - or did you not explain the problem clearly
> enough?

My guess is it's a combination of the latter two. I'm pretty new at
programming in REXX and to programming at all.

Quote:
> Your problem - as described - shows that the file contained groups of
> 3 lines of data at a time, followed by a blank line.

That was a pretty crude example. My SELECT block tests for twenty-one
conditions, including the blank line, but each entry in the LDIF file
may contain more or less than that. Netscape will only export the
fields actually used in the address book; in my case, as I use
relatively few of the fields, each entry may contain around ten or
eleven fields, some more.

(And if I could figure out how to get Netscape Communicator for OS/2 to
export an address book as a comma-delimited ASCII file, none of this
would have come up at all, because those files *do* contain empty
fields.)

Quote:
> Each group uses the same three "key" values, followed by colon and
> then data values and each group only contains three records.

You can expect only certain "key" values, and more than three but less
than, oh, twenty-five.

Quote:
> (ie it doesn't matter to you that the values of the pre-colon "key"
> texts are different in the second group in the second file).  You
> would expect to get:

>         '09'x || 'some' || ',' 'this is'  || '09'x || 'data'
>         '09'x || 'is'   || ',' 'and here' || '09'x || 'more'

> as the output from reading either of these files?

Yes.

Quote:
> there's no need to pay any attention to the values "abc" "ghi" "stu"
> etc of the pre-colon texts.  All you need to do is parse the data
> values on three successive lines into three vars of known name and
> then output them when you see the blank line.

If I could count on the LDIF file having the same number of lines
regardless (i.e., Netscape would export fields as blank) I could do
this. It's what I expected to be able to do at first, but when I
compared a "test" address book to my actual one, I was rather
disappointed.

Quote:
> another way, this code uses the value function to assign unknown data
> values to one of three variables (which MUST be called "abc" "def"
> and "ghi").  Why must they?  Because they have to be named explicitly
> on the output line.   This was implied by your example file, but
> might not have been the case for your real files.

Sadly, it is not the case. It would have made the job much easier.

Quote:
> The code above only works because we know that there will be 3
> records in each group of data lines.

Yes. And the number of lines in each record in the file I'm parsing is
unknown. The number of lines in each record may differ from record to
record, as well as from one file to another.

Quote:
> See how something that seemed simple when you described it, becomes
> quite complicated when you try it...

Oh, yes. :)

Quote:
> In your example you appear to write the "key" values out rather than
> the data values (is that really what you wanted?).

It isn't what I wanted. Here's my own listing from my Netscape Mail
address book, exported to LDIF:


modifytimestamp: 20000413160059Z
cn: DeBusk, Mike

xmozillausehtmlmail: FALSE
givenname: Mike
sn: DeBusk
o: UHCC
xmozillauseconferenceserver: 0
objectclass: top
objectclass: person

My first shot at this simply involved parsing the line keyed "dn:" to
get the name and e-mail address, which is all I, personally, wanted.
When I began to consider how nice it would be to pass the script to
someone else with the same problem I had, I figured I'd better make it
more flexible.

What I wanted was to PARSE VAR line WITH key ': ' keyValue, then create
a variable with the name of the value of key, and assign keyValue to
it. IOW, when parsing:

sn: DeBusk

I would then want to:

key = sn;
do something to create a variable named "sn";
sn = keyValue;
parse the next line

Quote:
>  What went wrong with the solution that uses the value function for
> example?

Quite possibly nothing but that it was offered to an incompetent newbie
REXX programmer. Besides DOS and 4DOS batch files, the closest I've
gotten to programming has been to develop fair competence in the
WordPerfect 6.x for DOS macro language. REXX is new to me.

Thanks so much for your time and attention. It's good to know I have a
forum where I can get such great help.

--
I RARELY check the my-deja e-mailbox. Please use the munged address
below to contact me.


Sent via Deja.com
http://www.deja.com/



Fri, 06 Jun 2003 15:54:08 GMT  
 variable contents to variable name

Quote:
>I would then want to:
>key = sn;
>do something to create a variable named "sn";
>sn = keyValue;
>parse the next line

[sorry this is a bit of a late response, but anyway...]

Since what you are trying to do is build a miniature database, it makes
sense to use the structure that Rexx provided for that purpose, namely
stem variables.

If "line" contains the input, then this can be done as follows:

parse var line key ':' value
key = translate(key)
data.key = value

Now, if you print out the value of data.sn then you should see the
correct value.  The middle line translates the key into uppercase.
This isn't strictly necessary, but it makes the value easier to access
because when you ask for data.sn then Rexx will capitalise the "sn"
before looking it up.  Note that this only works if you do _not_ have a
variable called "sn" in your program.

You can also look the value up like this:

key = "sn"
say data.key

which is less error prone but also less convenient.  In this case you do
not have to uppercase the key names.

Before you start reading in the data, you can initialise all the values to
blank by typing

data. = ""
--

------ http://users.comlab.ox.ac.uk/ian.collier/imc.shtml

New to this group?  Answers to frequently-asked questions can be had from
http://rexx.hursley.ibm.com/rexx/ .



Sat, 14 Jun 2003 08:56:34 GMT  
 variable contents to variable name


Quote:
> [sorry this is a bit of a late response, but anyway...]

I take off my virtual hat to you, sir. You have succeeded in getting
through my thick skull. (I really do have one. It annoys the x-ray
techs sometimes.) I managed to get your suggested solution to work.
It's perfect: simple, elegant, and even one as clueless as I can
implement it. As soon as I read it, I knew... I slapped my forehead and
said, "Of course! THAT was what I was trying to think of!" :)

It shaves 1576 bytes off my script and speeds things up a bit too.

Thank you so much.

And, to the group, again, thanks to all of you for your assistance.

(Now, with what else can I harrass them? <g>)

--
I RARELY check the my-deja e-mailbox. Please use the munged address
below to contact me.


Sent via Deja.com
http://www.deja.com/



Sun, 15 Jun 2003 15:05:51 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Using a variable name for a variable name??

2. object-name = content of a variable (migration from perl)

3. content of content variable

4. array name in variable variable

5. getting variable name from the variable itself?

6. Variables containing variable names

7. name of a variable stored in another variable

8. Variable variable names?

9. values of variable variable-names

10. Class variables / global variables / Init variables

11. field name and key name as a variable

12. field name and key name as a variable

 

 
Powered by phpBB® Forum Software