Author Topic: Null vs empty?  (Read 11513 times)

0 Members and 1 Guest are viewing this topic.

CHulse

  • Swamp Rat
  • Posts: 504
Null vs empty?
« on: August 04, 2018, 07:55:01 PM »
Hi folks

In looping through a list of lists, I encounter the first empty row ("" "" "" "" "" "" "" "" "" "" "" "" ""). I'm having trouble testing for the first "" value to escape the program. I've tried

Code - Auto/Visual Lisp: [Select]
  1. (if (not (null (car x)))...

So is the "" value something other than "null"?

Thanks
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

CHulse

  • Swamp Rat
  • Posts: 504
Re: Null vs empty?
« Reply #1 on: August 04, 2018, 07:58:55 PM »
OK, I'm sorry. I have no idea why this posted 3 times.
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Null vs empty?
« Reply #2 on: August 04, 2018, 09:32:40 PM »
See if any of there help

Code - Auto/Visual Lisp: [Select]
  1. ;; (KDUB:emptystring-p "1") ->nil
  2. ;; (KDUB:emptystring-p "") ->T
  3. ;; (KDUB:emptystring-p " ") ->T
  4. ;;
  5. ;; (KDUB:notemptystring-p "x") -> T
  6. ;; (KDUB:notemptystring-p "") -> nil
  7. ;; (KDUB:notemptystring-p " ") -> nil
  8. ;;
  9. ;; (KDUB:validstring-p "x") -> T
  10. ;; (KDUB:validstring-p "") -> nil
  11. ;; (KDUB:validstring-p " ") -> T
  12. (defun kdub:string-p (arg) (= (type arg) 'str))
  13. (defun kdub:validstring-p (arg) (and (= (type arg) 'str) (/= 0 (strlen arg))))
  14. (defun kdub:emptystring-p (arg)
  15.   (and (= (type arg) 'str) (= 0 (strlen (vl-string-trim " " arg))))
  16. )
  17. (defun kdub:notemptystring-p (arg)
  18.   (and (= (type arg) 'str) (/= 0 (strlen (vl-string-trim " " arg))))
  19. )
  20.  
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Null vs empty?
« Reply #3 on: August 05, 2018, 05:49:18 AM »
To test whether the entire row is empty, you could use:

Code - Auto/Visual Lisp: [Select]
  1. (= "" (apply 'strcat x))

And yes, the empty string is not considered a null value, only nil is null.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Null vs empty?
« Reply #4 on: August 05, 2018, 06:37:30 AM »
Alternatively to Lee's suggestion:

Code - Auto/Visual Lisp: [Select]
  1. (not (apply '= (cons "" x)))
(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

CHulse

  • Swamp Rat
  • Posts: 504
Re: Null vs empty?
« Reply #5 on: August 05, 2018, 08:54:08 AM »
Thanks folks, I think I've got it working now. Much appreciated.
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Null vs empty?
« Reply #6 on: August 06, 2018, 11:26:45 AM »
Just a word of advice on this topic.
Given a list:
Code: [Select]
(setq x ("" "" "" "" "" "" "" "" "" "" "" "" ""))
Both Lee's and Grrr1337's suggestions will work.

However, if given a list:
Code: [Select]
(setq x ("" "" "" "" "" "" "" "" "" "" "" "" "" 1))
The first method will fail because strcat assumes a string.
-i.e.
Code - Auto/Visual Lisp: [Select]
  1. (= "" (apply 'strcat x)
  2. ; will fail if non-string entry is found.
  3.  
  4. (not (apply '= (cons "" x))))
  5. ; will still work in case of non-string entry
  6.  

In other programming languages you will spend quite a bit of time developing type safe routines and procedures. I would approach this much the same way kdub did and his type checking.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Null vs empty?
« Reply #7 on: August 06, 2018, 01:29:02 PM »
I think it ultimately depends on the use case - in this particular example, I would hazard a guess that the list has been obtained from reading a delimited file, in which case it is guaranteed that the list will only contain strings and any type checking would therefore be superfluous.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Null vs empty?
« Reply #8 on: August 06, 2018, 01:50:26 PM »
Okay, but my point is that you *should not* do that (assume) because it's more of a matter of time before you get burned and a "stringp" error, being caused in a far-flung section of code will/is not fun to locate; it's far easier and better for everyone if people/you/me/them/Mickey Mouse adds simple "type safety" check into functions like this.

Perhaps I wasn't clear on my second point of my post as well so here is further explanation.
Grrr1337's example code doesn't toss a wobbly when passed the non-string value but I would still--if I were typing--use a (eq (type... function just for extra readability because it works by obscurity (it's not clear that it will pass the type check) to most people.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CHulse

  • Swamp Rat
  • Posts: 504
Re: Null vs empty?
« Reply #9 on: August 07, 2018, 07:01:35 AM »
Thanks guys. I'm still learning.
But in this case, Lee is correct in assuming since he wrote most of the original routine I use. I've added to it over the years, but it relies on his string break function. So I was looking for a way to get the main routine to see the "end" of that list (i.e. The first empty row of strings). I've been working to make it more stupid proof and less reliant on the user.
So thanks again Lee, the tree block insertion routine has been a huge help and we use it almost daily.
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Null vs empty?
« Reply #10 on: August 07, 2018, 08:37:44 AM »
That sounds like a wonderful way to get out of many of my engineering tasks; I don't need to do any of these calculations because I will just assume it will never happen or always remain the way it is now. Maybe we should design roads that way too; we can post big signs saying no more then ten cars per hour allowed (and no trucks...or livestock). Brilliant!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Null vs empty?
« Reply #11 on: August 07, 2018, 10:16:10 AM »
Outside of libraries it's uncommon to have an unbounded "problem space".  One of the key parts to planning software, either top-level or in the weeds, is to create that limited and well defined "problem space" so you don't go cross-eyed trying to handle every possible eventuality.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Null vs empty?
« Reply #12 on: August 07, 2018, 10:21:07 AM »
Outside of libraries it's uncommon to have an unbounded "problem space".  One of the key parts to planning software, either top-level or in the weeds, is to create that limited and well defined "problem space" so you don't go cross-eyed trying to handle every possible eventuality.
Agreed .. if you know what data you're passing ( and you should ) why add overhead to check the data.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Null vs empty?
« Reply #13 on: August 07, 2018, 11:48:07 AM »
*blink-blink* Whaaat?

We are talking overhead measured in milliseconds if you chose to be type-safe (and, honestly, I'd bet that Grr1337's snip is faster to begin with because I think CONS is quicker then STRCAT). Even if it wasn't faster/more overhead, I'd still choose to adopt the method employed by Grrr1337's snip to take on type-safe vs not at all. ...Being redundant in a type-safe check is a little overboard (to add an "eq(type..." to that snip, for example)--I'll give you that--but I've never heard advice about when it is okay to NOT be type-safe.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

owenwengerd

  • Bull Frog
  • Posts: 451
Re: Null vs empty?
« Reply #14 on: August 07, 2018, 01:10:58 PM »
... I've never heard advice about when it is okay to NOT be type-safe.

To the extent that type safety hides violations of an API contract (i.e. when it's best to crash definitely and immediately), I think it is a very bad idea. And in my opinion, it's a very bad idea in this case, where non-strings mean that something is already wrong.