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

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
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
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • 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.5
3 1/2     -> 3.5
3 1/2"    -> 3.5
3 1/2'    -> 42.0
3-1/2'    -> 42.0
3'-1"     -> 37.0
3' -1"    -> nil
3'- 1"    -> nil
3'-1      -> 37.0
3' 1      -> 37.0
3' 1"     -> 37.0
3' 1 1/2  -> 37.5
3'-1-1/2  -> 37.5
3' 1 1/2" -> 37.5
3' 1.5    -> 37.5
3' .5     -> 36.5
3'.5      -> 36.5
10-1      -> nil
-10       -> -10.0
-10-1     -> nil
.5        -> 0.5
0.5       -> 0.5
5 -2      -> nil
1/2       -> 0.5
1/2"      -> 0.5
9 1/2     -> 9.5
1.55E+01  -> 15.5
5A        -> nil
5 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 Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
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
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • 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 Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

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: 17750
  • 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 Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
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
Please support this web site.

Didge

  • Bull Frog
  • Posts: 211
Re: Testing a string for a number
« Reply #22 on: April 26, 2006, 12:25:19 PM »
Many thanks for the links guys, consider that function well and truely in the bag :-)
Think Slow......