Jump to content

Confirm Character is an Integer


Recommended Posts

Posted (edited)

In trying to validate some file names I need to read the 5th character in a string and confirm it's an integer [0-9]. I know how to grab the 5th character in the string but how does one confirm it's a number, not a dash, alpha-character, etc... ?

 

(setq fname "1234-R1.dwg")
(if (= (substr fname 5 1) "anything but a single digit integer")
   (do some stuff)
); end if

 

I guess I could make a list '(0123456789) and then iterate through it. If at the end one of the items in the list did not match the 5th digit...but would there be a cleaner method?

Edited by Bill Tillman
Posted

I'd look into :

 

(wcmatch chr "#")

 

-David

Posted

I just tried

 

(setq fname "1234.dwg")
(setq x (wcmatch (substr fname 5 1) #))

 

but I keep getting a weird error that seems to be left over from a previous undo operation. I think it's time to reboot this thing.

Posted

That should read:

(setq x (wcmatch (substr fname 5 1) [color=red]"[/color]#[color=red]"[/color]))

Also the 5th character in your example is the dot (".").

 

 

David, please don't miss the chr is a protected symbol.

Posted

MSasu...thank you sir. That's the ticket! :)

 

I'm trying to limit the program to only working with files which do not have an integer at the 5th character in the file name. Another one of these whacko projects they want done around here to keep the users from hosing it all up.

Posted

OK as usual right after I got the darned thing working they changed up the rules on me. Now they want any filename which contains any non-integer character within the first 5 characters to be filtered out. So I'm trying this but my brain is really fried and I keep getting "Too many arguments..."

 

(setq	count 0
match 0
fname "123-3.dwg"
 )

 (repeat 5
   (if	(wcmatch (substr fname count 1) "#")
     (setq match 0)
     (setq match 1)
   ); end if
 ); end repeat	

 

As I look at this I can see it won't really work like I need it. But I can't seem to find why it keeps returning too many arguments. It's late in the day here and I'm getting brain rot from coding all day.

 

UPDATE: OK, I found it...this darn computer has some old error trap in it and wow! Like I said it's been a long day. I need to set the second match to match1 or something else so it will not reset to zero on the next pass. And the too many arguments was from another typo..... I need a whiskey please!

Posted

Hey Bill,

 

Is this what you are looking for?

 

(while (not (wcmatch (Strcat (vl-filename-base (setq file (getfiled "Select a file" "" "*" ))(vl-filename-extension file)) "#####*")))

 

WC Matches

"#####*" First 5 chars are numbers

"@@@@#*" FIrst 4 chars are letters 5th char is a Number

"????#*" FIrst 4 chars are whatever 5th char is a Number

Posted

Well thanks and it might be...I need to check it out. However, I'm the weirdo on the block and in my programs there is absolutely, positively no user input allowed. I'm trying this now:

 

(setq count 1
       match 0
       match1 0
       fname "123-3.dwg")

(repeat 5
  (if
     (wcmatch (substr fname count 1) "#")
        (setq match 1)
        (setq match1 1)
  ); end if
); end repeat

 

I would think that each time through the loop only the first (setq match 1) would be set as long as the character is an integer. If it's not an integer then the second (setq match1 1) would be set. So if at anytime the loop finds a non-integer, match1 would be set to 1. At the end of it, I check the value of match1 and if it's set to 1, then my filter has worked. But when this runs, match ends up being 1, and match1 ends up being 0....which is not what I expected. The 4th time through the loop, it should have been set to 1 because the 4th character in the string fname is a dash. But I'm looking at that mask you created "#####*". I have not used wcmatch until today so I need to study that more. It may be what I'm looking for. They want any filename with the first 5 characters as integers to pass through the test and be saved in a different location than something with any non-integer character in the first 5 characters.

Posted

OK...it takes me a little longer with new commands:

 

(if (wcmatch fname "#####*")
   (setq match 1)
   ); end if

 

This looks like it's the real ticket I want.

Posted

Lol you never used wcmatch...... You are 1:1,000,000,000,000 and possibly a God among men......I dont think i could live without WCmatch........ But if you dont like the user input try

(setq filename "123-4R.DWG")
(if (wcmatch filename "###.#*")
        (setq match 1)
        (setq match1 1)
  )

 

With no * at the beginning # starts at the very first char. If the first char isnt a number will automatically will return Nil. Even if the rest of its true or there is another pattern that matches it later on.

# find single number

# find single number

# find single number

. find single nonalphanumeric character

# find single number

* may include any other characters of any type or quantity

 

 

filename "123-4R.DWG" returns T

filename "1123-4R.DWG" returns Nil reason the 4 char is no longer a nonalphanumeric character

filename "A23-4R.DWG" returns Nil reason starts with a letter not a number

Posted

Well I wouldn't go that far...but no I have never used wcmatch until today. I have seen it used and there may have been programs authored by others where I used it without knowing.

 

Now here's one I found recently, and I don't know it is available in anything prior to 2013:

 

tspaceinvaders

 

Check it out. I don't know of any use I'd have for it at the moment, but I can see that one day I might need this undocumented AutoCAD command. And thanks to everyone for the advice. It's working nicely, that is until all the users figure out a way to crash it. And they will.

Posted

Lee -mac has a nice pull numbers out of text that may be usefull

 

; change the 410 to layout name
;;-------------------=={ Parse Numbers }==--------------------;;
;;                                                            ;;
;;  Parses a list of numerical values from a supplied string. ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2011 - [url="http://www.lee-mac.com"]www.lee-mac.com[/url]       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  s - String to process                                     ;;
;;------------------------------------------------------------;;
;;  Returns:  List of numerical values found in string.       ;;
;;------------------------------------------------------------;;
(defun LM:ParseNumbers ( s )
 (
   (lambda ( l )
     (read
       (strcat "("
         (vl-list->string
           (mapcar
             (function
               (lambda ( a b c )
                 (if
                   (or
                     (< 47 b 58)
                     (and (= 45 b) (< 47 c 58) (not (< 47 a 58)))
                     (and (= 46 b) (< 47 a 58) (< 47 c 58))
                   )
                   b 32
                 )
               )
             )
             (cons nil l) l (append (cdr l) (list nil))
           )
         )
         ")"
       )
     )
   )
   (vl-string->list s)
 )
)

eg
(setq tabname "layout-52")
(setq dwgnum (Lm:parsenumbers tabname))

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...