parse and empty strings

Started by cormullion, March 10, 2006, 12:30:18 AM

Previous topic - Next topic

cormullion

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?

Lutz

#1
'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

cormullion

#2
I'd welcome the extra parameter! Look forward to possibly seeing it one day...



Thanks

Lutz

#3
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

cormullion

#4
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?

Lutz

#5
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