Re: parsing measurements
Alan Baljeu <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
My solution: parse_measure(Atom, Number, Unit) :- atom_codes(Atom, Codes) , append(NumberCodes, [U|NitCodes], Codes) , \+ member(U, "1234567890-.") , ! , catch(number_codes(Number, NumberCodes), _, fail) , atom_codes(UnitText, [U|NitCodes]) , catch( atom_to_term(UnitText, Unit, _), _, fail). It's rough, but for processing a simple field it seems to do the trick. Alan Baljeu P.S. This is a rather strange error message: 161 ?- number_codes(E, "6-2.6"). ERROR: number_chars/2: Syntax error: no_error ----- Original Message ----- From: Alan Baljeu <[email protected]> To: SWI Prolog <[email protected]> Cc: Sent: Thursday, July 18, 2013 11:25:52 AM Subject: [SWIPL] parsing measurements What's the easiest way to code parse_measure? ?- parse_measure('3.4mm/kg', N, U), N == 3.4, U == (mm/kg). It seems like a trivial problem given built-in stuff to parse numbers, and stuff to convert text into terms, but I can't find anything that will help identifying and pulling out the number, neither a scanf equivalent nor something that splits an atom over certain text. Alan Baljeu ------------ For further info, please visit http://www.swi-prolog.org/ To unsubscribe, send a plaintext mail with "unsubscribe prolog <e-mail>" in its body to [email protected] ------------ For further info, please visit http://www.swi-prolog.org/ To unsubscribe, send a plaintext mail with "unsubscribe prolog <e-mail>" in its body to [email protected]