Jump to content

Recommended Posts

Posted

Hello.

As with AutoLISP obtained in this way, a sorted list.(see Annex).

list.jpg

  • Replies 21
  • Created
  • Last Reply

Top Posters In This Topic

  • jan_ek

    11

  • irneb

    4

  • Lee Mac

    3

  • fixo

    1

Top Posters In This Topic

Posted Images

Posted

; ----- Error around expression -----

(VL-STRING-ELT A I)

;

; error : bad argument type ; expected at [VL-STRING-ELT]

 

For list (attach)

Function eea-str_int_sort2

 

???

lista.txt

Posted

That list seems to contain symbol names instead of strings. Also you've not included the rest of the code.

 

From that error it looks as if the I variable doesn't contain an integer. See the help on vl-string-elt.

 

Did you use any particular defun from that other thread? Or did you change something in one?

  • 7 months later...
Posted

Hello.

I have the same problem

(defun c:trewq ( / )
(eea-str_int_sort2 (list "DN1""DN10""DN11""DN12""DN13""DN14""DN15""DN16""DN17""DN2""DN3""DN4""DN5""DN6""DN7""DN8""DN9)t)
)
(VL-STRING-ELT A I)
;bad argument type <3> ; expected <INTEGER> at [VL-STRING-ELT]

but if the list looks like this:

(eea-str_int_sort2 (list  "DN1x""DN10""DN11""DN12""DN13""DN14""DN15""DN16""DN17""DN2""DN3""DN4""DN5""DN6""DN7""DN8""DN9")t )

it's ok.

Am I doing something wrong?. Do you work in.Please Help.

Posted

Firstly your list is incorrect, it doesn't end: ..."DN9)... should be ..."DN9")...

 

Secondly, I think you've found a problem with Evgeniy's code. If I run it on his code:

Command: (eea-str_int_sort (list "DN1""DN10""DN11""DN12""DN13""DN14""DN15""DN16""DN17""DN2""DN3""DN4""DN5""DN6""DN7""DN8""DN9") t)
; error: bad argument value: string position out of range 3

But using Lee's _Filesort2 seems to work:

Command: (_filesort2 (list "DN1""DN10""DN11""DN12""DN13""DN14""DN15""DN16""DN17""DN2""DN3""DN4""DN5""DN6""DN7""DN8""DN9") t)
("DN1" "DN2" "DN3" "DN4" "DN5" "DN6" "DN7" "DN8" "DN9" "DN10" "DN11" "DN12" "DN13" "DN14" "DN15" "DN16" "DN17")

Posted

First - my mistake.

Second - thanks for the quick response.

Posted

Here is my modified version of Evgeniy's function, it should perform successfully with your examples:

([color=BLUE]defun[/color] eea-lm-str_int_sort ( l f )
   ([color=BLUE]mapcar[/color] ([color=BLUE]function[/color] ([color=BLUE]lambda[/color] ( a ) ([color=BLUE]nth[/color] a l)))
       ([color=BLUE]vl-sort-i[/color] ([color=BLUE]if[/color] f ([color=BLUE]mapcar[/color] '[color=BLUE]strcase[/color] l) l)
           ([color=BLUE]function[/color]
               ([color=BLUE]lambda[/color] ( a b [color=BLUE]/[/color] i )
                   ([color=BLUE]setq[/color] i ([color=BLUE]vl-string-mismatch[/color] a b))
                   ([color=BLUE]cond[/color]
                       (   ([color=BLUE]zerop[/color] i)
                           ([color=BLUE]if[/color] ([color=BLUE]and[/color] ([color=BLUE]<[/color] 47 ([color=BLUE]ascii[/color] a) 58) ([color=BLUE]<[/color] 47 ([color=BLUE]ascii[/color] b) 58))
                              ([color=BLUE]<[/color] ([color=BLUE]atoi[/color] a) ([color=BLUE]atoi[/color] b))
                              ([color=BLUE]<[/color] a b)
                           )
                       )
                       (   ([color=BLUE]=[/color] i ([color=BLUE]strlen[/color] a)))
                       (   ([color=BLUE]=[/color] i ([color=BLUE]strlen[/color] b)) [color=BLUE]nil[/color])
                       (   ([color=BLUE]and[/color]
                               ([color=BLUE]<[/color] 47 ([color=BLUE]vl-string-elt[/color] a i) 58)
                               ([color=BLUE]<[/color] 47 ([color=BLUE]vl-string-elt[/color] b i) 58)
                           )
                           ([color=BLUE]<[/color] ([color=BLUE]atoi[/color] ([color=BLUE]substr[/color] a ([color=BLUE]1+[/color] i))) ([color=BLUE]atoi[/color] ([color=BLUE]substr[/color] b ([color=BLUE]1+[/color] i))))
                       )
                       (   ([color=BLUE]<[/color] 47 ([color=BLUE]vl-string-elt[/color] a ([color=BLUE]1-[/color] i)) 58)
                           ([color=BLUE]<[/color] ([color=BLUE]atoi[/color] ([color=BLUE]substr[/color] a i)) ([color=BLUE]atoi[/color] ([color=BLUE]substr[/color] b i)))
                       )
                       (   ([color=BLUE]<[/color] a b))
                   )
                )
           )
       )
   )
)

_$ (eea-lm-str_int_sort '("DN1" "DN10" "DN11" "DN12" "DN13" "DN14" "DN15" "DN16" "DN17" "DN2" "DN3" "DN4" "DN5" "DN6" "DN7" "DN8" "DN9") t)
("DN1" "DN2" "DN3" "DN4" "DN5" "DN6" "DN7" "DN8" "DN9" "DN10" "DN11" "DN12" "DN13" "DN14" "DN15" "DN16" "DN17")

  • 1 month later...
Posted

Hi, thanks for the revised version. It works great.

I have one small question.Is is difficult to adapt to the list sorting function

(list ("A" "AC") ("D" "1C") ("2" "#C") ("11" "AsS"))

I would like to sort the position 2 list

Posted

Something like this:

(setq master (list '("A" "AC") '("D" "1C") '("2" "#C") '("11" "AsS")))
(setq match_list (mapcar '(lambda(x)(cons (cadr x)(car x))) master))
(setq unsorted (mapcar 'cadr master))
(setq sorted (eea-lm-str_int_sort unsorted nil))
(setq result nil)
(foreach item sorted
 (setq result (cons (cons (cdr (assoc  item match_list)) item) result)))
(setq result (reverse result))

Posted
I have one small question.Is is difficult to adapt to the list sorting function

(list ("A" "AC") ("D" "1C") ("2" "#C") ("11" "AsS"))

I would like to sort the position 2 list

 

Change:

(if f (mapcar 'strcase l) l)

 

to:

(if f (mapcar 'strcase (mapcar 'cadr l)) (mapcar 'cadr l))

Posted
(setq master (list '("A" "AC") '("D" "1C") '("2" "#C") '("11" "AsS"))) (setq match_list (mapcar '(lambda(x)(cons (cadr x)(car x))) master)) (setq unsorted (mapcar 'cadr master)) (setq sorted (eea-lm-str_int_sort unsorted nil)) (setq result nil) (foreach item sorted (setq result (cons (cons (cdr (assoc item match_list)) item) result))) (setq result (reverse result))

And if we have a list of any number of characters.

And if we want to have a universal function, sorted by the selected argument (np. 3 or 6).

Posted

You'll need in extra input to specify which item to sort on, then you'll use nth instead of cadr to get the relevant item.

 

Edit: Something like this

(defun eea-lm-str_int_sort ( l f n / test)
 (defun test (item) (nth n item))
 (mapcar (function (lambda ( a ) (nth a l)))
         (vl-sort-i (if f (mapcar 'strcase (mapcar 'test l)) (mapcar 'test l))

Posted

Thanks for the help, but could you explain to me how this line"

(defun test (item) (nth n item))

How retrieves item?

Posted

More on NTH, and DEFUN functions... Be sure to note the 'Return values'. :thumbsup:

Posted

It is called through the mapcar's. I made it into a true defun since I use it twice, else it could have been a lambda instead - but in this case I'd have made 2 lambda's exactly the same, so one defun is less coding (and probably slightly more efficient).

 

The mapcar takes the list(s) and sends each item in the list in turn to the test function, that is where it's input argument comes from. In this case I called it item, it's arbirary what you call it - lisp doesn't care, you could've called it this-strange-thing-which-is-passed-to-here-which-I-need-to-do-something-to, or anything your heart desires. I just used item, since it's rather close to what it truly is. Perhaps I could've called it subList instead, might have been more descriptive.

  • 1 year later...
Posted

Hello

I have another problem. In attachment Test lisp. The problem arises when the above position 11.

What am I doing wrong?

stest.lsp

Posted

Thanks for this great sorting routine, it's working well for me!

Posted

Right. At autcad works.Unfortunately, in Bricscad gives the result as an attachment.I have no idea what is wrong

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