### Author Topic: Testing a string for a number  (Read 4195 times)

0 Members and 1 Guest are viewing this topic.

#### CAB

• Global Moderator
• Seagull
• Posts: 10388
##### Re: Testing a string for a number
« Reply #15 on: April 24, 2006, 02:38:02 PM »
PS This routine came from a thread, maybe in the old swamp, not sure.
And I think Stig was the author.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970

#### MP

• Seagull
• Posts: 17686
• Have thousands of dwgs to process? Contact me.
##### Re: Testing a string for a number
« Reply #16 on: April 24, 2006, 09:32:07 PM »
Thanks Alan, sure wish Stig posted more often.

Here's the output from a quickly bashed out pile of code (sorry, it's pretty ugly) --

Code: [Select]
`3.5       -> 3.53 1/2     -> 3.53 1/2"    -> 3.53 1/2'    -> 42.03-1/2'    -> 42.03'-1"     -> 37.03' -1"    -> nil3'- 1"    -> nil3'-1      -> 37.03' 1      -> 37.03' 1"     -> 37.03' 1 1/2  -> 37.53'-1-1/2  -> 37.53' 1 1/2" -> 37.53' 1.5    -> 37.53' .5     -> 36.53'.5      -> 36.510-1      -> nil-10       -> -10.0-10-1     -> nil.5        -> 0.50.5       -> 0.55 -2      -> nil1/2       -> 0.51/2"      -> 0.59 1/2     -> 9.51.55E+01  -> 15.55A        -> nil5 A       -> nil`
Code: [Select]
`(   (lambda ( strings / maxlen )        (setq maxlen             (apply 'max                 (mapcar 'strlen strings)            )        )        (mapcar           '(lambda ( string / padding )                (setq padding " ")                                (princ                    (strcat                        (substr                            (progn                                (while                                     (<                                        (strlen                                             (setq padding                                                 (strcat                                                     padding                                                     padding                                                )                                            )                                        )                                        maxlen                                    )                                )                                (strcat string padding)                            )                            1                            maxlen                        )                        " -> "                        (   (lambda ( x / result )                                (vl-catch-all-apply                                   '(lambda ( )                                        (vl-some                                           '(lambda ( units )                                                 (setq result                                                     (distof x units)                                                )                                            )                                           '(4 5 2 1)                                        )                                    )                                )                                (vl-prin1-to-string result)                            )                            string                        )                        "\n"                    )                )                )            strings        )        (princ)    )   '(        "3.5"        "3 1/2"        "3 1/2\""        "3 1/2'"        "3-1/2'"        "3'-1\""        "3' -1\""        "3'- 1\""        "3'-1"        "3' 1"        "3' 1\""        "3' 1 1/2"        "3'-1-1/2"        "3' 1 1/2\""        "3' 1.5"        "3' .5"        "3'.5"        "10-1"        "-10"        "-10-1"        ".5"        "0.5"        "5 -2"        "1/2"        "1/2\""        "9 1/2"        "1.55E+01"        "5A"        "5 A"    ))`
Engineering Technologist • CAD Specialist • Programmer Analyst
Design • Drafting • Document Control • Automation.

#### CAB

• Global Moderator
• Seagull
• Posts: 10388
##### Re: Testing a string for a number
« Reply #17 on: April 25, 2006, 12:34:13 AM »
Michael,
Your command of the language is astounding.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970

#### MP

• Seagull
• Posts: 17686
• Have thousands of dwgs to process? Contact me.
##### Re: Testing a string for a number
« Reply #18 on: April 25, 2006, 01:15:57 AM »
Thanks Alan, you are way too generous. I should have written it properly (i.e. read-able by others) but had about 5 minutes to knock it off. Normally I'd break a problem down to its constituent functions but I was real pressed for time; ergo the mess I posted. Don't have any more tine tonight either -- I'm struggling to post this as a sleeping pill slams my synapses; both of them.
Engineering Technologist • CAD Specialist • Programmer Analyst
Design • Drafting • Document Control • Automation.

#### Didge

• Bull Frog
• Posts: 211
##### Re: Testing a string for a number
« Reply #19 on: April 26, 2006, 06:10:41 AM »
A very interesting thread Guys, I've often required a fail-safe string to number function, but something meaty enough to extract numbers from the following types of strings without falling over.

"Cable 1.2m deep"  and  "Cable depth 1.2m"

To the user either string is much the same as the other, but ATOF tends to be a touch more fussy in it's requirements. I guess a character by character evaluation is called for.
Think Slow......

#### MP

• Seagull
• Posts: 17686
• Have thousands of dwgs to process? Contact me.
##### Re: Testing a string for a number
« Reply #20 on: April 26, 2006, 07:47:28 AM »
Might be some tips you can glean in this thread.
Engineering Technologist • CAD Specialist • Programmer Analyst
Design • Drafting • Document Control • Automation.

#### CAB

• Global Moderator
• Seagull
• Posts: 10388
##### Re: Testing a string for a number
« Reply #21 on: April 26, 2006, 11:22:07 AM »
Here is another thread of intrest http://www.theswamp.org/index.php?topic=3710.0
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970