newLISP Fan Club

Forum => Anything else we might add? => Topic started by: cormullion on March 10, 2006, 12:30:18 AM

Title: parse and empty strings
Post by: cormullion on March 10, 2006, 12:30:18 AM
While the parse function is great, I've noticed that it always seems to generate loads of empty strings. It would be cool if there was some type of switch that automatically eliminates them if you didn't want them. At present I'm doing a (filter (fn x) (!= x "")) on the results, which is OK, but seems like one more step. Or is there a better way?
Title:
Post by: Lutz on March 10, 2006, 03:46:29 AM
'parse' leaves empty strings for each empty space between separators. This is necessary for parsing CSV or other DB records etc. to maintain fixed positions in a record.



'filter' is a good way to get rid of, 'replace' in remove mode can also be used. A additional parameter to suppress ampy strings seems like a good idea and may be implemented in a future version.



Lutz
Title:
Post by: cormullion on March 10, 2006, 11:21:30 AM
I'd welcome the extra parameter! Look forward to possibly seeing it one day...



Thanks
Title:
Post by: Lutz on March 11, 2006, 01:15:20 AM
After thinking about this more, it seems to me that the case of empty strings in 'parse' can always be resolved using the repeat operator '+' in the regular expression for the break string:



> (parse "1,2,, 3,4" ",| " 0)
("1" "2" "" "" "3" "4")
> (parse "1,2,, 3,4" "(,| )+" 0)
("1" "2" "3" "4")
>


so an additional parameter for 'parse' would not be required.



Lutz
Title:
Post by: cormullion on March 11, 2006, 05:38:19 AM
I'm not always using a regex parse. For example:


(set 'l "/Users/me/documents/fred.txt")
(parse l "/")
;-> ("" "Users" "me" "documents" "fred.txt")


I was kind of under the impression that a regex parse was probably going to be slightly slower than a non-regex one, so I should avoid them if I don't need them?
Title:
Post by: Lutz on March 12, 2006, 12:10:37 AM
Yes, a non-regex parse is faster and preferred when speed is critical, so the method secribed to avoid empty strings would only be usable on regex parse.



Lutz