A variable number of Variables

A variable number of Variables

Anonymous
Not applicable
1,157 Views
5 Replies
Message 1 of 6

A variable number of Variables

Anonymous
Not applicable

I am looking for some help to create some code to create and assign values to a number of variables - such as A1, A2, A3  ....An,  where n is a variable number depending on the circumstances of external data.   

 

I have done this in the very long distant past, but I have forgotten and/or lost the knack or coding technique, although I think that it used the (read) or (set) functions. 

 

I have to extract strings from a command and quote delimited file from Excel on a per line basis (e.g. "text1", "text2", "text3") and subsequently assign A1 to "text1", A2 to "text2" etc but the number of strings is variable  - depending on the project.

 

Can someone please help me get started.

 

Thanks

 

George in New Zealand

0 Likes
Accepted solutions (1)
1,158 Views
5 Replies
Replies (5)
Message 2 of 6

Shneuph
Collaborator
Collaborator
Accepted solution

var.JPG

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 3 of 6

Ranjit_Singh
Advisor
Advisor

Let's assume you read the excel file and got all the data in a list like

(setq lst ("text1" "text2" "text3".....))

you can do this to assign each to a variable

(setq lst '("text1" "text2" "text3")
      ctr 0)
(mapcar '(lambda (x) (setq ctr (1+ ctr)) (set (read (strcat "A" (itoa ctr))) x)) lst)

 

0 Likes
Message 4 of 6

scot-65
Advisor
Advisor
In my opinion Ranjit's first code quote is all that is needed.

He is showing a variable that contains a undetermined number
of items that can be directly accessed using NTH.

(nth 1 lst) = "text1"
(nth 3 lst) = "text3"
...

Use LENGTH to determine how many members are in the list.
CONS is your friend to build the initial list (then reverse the list
when the last one is fetched).

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 5 of 6

dgorsman
Consultant
Consultant

Lists (it is the "L" in LISP, after all)!  If sequential indexing is impractical, then use dotted pairs.  Note that "dotted pairs" aren't restricted to a single value as (index . value), the "value" can be a list itself, which may be a list of dotted pairs, which could be a list, and so on.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 6 of 6

Anonymous
Not applicable

Many thanks for wonderful solution 🙂

0 Likes