Author Topic: how can i count how many times an letter appears in a string  (Read 3669 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
how can i count how many times an letter appears in a string
« on: March 05, 2013, 09:29:17 AM »
since yesterday ive been having trouble using the search bar here.
no matter what i search for i get this error

"Each word must be at least two characters long."
maybe im entering to long of a search string?

anyway, does anyone have a function that tells you how many times a letter or number appears in a string?

thanks

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: how can i count how many times an letter appears in a string
« Reply #1 on: March 05, 2013, 09:32:50 AM »
Using existing code:

Count Items
Code - Auto/Visual Lisp: [Select]
  1. (defun lettercount ( str )
  2.     (LM:CountItems (mapcar 'chr (vl-string->list str)))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (lettercount "Lee Mac")
  2. (("L" . 1) ("e" . 2) (" " . 1) ("M" . 1) ("a" . 1) ("c" . 1))

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: how can i count how many times an letter appears in a string
« Reply #2 on: March 05, 2013, 09:33:24 AM »
Use vl-string->list and sort and count duplicates.
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
Re: how can i count how many times an letter appears in a string
« Reply #3 on: March 05, 2013, 09:34:12 AM »
Lee is way too fast  :-D
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.

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: how can i count how many times an letter appears in a string
« Reply #4 on: March 05, 2013, 09:34:46 AM »
Or for a single character:
Code - Auto/Visual Lisp: [Select]
  1. (defun countletter ( char str / lst )
  2.     (setq lst (vl-string->list str))
  3.     (- (length lst) (length (vl-remove (ascii char) lst)))
  4. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (countletter "e" "Lee Mac")
  2. 2

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: how can i count how many times an letter appears in a string
« Reply #5 on: March 05, 2013, 09:35:49 AM »

ronjonp

  • Needs a day job
  • Posts: 7531
Re: how can i count how many times an letter appears in a string
« Reply #6 on: March 05, 2013, 09:41:37 AM »
And another  :-)

Code - Auto/Visual Lisp: [Select]
  1. (defun _count (letter string)
  2.   (length (vl-remove-if-not
  3.             (function (lambda (x) (= (strcase letter) (strcase (chr x)))))
  4.             (vl-string->list string)
  5.           )
  6.   )
  7. )
  8. ;; Not case sensitive
  9. (_count "T" "this is a test")
  10. 3
  11.  

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: how can i count how many times an letter appears in a string
« Reply #7 on: March 05, 2013, 09:46:55 AM »
And another:
Code - Auto/Visual Lisp: [Select]
  1. (defun countletter ( char str / pos )
  2.     (if (setq pos (vl-string-search char str))
  3.         (1+ (countletter char (substr str (+ pos 2))))
  4.         0
  5.     )
  6. )

And another:
Code - Auto/Visual Lisp: [Select]
  1. (defun countletter ( char str )
  2.     (apply '+ (mapcar '(lambda ( x ) (if (= x (ascii char)) 1 0)) (vl-string->list str)))
  3. )

And another:
Code - Auto/Visual Lisp: [Select]
  1. (defun countletter ( char str )
  2.     (if (= "" str)
  3.         0
  4.         (if (= (ascii char) (ascii str))
  5.             (1+ (countletter char (substr str 2)))
  6.             (countletter char (substr str 2))
  7.         )
  8.     )
  9. )

And another:
Code - Auto/Visual Lisp: [Select]
  1. (defun countletter ( char str / lst num )
  2.     (setq char (ascii char)
  3.           lst  (vl-string->list str)
  4.           num  0
  5.     )
  6.     (while (setq lst (member char lst))
  7.         (setq lst (cdr lst)
  8.               num (1+  num)
  9.         )
  10.     )
  11.     num
  12. )
« Last Edit: March 05, 2013, 12:53:40 PM by Lee Mac »

andrew_nao

  • Guest
Re: how can i count how many times an letter appears in a string
« Reply #8 on: March 05, 2013, 09:52:51 AM »
wow you guys are fast..

Error: no function definition: LM:COUNTITEMSAutomation Error. Drawing is busy.

drawing is busy? never seen that before


Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: how can i count how many times an letter appears in a string
« Reply #9 on: March 05, 2013, 09:55:29 AM »
Error: no function definition: LM:COUNTITEMS

I'm guessing you didn't load my LM:CountItems function before running the code?

andrew_nao

  • Guest
Re: how can i count how many times an letter appears in a string
« Reply #10 on: March 05, 2013, 09:56:25 AM »
i gotta do more reading on that lambda, i see that alot and just lack the brain cell to grasp its use..

thanks all for the help :)

andrew_nao

  • Guest
Re: how can i count how many times an letter appears in a string
« Reply #11 on: March 05, 2013, 09:57:22 AM »
Error: no function definition: LM:COUNTITEMS

I'm guessing you didn't load my LM:CountItems function before running the code?

well yea that i knew what the issue was, i was just curious about the drawing is busy part...

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: how can i count how many times an letter appears in a string
« Reply #12 on: March 05, 2013, 10:24:55 AM »
Code: [Select]
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
; Function: ALE_ListCountItem - 31/08/2005
;
; Version 1.00
;
; Description:
;   returns the number of repetition of a item in a list
;
; Arguments:
;   TstItm = An atom or list
;   In_Lst = A list
;
; Return Values:
;   Integer or 0 if TstItm is not member of the list
;
; Examples:
;   (setq alist   '(0 1 2 3 4 3 5 3 6 3 3 7))
;
;   (ALE_ListCountItem 3 alist)
;   Returns: 5
;
;
(defun ALE_ListCountItem (TstItm In_Lst)
  (-
    (length In_Lst)
    (length (vl-remove TstItm In_Lst))
  )
)

Command: (ALE_ListCountItem (ascii "3") (vl-string->list "1231234567833"))
4

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: how can i count how many times an letter appears in a string
« Reply #13 on: March 06, 2013, 12:22:16 AM »
And another  :)

Code - Auto/Visual Lisp: [Select]
  1. (defun _count (letter string)
  2.   (length (vl-remove-if-not
  3.        (function (lambda (x) (= (strcase letter) (strcase (chr x)))))
  4.        (vl-string->list string)
  5.      )
  6.   )
  7. )
  8. ;; Not case sensitive
  9. (_count "T" "this is a test")
  10. 3
  11.  

Just a question: Why use the strcase inside the lambda? Might it not be more optimal to place one of them inside the vl-string->list? And perhaps even have a setq at the start to set the "letter" to uppercase? That way the lambda can be simplified a lot.
Code - Auto/Visual Lisp: [Select]
  1. (defun _count  (letter string)
  2.   (setq letter (ascii (strcase letter)))
  3.   (length (vl-remove-if-not (function (lambda (c) (= c letter)))
  4.             (vl-string->list (strcase string)))))
Of course this is then a mixed functional/imperative code. But IMO that's one of lisp's strengths: the ability to mix and match different coding idioms to make your code simpler and/or more efficient (both in execution and in actually writing it).
« Last Edit: March 06, 2013, 12:25:34 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

ronjonp

  • Needs a day job
  • Posts: 7531
Re: how can i count how many times an letter appears in a string
« Reply #14 on: March 06, 2013, 09:47:44 AM »
You are correct. Taking it out of the loop will help performance. That is the beauty of lisp  8-).

Better yet, remove the strcase completely and call it correctly. (_count "T" (strcase "this is a test"))  :-D

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC