parse and empty strings
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
parse and empty strings
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?
'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
'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
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
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:
so an additional parameter for 'parse' would not be required.
Lutz
Code: Select all
> (parse "1,2,, 3,4" ",| " 0)
("1" "2" "" "" "3" "4")
> (parse "1,2,, 3,4" "(,| )+" 0)
("1" "2" "3" "4")
>
Lutz
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
I'm not always using a regex parse. For example:
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?
Code: Select all
(set 'l "/Users/me/documents/fred.txt")
(parse l "/")
;-> ("" "Users" "me" "documents" "fred.txt")