Jump to content

Recommended Posts

Posted

Hi guys,

 

Yesterday I was experimenting with reading barcode values.

But in this example I'll try to keep it simple:

; Example input: 0011 0012 0033... 00XX
; split 0011 to 00 and 11 (the first and 2nd character are separated from 3rd and 4th)
; store 00 value and calculate 11 value (first and 2nd character is stored, and 3rd with 4rth are used for calculations)
; list is constructed with the calculation values
; sum the total of the calculation values in the while loop
; when loop is exited display the grand total of calculation values

(defun C:test (/ lst calc-val-list )

(setq lst '( 0 ))

(while

(setq get-val (getstring t "\nInput value: ")) ; example 0011
(setq code-val (strcat (substr get-val 1 2 ))) ; stored 00
(setq calc-val (strcat (substr get-val 3 2 ))) ; stored 11 string
(setq calc-val-num (strcat (rtos (atof calc-val) 2 5) )) ; stored 11 number
(princ (strcat "\nCode-val: " code-val " " )) ; display 00 for check
(princ (strcat "\nCalc-val: " calc-val-num " " )) ; display 11 number for check

(setq calc-val-list (append '(calc-val-num) lst)) ; constructs a list with the stored values (calc-val-num) ; <---- problem here?
(princ (strcat "\nCalc-val-list: " calc-val-list " " )) ; this doesn't print, problem?, cannot check

);while

(setq grand-total  (apply '+ (calc-val-list)) ) ; sums the items in the list ; <---- problem here?
(princ (strcat "\nGrand-total: " grand-total " " )) ; this doesn't print, problem?, cannot check

(princ)
);defun

 

As you see I added a little information in the code, but I'll try to explain:

 

1. It expects 4-character input (numbers) [ex: 1234](string)

2. The first two characters are collected for information. [ex: 12](string)

3. The 3rd and 4th characters are used for calculations. (this should be a number) [ex: 34](number)

4. In the loop a list is constructed, adding the numbers,

while values are inputted. [if inputing only 1234, it would be 34+34+34+34...]

5. When exiting the loop it displays the grand-total of the collected items in the list.

 

I have problems in steps 4 and 5 or to be precise, somewhere in these lines of the code:

(setq calc-val-list (append '(calc-val-num) lst)) ; constructs a list with the stored values (calc-val-num) ; <---- problem here?
(princ (strcat "\nCalc-val-list: " calc-val-list " " )) ; this doesn't print, problem?, cannot check

(setq grand-total  (apply '+ (calc-val-list)) ) ; sums the items in the list ; <---- problem here?
(princ (strcat "\nGrand-total: " grand-total " " )) ; this doesn't print, problem?, cannot check

 

I don't have much experience working with lists, can anyone help?

Thanks!

Posted

Try changing this :

(setq calc-val-list (append '(calc-val-num) lst))

 

 

to this:

 

(setq calc-val-list (append 'calc-val-num lst))

Posted

Hi,

 

The variable calc-val-num can not be used like function nor as a symbol, so try the following;

 

(setq calc-val-list (append (list calc-val-num) lst))

Posted

I did not read the complete routine from the first time to encounter that you have other issues , so this must be complete one following your instruction of the user inputs;

 

(setq calc-val-list (append (list calc-val-num) lst))    
(princ (strcat "\nCalc-val-list: " (vl-princ-to-string calc-val-list) " "))                              
   ) ;; end of While 
(setq grand-total (apply '+ (mapcar '(lambda (n) (if (= (type n) 'STR) (atoi n) n)) calc-val-list)))
(princ (strcat "\nGrand-total: " (itoa grand-total) " "))

Posted

Thank you for the help David and Tharwat!

 

It seems that

(setq calc-val-list (append (list calc-val-num) lst))

redefines the list, see what it returns for the "calc-val-list" :

Input value: 0011
Code-val: 00
Calc-val: 11.00000
Calc-val-list: (11.00000 0)
Input value: 0022
Code-val: 00
Calc-val: 22.00000
Calc-val-list: (22.00000 0)

Also I was trying to find a way to exit the loop and to display the grand total, so I did:

(cond
 ((= get-val "total")
  (setq grand-total (apply '+ (mapcar '(lambda (n) (if (= (type n) 'STR) (atoi n) n)) calc-val-list)))
  (princ (strcat "\nGrand-total: " (itoa grand-total) " "))
  )
);cond
                   
) ;; end of While 

But with the problem above I get (after the 0011 and 0022) inputs:

Input value: total
Code-val: to
Calc-val: 0.00000
Calc-val-list: (0.00000 0)
Grand-total: 0

In this case it should display a Grand-total of 33.

Posted

Is this what you are after?

(defun c:test ( / lst str )
   (setq lst '(0 0))
   (while (/= "" (setq str (getstring "\nEnter code <total>: ")))
       (if (wcmatch str "####")
           (setq lst (mapcar '+ lst (list (atoi (substr str 3 2)) (atoi (substr str 1 2)))))
       )
   )
   (print lst) (princ)
)

Posted

Exactly, Lee!

Now I'll study your examples to continue with my experiments.

Thanks alot! :)

Posted

You're welcome - feel free to ask if you have any questions.

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...