Author Topic: Split String for list_box  (Read 2949 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
Split String for list_box
« on: February 18, 2018, 06:07:42 PM »
Hey guys,
I've got this scenario with a pretty long string that needed to be splitted at certain character(s) in order to be displayed correctly in a list_box tile.
My main uses multiple listboxes to display different paragraphs of info.

So In other words (code) :

Code - Auto/Visual Lisp: [Select]
  1. _$ (mapcar
  2.   '(lambda (x / tmp)
  3.     (setq tmp (SplitStringForLB x "The Quick Brown Fox Jumps Over The Lazy Dog."))
  4.     (cons x (cons (length tmp) tmp))
  5.   ); lambda
  6.   '(2 4 6 8 10 12 14 16 18 20 22 24 26)
  7. ); mapcar
  8. >>
  9. ( ; (car = argument) (cadr = amount of strings returned) (cdddr = the list of words which was returned)
  10.   (2 9 "The" "Quick" "Brown" "Fox" "Jumps" "Over" "The" "Lazy" "Dog.")
  11.   (4 6  "The Quick" "Brown" "Fox Jumps" "Over" "The Lazy" "Dog.") ; strlen of 4 or above is attempted to be splitted on the first following occurence of " " or "."
  12.   (6 5 "The Quick" "Brown Fox" "Jumps Over" "The Lazy" "Dog.") ; strlen of 6 or above is attempted to be splitted on the first following occurence of " " or "."
  13.   (8 5 "The Quick" "Brown Fox" "Jumps Over" "The Lazy" "Dog.")
  14.   (10 3 "The Quick Brown" "Fox Jumps Over" "The Lazy Dog.")
  15.   (12 3 "The Quick Brown" "Fox Jumps Over" "The Lazy Dog.")
  16.   (14 3 "The Quick Brown" "Fox Jumps Over" "The Lazy Dog.")
  17.   (16 3 "The Quick Brown Fox" "Jumps Over The Lazy" "Dog.")
  18.   (18 3 "The Quick Brown Fox" "Jumps Over The Lazy" "Dog.")
  19.   (20 2 "The Quick Brown Fox Jumps" "Over The Lazy Dog.")
  20.   (22 2 "The Quick Brown Fox Jumps" "Over The Lazy Dog.")
  21.   (24 2 "The Quick Brown Fox Jumps" "Over The Lazy Dog.")
  22.   (26 2 "The Quick Brown Fox Jumps Over" "The Lazy Dog.")
  23. )

Heres the function I used:
Code - Auto/Visual Lisp: [Select]
  1. (defun SplitStringForLB  (n s / a tmp r)
  2.   (setq tmp "")
  3.   (while (/= "" s)
  4.     (setq a (substr s 1 1))
  5.     (and (>= (strlen tmp) n) (member a '(" " ".")) (setq r (cons (strcat tmp (cond ((= a ".") (setq a "") ".")(""))) r)) (setq tmp ""))
  6.     (setq tmp ('((a b) (if (and (= "" a) (= " " b)) a (strcat a b))) tmp a))
  7.     (setq s (substr s 2))
  8.   ); while
  9.   (reverse ('((a b) (if (/= "" a) (cons a b) b)) tmp r))
  10. ); defun SplitStringForLB

Code - Auto/Visual Lisp: [Select]
  1. _$ (SplitStringForLB 6 "Abc de. Klmn op") >> ("Abc de." "Klmn op")

Just sharing.. and I think it would be interesting to see your attempts on this.  :tongue2:
« Last Edit: February 18, 2018, 07:01:07 PM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: Split String for list_box
« Reply #1 on: February 18, 2018, 06:37:52 PM »
i think a 'period' should always stick to the previous word
(SplitStringForLB 6 "Abc de. Klmn op")
?
« Last Edit: February 18, 2018, 06:41:06 PM by VovKa »

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Split String for list_box
« Reply #2 on: February 18, 2018, 06:48:15 PM »
Not exactly the same but you may want to explore this very old routine.
https://www.theswamp.org/index.php?topic=1392.0
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.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Split String for list_box
« Reply #3 on: February 18, 2018, 07:06:38 PM »
i think a 'period' should always stick to the previous word
(SplitStringForLB 6 "Abc de. Klmn op")
?

Ah, thanks for pointing that out Vova - modified the code.


Not exactly the same but you may want to explore this very old routine.
https://www.theswamp.org/index.php?topic=1392.0

CAB, your link is broken (actually I was unable to access all the recent links you posted on the forum).
Anyway if you are refering to to your sparser, I'm aware of it. :)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Split String for list_box
« Reply #4 on: February 18, 2018, 07:15:02 PM »
Not exactly the same but you may want to explore this very old routine.
https://www.theswamp.org/index.php?topic=1392.0

CAB, your link is broken (actually I was unable to access all the recent links you posted on the forum).
Anyway if you are refering to to your sparser, I'm aware of it. :)

Not broken, just not valid https - change it to plain http and you should be fine: http://www.theswamp.org/index.php?topic=1392.0



Back on topic, here's an old one of mine, just looking at spaces.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Split String for list_box
« Reply #5 on: February 19, 2018, 09:42:09 AM »
Not broken, just not valid https - change it to plain http and you should be fine: http://www.theswamp.org/index.php?topic=1392.0

Sorry about my misunderstanding.
Well thats some hefty routine (I expected just some short and cute subfoo).
My main has just informative purpose, but the focus here is on this subfoo.

Back on topic, here's an old one of mine, just looking at spaces.

For sure I missed that!
Actually I've expected that you might have something like this, due your tremendous effort of writing lisp routines'n'subfunctions.  :-D



I did a small modification here, so if the following character was used ' • ' it would write as new-row and would tab(space) the next subrows [see pic] :

Code: [Select]
(defun SplitStringForLB  (n s / a tmp r flg)
  (setq tmp "")
  (while (/= "" s)
    (setq a (substr s 1 1))
    (or
      (and (>= (strlen tmp) n) (member a '(" " ".")) (setq r (cons (strcat tmp (cond ((= a ".") (setq a "") ".")(""))) r)) (progn (if flg (setq tmp "   ") (setq tmp "")) t))
      (and (= a "\n") (progn (setq flg nil) t) (setq r (cons tmp r)) (setq tmp ""))
      (and (= a "•") (progn (setq flg t) t) (setq r (cons tmp r)) (setq tmp ""))
    ); or
    (setq tmp ('((a b) (if (and (= "" a) (= " " b)) a (strcat a b))) tmp a))
    (setq s (substr s 2))
  ); while
  (reverse ('((a b) (if (/= "" a) (cons a b) b)) tmp r))
); defun SplitStringForLB

So the above yields:

Code: [Select]
_$ (SplitStringForLB 12 "• This would be the first thing • This is the second thing • This is the third thing")
>>
("" "• This would"   "    be the first" "    thing "
  "• This is the"   "    second thing" "    "
  "• This is the"   "    third thing"
)

; Proper return would be (I think) :
( "• This would"   "    be the first" "    thing "
  "• This is the"   "    second thing"
  "• This is the"   "    third thing"
)


_$ (SplitStringForLB 12 "• This would be the first thing • This is the second thing\nthis is a new row • This is the third thing")
>>
("" "• This would"
  "    be the first"
  "    thing "
  "• This is the"
  "    second thing"
  "\nthis is a new"
  "row "
  "• This is the"
  "    third thing"
)

Which I agree is 'buggy' so theres a way of improvement.
But still good to visualise in the list_box:
« Last Edit: February 19, 2018, 03:24:01 PM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg