Author Topic: Stepping thru a Alpha-Numerica Variable Name  (Read 2280 times)

0 Members and 1 Guest are viewing this topic.

BuckoAk

  • Newt
  • Posts: 69
Stepping thru a Alpha-Numerica Variable Name
« on: March 14, 2011, 12:40:24 PM »
Is it possible to step/change a variable name by combining text and Numbers together by strcat or some other function?
I have been trying to get this to work with several renditions with no luck, can someone shed some light or is it just not possible.
Thanks for any help on this.

Example of what I am trying to accomplish.
Code: [Select]
(setq ss1 "1")
(setq ss2 "2")
(setq ss3 "3")
(setq ss4 "4")
(setq ss5 "5")
;;; 40 plus variables, users anserwers from dcl
;;; Depending on answer a certain command exacuted

;;;(setq test "2") ; for testing

  (setq testn nil test nil tell nil)
  (setq testn 1)
  
  (repeat 5
    (setq testn (itoa testn))
    (setq test (strcat "ss" testn))  ; Roll thru the variables ss1, ss2, ss3 & etc.
    (setq tell (If (= "2" test)
(princ "Yes")  ; Assign Command to this line
      )
    )
    (setq testn (atoi testn))
    (setq testn (+ 1 testn))
  )

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Stepping thru a Alpha-Numerica Variable Name
« Reply #1 on: March 14, 2011, 01:34:46 PM »
Wouldn't a single list be a lot easier to deal with?

However, if you must have it, here's an example:

Code: [Select]
(set (read (strcat "A" "1")) "ALAN")
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Stepping thru a Alpha-Numerica Variable Name
« Reply #2 on: March 14, 2011, 01:36:07 PM »
"Variable" variable names aren't worth the lost hair.  Even if you are already bald.

As noted, lists are the way to go.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Stepping thru a Alpha-Numerica Variable Name
« Reply #3 on: March 14, 2011, 01:50:58 PM »
Think about the difficulty involved and messiness with localisation and you'll soon realise that lists are the way to go - after all, LISP was built for Lists.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Stepping thru a Alpha-Numerica Variable Name
« Reply #4 on: March 14, 2011, 01:51:35 PM »
Think about the difficulty involved and messiness with localisation and you'll soon realise that lists are the way to go :-)
x2
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Stepping thru a Alpha-Numerica Variable Name
« Reply #5 on: March 14, 2011, 01:58:31 PM »
eg.
Code: [Select]
(defun c:Test (/ lst choice item)
  (setq lst '("CHECK" "THIS" "LIST" "OUT"))

  (if (and (setq choice (getint "\nSpecify number [0 = CHECK / 1 = THIS / 2 = LIST / 3 = OUT]: "))
           (setq item (nth choice lst))
      )
    (alert (strcat "You picked \"" item "\" with nth: " (itoa choice)))
  )
  (princ)
)
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Stepping thru a Alpha-Numerica Variable Name
« Reply #6 on: March 14, 2011, 03:54:24 PM »
It easy to do, see here http://www.theswamp.org/index.php?topic=27226.0

BUT lists are your best bet. Learn how to use them. :-)

If you give examples we can help you deal with the problem using lists.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

BuckoAk

  • Newt
  • Posts: 69
Re: Stepping thru a Alpha-Numerica Variable Name
« Reply #7 on: March 14, 2011, 04:46:07 PM »
I totally agree with the list's, I am all for making things easy and keeping my code clean.
Lots of good info, and thanks for the Replies.