Get part off filename (list)

Get part off filename (list)

djurk_haas
Advocate Advocate
826 Views
4 Replies
Message 1 of 5

Get part off filename (list)

djurk_haas
Advocate
Advocate

Hello,

 

I can't figger out why my code below gives: error: bad argument type: consp "" instead of "B"

 

(setq str (strcat (substr (getvar 'dwgname) 1 (vl-string-search ".dwg" (getvar 'dwgname))) " "))
 (while (/= str "")
    (setq
      sub (substr str 1 (setq i (vl-string-search " " str)))
      ret (cons sub ret)
     str (substr str (+ i 2))
   )
  )

 (reverse ret)
;returns ("RV" "CZE" "B" "W0100" "P")

 

(setq e (nth 3 str))

 

Thanks in advance!

0 Likes
Accepted solutions (1)
827 Views
4 Replies
Replies (4)
Message 2 of 5

dlanorh
Advisor
Advisor

@djurk_haas wrote:

Hello,

 

I can't figger out why my code below gives: error: bad argument type: consp "" instead of "B"

 

(setq str (strcat (substr (getvar 'dwgname) 1 (vl-string-search ".dwg" (getvar 'dwgname))) " "))
 (while (/= str "")
    (setq
      sub (substr str 1 (setq i (vl-string-search " " str)))
      ret (cons sub ret)
     str (substr str (+ i 2))
   )
  )

 (reverse ret)
;returns ("RV" "CZE" "B" "W0100" "P")

 

(setq e (nth 3 str))

 

Thanks in advance!


str is a string not a list. nth works on lists and if str were a list as in ("RV" "CZE" "B" "W0100" "P") (nth 3 lst) would return "W0100" as lists are 0 (zero) based

 

try (setq e (nth 2 (reverse ret)))

I am not one of the robots you're looking for

0 Likes
Message 3 of 5

ВeekeeCZ
Consultant
Consultant

You should use (setq e (nth 3 ret)))

 

 

0 Likes
Message 4 of 5

djurk_haas
Advocate
Advocate

Thanks that does the trick!!

One more thing:

(setq e (nth 0 ret)) returns "P" instead of "CZE"?

0 Likes
Message 5 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

See the code.

You reversed (reverse red) but did not save it!? Unless it's a result of your sub, you need to save it (setq ret (reverse ret))

 

Then, CZE is 2nd item in the list, so the index should be 1 (since 0 is for the first one).

 
0 Likes