Jump to content

Recommended Posts

Posted

How can I assign different variable names for lists where I don't know how many members are in the list? I want to read a file that has an unknown number lines to it and assign each line's value to a variable in this pattern:

 

var1

var2

var3

var4

.....

.....

 

and so on til it reaches the last line.

 

I tried using:

(setq count 1)

 

 

before my main code and

(setq count (1+ count))

 

 

at the end of the read-line portion of the code and it indeed tells me how many lines are in the file thus how many variables I'm talking about but I don't know how to assign each to a variable with a different name.

 

I tried using:

(setq (strcat "var" (itoa count)) (read-line file))

 

but this is obviously wrong, lol.

 

Any suggestions would be appreciated.

Posted

Hi Lonnie,

 

setq only evaluates the second argument (expression argument), set evaluates both arguments:

 

(setq i 0)
(while (set (strcat "var" (itoa (setq i (1+ i)))) (read-line file)))

That said, I would strongly advise against that method of handling data - LISP was built for Lists, why not construct a list from the data?

Posted

Following on

 

(length list) is how many items in a list so you now know how many variables

(nth X list) is select item number x in the list as a variable remember though first is 0 not one

 

(setq x 0)
(setq y (length list))
(repeat Y
(princ (nth X list))
(setq x (+ x 1))
)

 

How do you know which varX stands for what though in the following steps of your program or is your list always perfect. is it like x y z colour shade material where x y is always there ?

Posted
[/code]That said, I would strongly advise against that method of handling data - LISP was built for Lists, why not construct a list from the data?
+1x1,000,000 ... Sounds like Deja-vu. As I've explained to someone in another thread (I think it's in another "infamous" forum) ... everyone starting out with lisp thinks this is a good idea (myself included a while back), until they burn their fingers and realise just how much troubles they've caused themselves.

 

Stick with lists, forget individually named variables. They're not only more trouble than they're worth ... they're also EXTREMELY DANGEROUS. You'll realize later just how elegant lists are in relation to this var1...varN idea. For those who've gone through it, we tend to think: "Why's this guy even considering it?" I know I need to step back and try to think the way I used to before starting in Lisp ... it's easier to think of a Nth variable than to think of a Nth item in a variable - until you become used to it.

Posted
How do you know which varX stands for what though in the following steps of your program or is your list always perfect. is it like x y z colour shade material where x y is always there ?
Also how would you know how many var#'s there are? Will you need to save into yet another counter variable? And keep this updated each and every time you do any modification? Sounds like a whole load of extra work for no benefit whatsoever.
Posted
That said, I would strongly advise against that method of handling data - LISP was built for Lists, why not construct a list from the data?

 

Lee, knowing your lisp abilities, I'd be a fool to not take your advice and not do as you suggest so I will see how I can work out what I'm trying to do with a list.

 

Stick with lists, forget individually named variables. They're not only more trouble than they're worth ... they're also EXTREMELY DANGEROUS.

 

Irneb, I was just thinking that if every line had its own variable it would just be easier in the long run. Honestly the thought never occurred to me that it was that big of a deal.

 

Thanks for the suggestions.

Posted

Sounds like you are generating the data file from a spreadsheet or database. Here is how I handle it:

 

Have either the spreadsheet or database report generate the text file in the proper autolisp format in an ASCII file.

 

Each row or record can be concatenated into a single string fairly easily. I use database reports mostly. They can add the header and footer lines in the output file.

 

test.dat

(setq mydata '(
("ABC" "123" "YES")
("DEF" "456" "NO")
("GHI" "789" "NA")
))

 

Then simply (load) the .dat file

(load "TEST.DAT")

 

Command: !mydata

(("ABC" "123" "YES") ("DEF" "456" "NO") ("GHI" "789" "NA"))

 

-David

DATABASE.JPG

Posted
+1x1,000,000 ... Sounds like Deja-vu. As I've explained to someone in another thread (I think it's in another "infamous" forum)

 

... Care to share a link to that post? :lol::P

 

Reminds me of 'Lord Voldemort'... '[The Site] [which] shall not be named'. lmao

Posted
... Care to share a link to that post? :lol::P

 

Reminds me of 'Lord Voldemort'... '[The Site] [which] shall not be named'. lmao

Closest I can get is doing a google on "variables with index values" and then click on the Cached link of the 1st item found :lol:. See google holds all data ... even bad data!
Posted
Irneb, I was just thinking that if every line had its own variable it would just be easier in the long run. Honestly the thought never occurred to me that it was that big of a deal.
Easier for a human to read? Perhaps. Easier to use in code? Definitely not. You'll end up having to do all sorts of tricks and bending-over-backwards stuff using strcat, itoa, read, eval, etc. Whereas Lisp already has exceptionally efficient functions like mapcar, foreach, apply, nth, car, cdr, etc.

 

Even if you disregard the inefficiencies of using a concatenated string with a converted integer, to be read into a symbol name and then evaluated (instead of simply using nth) ... you end up with a whole bunch of global variables, with all of their problems: possible overwrites of other defuns' values, memory hogging and leaks, difficulty in figuring out just how many varN's there are, etc.

 

To show just how problematic, the only "foolproof" way of clearing these variables from RAM (actually from the other thread - which shall not be named :ouch:) was to close the DWG file and reopen it. Do you want to do that each time your routine completes?

Posted
... Care to share a link to that post? :lol::P

 

Reminds me of 'Lord Voldemort'... '[The Site] [which] shall not be named'. lmao

Fear of a name only increases fear of the thing itself.

Posted
Fear of a name only increases fear of the thing itself.

 

Very true... [Hermione]. :geek:

Posted
Very true... [Hermione]. :geek:

I never pass up a chance to talk about HP.

Posted
Sounds like you are generating the data file from a spreadsheet or database.

 

You were exactly right David, and I took your advice and setup a file like you mention and it works great. Thanks.

 

Irneb, I didn't mean to get you all spun up so sorry about that. Now that you've explained in detail why I shouldn't handle assigning all the lines of my data file its own variable, I do understand the problems that might have arisen should I have actually have done that so I'm appreciative.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...