Sum of local variables

Sum of local variables

BeKirra
Advisor Advisor
808 Views
7 Replies
Message 1 of 8

Sum of local variables

BeKirra
Advisor
Advisor

I know how to use "while" function until I get to this point.
Here is what I am going to achieve:

(while (/= UserInput "")
(setq UserInput (getstring "\nInput: "))
); end of while

Obviously, the content of user' input and the number of inputs can be anything.
At the end of my code, I'd like to create a local variable "SumOfUserInput":

if the user's inputs are
1) "Input_1" - can be anything
2) "Input_2" - can be anything
3) "Input_3" - can be anything
4) "Input_4" - can be anything
5) "Input_5" - can be anything

 

then the new variable "SumOfUserInput" is expected to pick up all the inputs:

(setq SumOfUserInput (strcat "Input_1" "Input_2" "Input_3" "Input_4" "Input_5" …))

I could not figure it out.

 

Thanks in advance.

Please mark "Accept as Solution" and "Like" if my reply resolves the issue and it will help when others need helps.
= ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ =
A circle is the locus of a cursor, starting and ending at the same point on a plane in model space or in layout such that its distance from a given coordinates (X,Y) is always constant.
X² + Y² = C²
0 Likes
809 Views
7 Replies
Replies (7)
Message 2 of 8

BeKirra
Advisor
Advisor

Sorry guys, instead of these lines:

 

then the new variable "SumOfUserInput" is expected to pick up all the inputs:

(setq SumOfUserInput (strcat "Input_1" "Input_2" "Input_3" "Input_4" "Input_5" …))

 

I meant this:

 

How do I pick up all the user inputs, rather than manually adding them up by writing the following

(setq SumOfUserInput (strcat "Input_1" "Input_2" "Input_3" "Input_4" "Input_5" …))

 

the new local variable should be something like:

SumOfUserInput = > "Input_1, Input_2, Input_3, Input_4, Input_5, …"

 

I could not figure it out.

 

Thanks in advance.

Please mark "Accept as Solution" and "Like" if my reply resolves the issue and it will help when others need helps.
= ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ =
A circle is the locus of a cursor, starting and ending at the same point on a plane in model space or in layout such that its distance from a given coordinates (X,Y) is always constant.
X² + Y² = C²
0 Likes
Message 3 of 8

Ajilal.Vijayan
Advisor
Advisor

Hi,

Try to strcat the userinput within the loop.

Click the +sign to see the Code

Spoiler
 
(setq SumOfUserInput "")
(setq UserInput nil)
(while (/= UserInput "")
	(setq UserInput (getstring "\nInput: "))
	(setq SumOfUserInput (strcat SumOfUserInput UserInput))
);while
(princ SumOfUserInput)

 

Message 4 of 8

ВeekeeCZ
Consultant
Consultant

Maybe like this...

 

(setq SumOfUserInput "")
(while (/= "" (setq UserInput (getstring "\nInput: ")))
  (setq SumOfUserInput (strcat SumOfUserInput ", " UserInput))
)
(setq SumOfUserInput (substr SumOfUserInput 3))

 

Message 5 of 8

hmsilva
Mentor
Mentor

@BeKirra wrote:

I know how to use "while" function until I get to this point.
Here is what I am going to achieve:

(while (/= UserInput "")
(setq UserInput (getstring "\nInput: "))
); end of while

Obviously, the content of user' input and the number of inputs can be anything.
At the end of my code, I'd like to create a local variable "SumOfUserInput":

if the user's inputs are
1) "Input_1" - can be anything
2) "Input_2" - can be anything
3) "Input_3" - can be anything
4) "Input_4" - can be anything
5) "Input_5" - can be anything

 

then the new variable "SumOfUserInput" is expected to pick up all the inputs:

(setq SumOfUserInput (strcat "Input_1" "Input_2" "Input_3" "Input_4" "Input_5" …))

I could not figure it out.

 

Thanks in advance.


Another,

 

 ; while a valid usrt input
(while (/= (setq UserInput (getstring "\nInput: ")) "")
 ; store the strings in a list, in order to access the
 ; user input separately later, if needed
    (setq Input_lst (cons UserInput Input_lst))
) ; end of while
 ; if not nil Input_lst
(if Input_lst
    (progn
 ; reverse the list to to restore the correct input order
        (setq Input_lst (reverse Input_lst))
 ; set the SumOfUserInput variable, with the concatenation of multiple strings setarated by ", "
        (setq SumOfUserInput (vl-string-left-trim ", " (apply 'strcat (mapcar '(lambda (x) (strcat ", " x)) Input_lst))))
    ) ; progn
) ; if

 

Hope this helps,
Henrique

EESignature

Message 6 of 8

stevor
Collaborator
Collaborator

Ie:

 (setq AllStr "")

 (while (/= (setq UserInput (getstring "\nInput: ")) "")

   (setq AllStr (strcat AllStr UserInput))

  ); w

 

Note that the spacebar also terminates the input.

S
Message 7 of 8

Kent1Cooper
Consultant
Consultant

If you have any reason to want them to be stored in separate numerically-incremented variables named in the format in your listing, for direct access to each input later, and even to prompt the User with the variable name into which it will put each input, that can be done this way:

 

(setq InputInt 0)
(while
  (/=
    (setq UserInput (getstring (strcat "\nInput_" (itoa (setq InputInt (1+ InputInt))) ": ")))
    ""
  ); /=
  (set (read (strcat "Input_" (itoa InputInt))) UserInput)
); while
(if Input_1; at least one User input entered?
  (progn ; then -- string them together
    (setq ; starting values:
      SumOfUserInput ""
      int 0
    ); setq
    (repeat (1- InputInt)
      (setq SumOfUserInput
        (strcat
          SumOfUserInput
          ", "
          (eval (read (strcat "Input_" (itoa (setq int (1+ int))))))
        ); strcat
      ); setq
    ); repeat
    (setq SumOfUserInput (vl-string-left-trim ", " SumOfUserInput))
  ) ; progn
); if
Kent Cooper, AIA
Message 8 of 8

BeKirra
Advisor
Advisor

Thanks to everyone's helps/comments.

And Sorry I have been very busy since last post.

 

I just figured it out and my code works fine.

Please mark "Accept as Solution" and "Like" if my reply resolves the issue and it will help when others need helps.
= ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ = ♪ = ♫ =
A circle is the locus of a cursor, starting and ending at the same point on a plane in model space or in layout such that its distance from a given coordinates (X,Y) is always constant.
X² + Y² = C²
0 Likes