Page 1 of 1
parse and empty strings
Posted: Fri Mar 10, 2006 8:30 am
by 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?
Posted: Fri Mar 10, 2006 11:46 am
by Lutz
'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
Posted: Fri Mar 10, 2006 7:21 pm
by cormullion
I'd welcome the extra parameter! Look forward to possibly seeing it one day...
Thanks
Posted: Sat Mar 11, 2006 9:15 am
by Lutz
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:
Code: Select all
> (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
Posted: Sat Mar 11, 2006 1:38 pm
by cormullion
I'm not always using a regex parse. For example:
Code: Select all
(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?
Posted: Sun Mar 12, 2006 8:10 am
by Lutz
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