Author Topic: split list into lists  (Read 16212 times)

0 Members and 1 Guest are viewing this topic.

SMadsen

  • Guest
split list into lists
« Reply #15 on: January 14, 2005, 06:01:45 PM »
Well .. John .. that's ok but anyone making it a recursive can buy me a ticket to Orlando next year :)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
split list into lists
« Reply #16 on: January 14, 2005, 06:35:45 PM »
I did have this one for strings:
Code: [Select]
;--subroutine to take text with char (ex.~) & create list
(defun gettextlist (str_text char / char_len kks kkk kkl)
  (setq list_text nil)
  (setq char_len (strlen char))
  (setq kks 1
kkk 1
kkl (strlen str_text)
  )
  (while (<= kkk kkl)
    (if (= (substr str_text kkk char_len) char)
      (progn
(setq
 list_text (append list_text
   (list (substr str_text kks (- kkk kks)))
   )
)
(setq kks (+ kkk char_len))
      )
    )
    (setq kkk (1+ kkk))
  )
  (setq list_text (append list_text (list (substr str_text kks))))
)
 ;Call be (GETTEXTLIST "This is a Test String" " ")
 ;returns ("This" "is" "a" "Test" "String")
 ;Note that the CHAR can be more than 1 character long - example -
 ;(GETTEXTLIST "A12B12C" "12") returns ("A" "B" "C")
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
split list into lists
« Reply #17 on: January 14, 2005, 09:36:09 PM »
Quote from: SMadsen
Well .. John .. that's ok but anyone making it a recursive can buy me a ticket to Orlando next year :)

Can't afford a ticket to Orlando :shock:
Code: [Select]
 (defun parselst (item lst / ll)
    (while lst
      (if (eq (car lst) item)
        (setq ll  (if ll
                    (cons (reverse ll) (parselst item (cdr lst)))
                    (parselst item (cdr lst)))
              lst nil
        )
        (setq ll  (cons (car lst) ll)
              lst (cdr lst)
        )
      )
    )
   ll
 )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #18 on: January 14, 2005, 10:01:45 PM »
Quote from: SMadsen
Well .. John .. that's ok but anyone making it a recursive can buy me a ticket to Orlando next year :)


Humm.

Although i would really (read: R E A L L Y) like to beat you Stig. ...I cant without cheating. (I mean it would be easy with a seperate varable or a "container" argument to the main procedure, but other then those options i just cant think of any way to make it a recursive procedure. --BUT that didnt stop me from trying. :lol: )

:P
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #19 on: January 14, 2005, 10:02:52 PM »
...Hold on i just saw CABs post. Im goona check this out!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #20 on: January 14, 2005, 10:12:46 PM »
Ingenious!!!!  

How in dahell did you do that?!  ...:shock: :(

....humm
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #21 on: January 14, 2005, 10:23:24 PM »
CAB your not suposed to be ...damn man!?

Comment that code meh man!

*Se7en turns away and grumbes about recursive process' in recursive procedures.*
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
split list into lists
« Reply #22 on: January 14, 2005, 10:27:19 PM »
Put that in english man .............
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
split list into lists
« Reply #23 on: January 14, 2005, 11:09:04 PM »
Here is my FOREACH version.
Code: [Select]
 (defun parselst (item lst / ll l)
    (foreach x lst
      (cond ((and l (eq x item))
              (setq ll (cons (reverse l) ll) l nil))
            ((eq x item))
            ((setq l (cons x l)))
      )
    )
    (reverse ll)
  )


 Se7en's got me confused :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
split list into lists
« Reply #24 on: January 14, 2005, 11:21:41 PM »
If the idea is to parse a list into sublists based on an item as a trigger (and to be excluded from the results) here's a quick stab ...
Code: [Select]
(defun ParseOnX ( lst x / sublst result )
    (foreach i (reverse lst)
        (if (eq i x)
            (setq
                result (cons sublst result)
                sublst nil                
            )
            (setq sublst (cons i sublst))
        )    
    )
    (vl-remove-if 'null
        (if sublst
            (cons sublst result)
            result
        )    
    )    
)

;;  some data

(setq lst
   '(
        0 1 2 3 4
        0 1 2 3 4
        0 1 2 3 4
        0 1 2 3 4
    )
)

;;  examples ...

(ParseOnX lst 0) >> ((1 2 3 4) (1 2 3 4) (1 2 3 4) (1 2 3 4))

(ParseOnX lst 1) >> ((0) (2 3 4 0) (2 3 4 0) (2 3 4 0) (2 3 4))

(ParseOnX lst 2) >> ((0 1) (3 4 0 1) (3 4 0 1) (3 4 0 1) (3 4))

(ParseOnX lst 3) >> ((0 1 2) (4 0 1 2) (4 0 1 2) (4 0 1 2) (4))

(ParseOnX lst 4) >> ((0 1 2 3) (0 1 2 3) (0 1 2 3) (0 1 2 3))

Is that watcha had in mind?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
split list into lists
« Reply #25 on: January 15, 2005, 12:18:48 AM »
Quote from: MP
Is that watcha had in mind?

exactly!!

thanks for all the posts guys. I've enjoyed them. :D
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #26 on: January 15, 2005, 11:05:04 AM »
lol. CAB that was awesome.

Your procedure is a recursive procedure correct? Well...

(cons (reverse ll) (parselst item (cdr lst)))
;; This is a recursive process (Inside a recursive procedure)

This is both good and bad. This method is bad because it chews up the stack F A S T! (So a fairly moderate size list could overrun your stack.) and its good because its cool as hell! (Its a neat trick used to solve problems like this. ...It kinda gives you a second pair of hands when operating on items.)

Although your procedure is cool/fun/nice to look at/study I wouldnt suggest using it in any major app. Iteration is, and always will be, faster/better when large amounts of data could be used.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
split list into lists
« Reply #27 on: January 15, 2005, 11:11:28 AM »
Thanks for the heads up, as you know I'm not a recursive kinda guy.
I though any routine that called itself was a recursive procedure.

Can you tel me how nush data has to be fed this routine to overrun the stack?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #28 on: January 15, 2005, 11:33:49 AM »
No problem.

heh, no. Im not that smart. (Theres a formula but i dont understand it. :D )

There are two types of Recursive procedures.  (well theres more but for the sake of this convo...)
1. A recursive procedure using an iterative process.

2.  A recursive procedure using a recursive (actualy called: linear recursive) process.

You used both in your procedure.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
split list into lists
« Reply #29 on: January 15, 2005, 11:43:31 AM »
Well you have me confused about this statement.
;; This is a recursive process (Inside a recursive procedure)

This is how the routine works:
With this list (32 53 54 46 48 48 32 32 55 56 32 32 56 56 32)
Code: [Select]
(if (eq (car lst) item) ----  true (32)
(if ll (cons (reverse ll) (parselst item (cdr lst)))
        (parselst item (cdr lst)))
          ll is false so sent the remainder of the list through
          (53 54 46 48 48 32 32 55 56 32 32 56 56 32)
 next loop
   (if (eq (car lst) item) ----  false(53)
   so execute the while loop
   (setq ll  (cons (car lst) ll)
         lst (cdr lst)
which builds (53 54 46 48 48)
at the next 32 send this through (32 55 56 32 32 56 56 32)
because its 32 again send this through (55 56 32 32 56 56 32)
collect this (55 56)
send this through (56 56 32)


Well you get the idea

It may at first glance look like the while loop would send the data through
multiple times but because of the setq lst nil the loop is stopped and only
one pass is done.

So it look like a normal recursive to me, but I am inexperienced at this procedure
and may be missing something.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.