pilky1986 Posted September 8, 2010 Share Posted September 8, 2010 I need to return a value depending on the number of the following character | that are found in a string. In my program i use the character | to sepearate the string into an array. However i need to distinguish between the number of | in each example. There is only two possibilites.5 lots of |or7 lots of |The reason why i need to distinguish between the number of | characters is so i can select the right field in the array. The data i am loooking to collect would be either 0230377X or 0230346Q in these examples. Therefore it could be either in the array [5] or [3]. The problem is that the format of the data in that field could change in the furture it may not always be 7numbers and then a letter. So would rule out me searching each array and using the stringregexpression to find the data that matches that pattern. The only constant is the total number of fields and therefore the number of | characters. Here are two "live" examplesNO|NO|DVTC|Alex Test|0230377X|46|sWAR,eGFR,L,sANA,E|Q3|Jesus Christ|0230346Q|49|E|The data between each character "|" can be letters, numbers, characters, white space, any case.. basically anything but the character | itselfI was looking at the stringregexp to try and resolve this however i noticed that the character that i am using ( | ) is used as an "OR". Therefore i am not sure if i can still use this character.Could someone please point me in the right direction. Thanks! Link to comment Share on other sites More sharing options...
cageman Posted September 8, 2010 Share Posted September 8, 2010 i am not a pro at regex, but i know you can escape special characters using a backslash. I might be able to assist you a little further if you still can't solve it. Try using http://rubular.com/ I got that link from someone here and it makes solving your problem much faster. Good luck. Link to comment Share on other sites More sharing options...
wakillon Posted September 8, 2010 Share Posted September 8, 2010 I need to return a value depending on the number of the following character | that are found in a string... try : $text = StringReplace ( $_YourString, "|", "|") $numreplacements = @extended MsgBox(0, "The number of replacements done was", $numreplacements) AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
pilky1986 Posted September 8, 2010 Author Share Posted September 8, 2010 [Cageman] Thanky you for your quick response. Although it did not resolve the problem that is a very useful website and will come in handy! So a big thanks goes out to you! [Wakillon] That is a very simple way of doing it.. i had not even thought of using stringreplace! It works a treat, thank you very much! Cheers guys! *************** PROBLEM SOLVED *************** Link to comment Share on other sites More sharing options...
wakillon Posted September 8, 2010 Share Posted September 8, 2010 The simplest solutions are often the best ! AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
kaotkbliss Posted September 8, 2010 Share Posted September 8, 2010 I would have suggested $var=StringSplit ( $string, "|" ) This would 1. put each value between | into an array and 2. $var[0] would give you a total 010101000110100001101001011100110010000001101001011100110010000 001101101011110010010000001110011011010010110011100100001 My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek We're gonna need another Timmy! Link to comment Share on other sites More sharing options...
Varian Posted September 8, 2010 Share Posted September 8, 2010 Wouldn't the [0] index in the Stringsplit array (that you already use) return 7 or 5? Or at least doing a uBound on the existing array depending on your StringSplit flag would result in the same. Link to comment Share on other sites More sharing options...
pilky1986 Posted September 8, 2010 Author Share Posted September 8, 2010 [ kaotkbliss & Varian ] Yes you are both correct however at this point my array is not split it is as the above data shows. I was looking for a method which didnt require using stringsplit and placing it in an array. Thanks again everyone. Hope this can be of use for others in the future. Regards Pilky Link to comment Share on other sites More sharing options...
kaotkbliss Posted September 8, 2010 Share Posted September 8, 2010 Wouldn't the [0] index in the Stringsplit array (that you already use) return 7 or 5? Exactly. From what I'd gathered from the question, the OP wanted to know how many times the | appeared in the string. and $var[0] would give him just that and if he only needed one value between | then he could do an if/then like If Int($var[0])=7 Then $value=$var[5] Elseif Int($var[0])=5 Then $value=$var[3] Endif It's just an easy way I would tackle the problem 010101000110100001101001011100110010000001101001011100110010000 001101101011110010010000001110011011010010110011100100001 My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek We're gonna need another Timmy! Link to comment Share on other sites More sharing options...
Varian Posted September 8, 2010 Share Posted September 8, 2010 OK..I thought your Array was created was created from the Stringsplit function. I see that one Element in your array is actually: NO|NO|DVTC|Alex Test|0230377X|46|sWAR,eGFR,L,sANA,E| and another is: Q3|Jesus Christ|0230346Q|49|E| For Fun!Local $Array[2] = ["NO|NO|DVTC|Alex Test|0230377X|46|sWAR,eGFR,L,sANA,E|", "Q3|Jesus Christ|0230346Q|49|E|"] For $i = 0 To UBound($Array) - 1 Local $String Switch UBound(StringRegExp($Array[$i], '\|', 4)) Case 5 $String = StringRegExpReplace($Array[$i], '(?m)^((.*?\|)){2}([^\|]*)\|.+$', '$3') Case 7 $String = StringRegExpReplace($Array[$i], '(?m)^((.*?\|)){4}([^\|]*)\|.+$', '$3') EndSwitch MsgBox(32, '', 'Found String for: ' & @LF & $Array[$i] & @LF & @LF & 'is: ' & $String) Next 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