parse and empty strings

For the Compleat Fan
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

parse and empty strings

Post 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?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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

Thanks

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Locked