Author Topic: null string in list  (Read 5302 times)

0 Members and 1 Guest are viewing this topic.

csgoh

  • Newt
  • Posts: 174
null string in list
« on: August 03, 2015, 06:20:57 AM »
I have say a list ("" "" "" "" "").
How to check if all the items in a list is null string.
thank you.


roy_043

  • Water Moccasin
  • Posts: 1799
  • BricsCAD 18
Re: null string in list
« Reply #1 on: August 03, 2015, 06:42:01 AM »
Here is one way:
Code - Auto/Visual Lisp: [Select]
  1. (setq lst '("" "" "" ""))
  2. (not (vl-remove "" lst)) => T

AIberto

  • Newt
  • Posts: 127
Re: null string in list
« Reply #2 on: August 03, 2015, 07:23:52 AM »
csgoh

if list ("" "" "" "" "") you want returns T ,
list  ("" "1" "" "" "") you want returns T or nil ?
AutoCad2012 ,Windows XP 32bit

ronjonp

  • Needs a day job
  • Posts: 7148
Re: null string in list
« Reply #3 on: August 03, 2015, 09:21:13 AM »
Here's some more for fun.  :)
Code - Auto/Visual Lisp: [Select]
  1. (setq l '("" "" "" ""))
  2.  
  3.  
  4. (vl-every '(lambda (x) (and (= (type x) 'str) (= x ""))) l)
  5.  
  6.  
  7.  
  8.  
  9. (= "" (apply 'strcat l))
  10.  
  11.  
  12. (zerop (apply '+ (mapcar 'strlen l)))

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

ribarm

  • Water Moccasin
  • Posts: 2367
  • Marko Ribar, architect
Re: null string in list
« Reply #4 on: August 03, 2015, 09:30:42 AM »
Based on Roy's example - for more general purposes :

Code - Auto/Visual Lisp: [Select]
  1. ;; (eqellst '(10.0 10.0 10.0) 10.0 1e-6) => T
  2. ;; (eqellst '(10.0 10.0 10.0) 1.0 1e-6) => nil
  3. ;; (eqellst '(10.0 10.0 1.0) 10.0 1e-6) => nil
  4.  
  5. (defun eqellst ( l el f )
  6.   (while
  7.     (vl-some
  8.       (function
  9.         (lambda ( x )
  10.           (if (equal el x f)
  11.             (setq l
  12.               (vl-remove-if
  13.                 (function
  14.                   (lambda ( x )
  15.                     (equal el x f)
  16.                   )
  17.                 )
  18.                 l
  19.               )
  20.             )
  21.           )
  22.         )
  23.       )
  24.       l
  25.     )
  26.   )
  27.   (zerop (length l))
  28. )
  29.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1799
  • BricsCAD 18
Re: null string in list
« Reply #5 on: August 03, 2015, 10:12:40 AM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (vl-every '= (cons "" lst) lst)
« Last Edit: August 03, 2015, 10:53:43 AM by roy_043 »

Tharwat

  • Swamp Rat
  • Posts: 526
  • Hypersensitive
Re: null string in list
« Reply #6 on: August 03, 2015, 12:32:34 PM »
Contrary way  :-D

Code - Auto/Visual Lisp: [Select]
  1. (setq l '("" "" "" ""))
  2. (not (vl-some '(lambda (u) (/= u "")) l))
  3.  

Lee Mac

  • Seagull
  • Posts: 12390
  • London, England
Re: null string in list
« Reply #7 on: August 03, 2015, 12:50:25 PM »
Another, just for variation:
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l ) (or (not l) (and (= "" (car l)) (f (cdr l)))))
  2. _$ (f '("" ""))
  3. T

Lee Mac

  • Seagull
  • Posts: 12390
  • London, England
Re: null string in list
« Reply #8 on: August 03, 2015, 12:56:13 PM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (vl-every '= (cons "" lst) lst)

Very good roy.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16786
  • Superior Stupidity at its best
Re: null string in list
« Reply #9 on: August 03, 2015, 01:08:14 PM »
Wouldn't this work, just to determine if it is in the list?

Code: [Select]
(if (member "" lst) T)

NOTE: I am not where I can test it
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Tharwat

  • Swamp Rat
  • Posts: 526
  • Hypersensitive
Re: null string in list
« Reply #10 on: August 03, 2015, 01:13:28 PM »
Wouldn't this work, just to determine if it is in the list?

Code: [Select]
(if (member "" lst) T)

NOTE: I am not where I can test it

That checks for one item and the OP wants to check all the items in a list  :wink:

JAY

  • Newt
  • Posts: 105
Re: null string in list
« Reply #11 on: August 03, 2015, 01:21:46 PM »
Another, just for variation:
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l ) (or (not l) (and (= "" (car l)) (f (cdr l)))))
  2. _$ (f '("" ""))
  3. T

Lee ,can make it like this ?
'("" "" "") returns nil
'("1" "" "") returns nil
'("1" "10" "") returns nil
……
……
Only '("1" "s1"  "a") returns T

Lee Mac

  • Seagull
  • Posts: 12390
  • London, England
Re: null string in list
« Reply #12 on: August 03, 2015, 01:55:48 PM »
Lee ,can make it like this ?
'("" "" "") returns nil
'("1" "" "") returns nil
'("1" "10" "") returns nil
……
……
Only '("1" "s1"  "a") returns T

Code - Auto/Visual Lisp: [Select]
  1. _$ (defun f ( x ) (equal x '("1" "s1" "a")))
  2. _$ (f '("1" "s1" "a"))
  3. T

roy_043

  • Water Moccasin
  • Posts: 1799
  • BricsCAD 18
Re: null string in list
« Reply #13 on: August 03, 2015, 04:56:54 PM »
Another:
Code - Auto/Visual Lisp: [Select]
  1. (vl-every '= (cons "" lst) lst)

Very good roy.
Thanks.

@ ANJALI: If you want to check for the absence of empty strings you should try Keith's contribution. His code checks for the opposite, but that can easily be fixed...
« Last Edit: August 03, 2015, 05:00:09 PM by roy_043 »

bruno_vdh

  • Guest
Re: null string in list
« Reply #14 on: August 03, 2015, 05:22:18 PM »
Hello,
Quote
I have say a list ("" "" "" "" "").
My variant
Code: [Select]
(apply '= (cons "" lst))

Regards,
« Last Edit: August 03, 2015, 05:44:05 PM by bruno_vdh »