anixon Posted February 6, 2009 Share Posted February 6, 2009 How do you extract the position within a string of the first appearance of a numeric with any number between 0-9. I was thinking in terms of writing a loop which extracted the first character in the string and tested to see if it was a numeric then the second and so on until it encountered a numeric or is there a simple string function? Some string examples and the position of the first numeric within the string: '1acde ffga 12zmo r' the result would be 1 'abcdef 012 zuf9gas' the result would be 8 'ab7cdef 973 zufsgas' the result would be 3 'abcdefgh ijkl mnop 7' the result would be 20 Assistance is always appreciated Ant.. Link to comment Share on other sites More sharing options...
jerem488 Posted February 6, 2009 Share Posted February 6, 2009 (edited) How do you extract the position within a string of the first appearance of a numeric with any number between 0-9. I was thinking in terms of writing a loop which extracted the first character in the string and tested to see if it was a numeric then the second and so on until it encountered a numeric or is there a simple string function? Some string examples and the position of the first numeric within the string: '1acde ffga 12zmo r' the result would be 1 'abcdef 012 zuf9gas' the result would be 8 'ab7cdef 973 zufsgas' the result would be 3 'abcdefgh ijkl mnop 7' the result would be 20 Assistance is always appreciated Ant.. With the StringRegExp() function. or StringInStr() An example with StringInStr() function : $String = "fndjsk123" For $i = 0 To 9 $Position = StringInStr($String,$i) If $Find <> 0 Then MsgBox(0,"",$Position) Exit EndIf Next Edited February 6, 2009 by jerem488 Qui ose gagneWho Dares Win[left]CyberExploit[/left] Link to comment Share on other sites More sharing options...
anixon Posted February 6, 2009 Author Share Posted February 6, 2009 With the StringRegExp() function. or StringInStr() An example with StringInStr() function : $String = "fndjsk123" For $i = 0 To 9 $Position = StringInStr($String,$i) If $Find <> 0 Then MsgBox(0,"",$Position) Exit EndIf Next Thanks for that given that the string can vari in length I have modified your example as follows CODE$String = "fndjskaaaa0123" For $i = 0 To stringlen($string) $Position = StringInStr($String,$i) If $position <> 0 Then MsgBox(0,"",$Position) Exit EndIf Next Link to comment Share on other sites More sharing options...
BrettF Posted February 6, 2009 Share Posted February 6, 2009 (edited) No... He goes from 0 to 9 because they are the only digits to check... StringInStr returns the position of the chosen string or 0. You DO NOT need to do it like you are now. Read his example again and try understand exactly whats happening. Cheers, Brett EDIT: But, the example might have flaws (I haven't tested it) as it might check for 0, yet 0 is the 2nd digit etc. Let me whip something different up EDIT: This works correctly for me $string = "abcdefg1f0d93045" MsgBox(0, "", _CheckStringFirstDigit($string)) Func _CheckStringFirstDigit($string) $split = StringSplit($string, "") For $i = 1 To $split[0] If StringRegExp($split[$i], "\d") Then ExitLoop Next Return $i EndFunc ;==>_CheckStringFirstDigit Edited February 6, 2009 by BrettF Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Spiff59 Posted February 6, 2009 Share Posted February 6, 2009 If you do care about speed... you ought to drop the "<> 0" as demonstrated here: $String = "fndjs4gffgdfb66" $time = TimerInit() For $z = 1 to 10000 For $i = 0 To 9 $Position = StringInStr($String,$i) If $Position <> 0 Then ; MsgBox(0,"","Position: " & $Position) ExitLoop EndIf Next Next Msgbox(1,"", int(TimerDiff($time))) $time = TimerInit() For $z = 1 to 10000 For $i = 0 To 9 $Position = StringInStr($String,$i) If $Position Then ; MsgBox(0,"","Position: " & $Position) ExitLoop EndIf Next Next Msgbox(1,"", int(TimerDiff($time))) Link to comment Share on other sites More sharing options...
BrettF Posted February 6, 2009 Share Posted February 6, 2009 Whats the point of the outer $z loop? Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Authenticity Posted February 6, 2009 Share Posted February 6, 2009 I believe it's for statistical results which are more relevant, well, for statistical purpose... Anyway why don't you just use StringRegExp: Dim $Str = 'opijoij3t_0ek *_$#^_dkoddokd' Dim $Arr = StringRegExp($Str, '\d', 1) MsgBox(0x40, 'Title', 'On position: ' & @extended-1) Link to comment Share on other sites More sharing options...
Spiff59 Posted February 6, 2009 Share Posted February 6, 2009 I just put jerem488's example in a 10000-iteration loop with a timer to demonstrate that although it works fine as is, it works even better without the "<> 0". Just trying to encourage efficient coding. Link to comment Share on other sites More sharing options...
BrettF Posted February 6, 2009 Share Posted February 6, 2009 I see Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Spiff59 Posted February 6, 2009 Share Posted February 6, 2009 Ouch. $String = "0fndjs4gffgdfb66" $time = TimerInit() For $z = 1 to 10000 For $i = 0 To 9 $Position = StringInStr($String,$i) If $Position Then ExitLoop Next Next Msgbox(1,"", int(TimerDiff($time))) $time = TimerInit() For $z = 1 to 10000 $split = StringSplit($string, "") For $i = 1 To $split[0] If StringRegExp($split[$i], "\d") Then ExitLoop Next Next Msgbox(1,"", int(TimerDiff($time))) Link to comment Share on other sites More sharing options...
Spiff59 Posted February 6, 2009 Share Posted February 6, 2009 (edited) That one's FAST, Auth! Do that one OP! Edit: babbling... (and here I generally considered StringRegExp a dog, I guess I seem to relate it to Grep and EGrep) Edited February 6, 2009 by Spiff59 Link to comment Share on other sites More sharing options...
Authenticity Posted February 6, 2009 Share Posted February 6, 2009 $String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7aaaaaaaa" Dim $Dummy, $Position $time = TimerInit() For $z = 1 to 1000 For $i = 0 To 9 $Position = StringInStr($String,$i) If $Position Then ExitLoop Next Next Msgbox(0x40,'StringInStr', int(TimerDiff($time))) $time = TimerInit() For $z = 1 to 1000 $Dummy = StringRegExp($String, '\d', 1) $Position = @extended-1 Next Don't add unneeded code lol, now ouch!... XP Link to comment Share on other sites More sharing options...
BrettF Posted February 6, 2009 Share Posted February 6, 2009 Ouch.Yes it may be faster but it returns the wrong result: See the following: $String = "fndjs4g0ffgdfb66" $time = TimerInit() For $z = 1 to 10000 For $i = 0 To 9 $Position = StringInStr($String,$i) If $Position Then ExitLoop Next Next Msgbox(0,$Position, int(TimerDiff($time))) $time = TimerInit() For $z = 1 to 10000 $split = StringSplit($string, "") For $i = 1 To $split[0] If StringRegExp($split[$i], "\d") Then ExitLoop Next Next Msgbox(0,$i, int(TimerDiff($time))) Just go with what Auth posted. It confused me because there is no mention of @extended return the position... Cheers, Brett Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Spiff59 Posted February 6, 2009 Share Posted February 6, 2009 Yes, I concede! I'll stop going "ewww" everytime I see SRE. I'll quit letting it remind me of UNIX scripting 20 years ago, the details of which I've long forgotten, but for which the unpleasant aftertaste still lingers Link to comment Share on other sites More sharing options...
BrettF Posted February 7, 2009 Share Posted February 7, 2009 (edited) Hur Hur Hur! EDIT: 6000th post... Quality right there... Edited February 7, 2009 by BrettF Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Aceguy Posted February 7, 2009 Share Posted February 7, 2009 (edited) how about $String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7aaaaaaaa" Dim $Dummy, $Position $time = TimerInit() $ss=StringSplit($String,"") For $i = 1 To $ss[0] if StringIsDigit($ss[$i])=1 then ExitLoop Next Msgbox(0,$i,"Done in "& int(TimerDiff($time))&" miliseconds and it found the number "&$ss[$i]) Edited February 7, 2009 by Aceguy [u]My Projects.[/u]Launcher - not just for games & Apps (Mp3's & Network Files)Mp3 File RenamerMy File Backup UtilityFFXI - Realtime to Vana time Clock Link to comment Share on other sites More sharing options...
Authenticity Posted February 7, 2009 Share Posted February 7, 2009 (edited) $String = "1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa7aaaaaaaa" Dim $Dummy, $Position $time = TimerInit() For $z = 0 To 500 $ss=StringSplit($String,"") For $i = 1 To $ss[0] if StringIsDigit($ss[$i]) then ExitLoop Next Next Msgbox(0,$i,"Done in "& int(TimerDiff($time))&" miliseconds and it found the number "&$ss[$i]) $time = TimerInit() For $z = 0 To 500 $Dummy = StringRegExp($String, '\d') $Position = $Dummy Next Msgbox(0,$i,"Done in "& int(TimerDiff($time))&" miliseconds and it found the number "&$ss[$i]) I added 1 in front of the string, still, the results are crystal clear. (=P ...@extended-1 instead of $Dummy. Edited February 7, 2009 by Authenticity Link to comment Share on other sites More sharing options...
Aceguy Posted February 7, 2009 Share Posted February 7, 2009 @ authenticity i wasnt trying to prove a point, just showing a different way..... [u]My Projects.[/u]Launcher - not just for games & Apps (Mp3's & Network Files)Mp3 File RenamerMy File Backup UtilityFFXI - Realtime to Vana time Clock Link to comment Share on other sites More sharing options...
anixon Posted February 7, 2009 Author Share Posted February 7, 2009 Hi guys thanks for the spirited response I really appreciated the effort from those concerned. On reading the installments it just confirms what I have anyways thought and that is there is more than one way of coding a solution. Based on those presented I have coded my requirement as follows. ;//Extract the Phone Number $sString = "Most Urgent 61390890965" StringRegExp($sString, '\d', 1) ;\\finds position of first numeric occurance $sPosition = @extended - 1 ;calculate the @mid start position $sPhoneNum = StringMid($sString, $sPosition, StringLen($sString) - ($sPosition - 1)) Msgbox(0,"",$sPhoneNum,5) Thanks again Ant.. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 7, 2009 Moderators Share Posted February 7, 2009 Hi guys thanks for the spirited response I really appreciated the effort from those concerned. On reading the installments it just confirms what I have anyways thought and that is there is more than one way of coding a solution. Based on those presented I have coded my requirement as follows. ;//Extract the Phone Number $sString = "Most Urgent 61390890965" StringRegExp($sString, '\d', 1) ;\\finds position of first numeric occurance $sPosition = @extended - 1 ;calculate the @mid start position $sPhoneNum = StringMid($sString, $sPosition, StringLen($sString) - ($sPosition - 1)) Msgbox(0,"",$sPhoneNum,5) Thanks again Ant..Hmm... Local $a_phone = StringRegExp($s_string, "\d+", 1) If Not @error Then MsgBox(64, "Number", $a_phone[0]) Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now