A string consist of digits and non-digit characters. The digits contains a series of positive integers. For instance, the string "abc22zit62de0f" contains the integers 22, 62 and 0.
Write a function to calculate the sum of the integers inside a string (es. 22 + 62 + 0 = 84)
			
			
			
				Hi cameo,
my first attempt would be:
  (apply + (map int (clean empty? (parse str {[^0-9]} 0)))))
if it needs to be faster I would do: 
(define (parse-str str)
	(let (total 0)
		(dolist (s (parse str {[^0-9]} 0))
			(unless (empty? s)
				(inc total (int s))))
		total))
			
			
				Hi fdb,
thanks for your functions.
Only a problem: numbers with leading 0 will convert in octal base.
My function:
  (local (numeri expr)
    (setq numeri '())
    (setq expr {[0-9]+})
    (replace expr str (push $0 numeri -1) 0)
    (apply + (map (fn (x) (int x 0 10)) numeri))
  ))
(sum-str "o123p010iru5")
;-> 138
best regards,
cameyo
			
			
			
				In this case we could also do:
138
			
			
				Nice, I didn't know starts-with and didn't know I could use a regex in find-all, but then we could also simplify your code:[code]
> (apply + (map int (find-all {[1-9][0-9]*} "o123p010iru5")))
138
>
			
			
				Or replacing the map with a function for apply, so only traversing the string two times  
> (apply (fn(x y) (+ (int x) (int y))) (find-all {[1-9]d*} "o123p0010iru5") 2) 
138
			
			
				thank you. I learned new things.
			
			
			
				Quote from: cameyo post_id=24891 time=1593772007 user_id=732
thank you. I learned new things.
You're welcome! I learned new things too... :)