1.

Solve : VBS - Replace String where not 0 or 1?

Answer» <html><body><p>I have a value "00000000" without quotes where I am adding a 1 to any of the positions if a value has changed in an input file but in some cases I will end up with a value of "00130012" but what I would like my end result to be is only 0(unchanged) or 1(changed) in any position ... in other works if the value is greater then 1 I want to change it back to a 1. I'm trying to figure out if the easiest way to do this would be a replace function of some <a href="https://interviewquestions.tuteehub.com/tag/sort-238033" style="font-weight:bold;" target="_blank" title="Click to know more about SORT">SORT</a>? I'm thinking there would have to be something simpler then a doing a split and checking each value. Or instead of the above is there a way to check the position of a digit within the value to see if <a href="https://interviewquestions.tuteehub.com/tag/already-1974237" style="font-weight:bold;" target="_blank" title="Click to know more about ALREADY">ALREADY</a> 1, if it is don't add again. I <a href="https://interviewquestions.tuteehub.com/tag/could-410026" style="font-weight:bold;" target="_blank" title="Click to know more about COULD">COULD</a> do 8 counters for each position that if greater then 0 add 1 to the position but keeping track of a bunch of counters seems like a lot of work. why don't you show us your code so far? That would save one *censored* of a lot of time asking questions. Why don't you, instead of adding 1 to a position, check if the digit in that position is already changed to 1 and if it is, leave it alone?<br/><br/>Ended up using counters to track if I had already added a 1 to that position. Not sure how to go about checking if the position already has a 1 within a number. <br/><br/>Didn't post the code as it is quite messy and very much spaghetti code. Not really sure yet how to clean it up into tidy functions but at least it works and does what I want for now. I'll still posted if you <a href="https://interviewquestions.tuteehub.com/tag/care-kii-spnaa-kii-406856" style="font-weight:bold;" target="_blank" title="Click to know more about CARE">CARE</a> to see as I'm always interested in learning a cleaner meaner approach. Quote from: nothlit on October 29, 2010, 01:30:52 PM</p><blockquote>Ended up using counters to track if I had already added a 1 to that position. Not sure how to go about checking if the position already has a 1 within a number. <br/></blockquote> <br/>I am wondering... what are you going to do when you get to more than 9 changes for any particular position? Whne the number of changes is no longer a single digit?<br/><br/>But assuming you never reach that state, you could <br/><br/>create an empty string <br/>go through your string e.g. "00130012" character by character<br/>for each character of your string, if it is an 0 add a 0 to your new string otherwise add a 1 to your new string.<br/><br/>e.g. <br/> Code: <a>[Select]</a>MyString="00130012"<br/>NuString=""<br/>For i=1 To 8<br/>    If Mid(MyString,i,1)="0" Then<br/>       NuString=NuString &amp; "0"<br/>    Else<br/>       NuString=NuString &amp; "1"<br/>    End If<br/>Next<br/>REM NuString now reads 00110011<br/>But why don't you just have an array with 8 elements, set them all to 0 initially and then it's easy to check if an element is 0 and if it is, change it to 1, and if it isn't 0, leave it alone. At the end read out the elements into a string like above.<br/><br/><br/><br/><br/><br/>My messy code ... <br/> Code: <a>[Select]</a>Option Explicit<br/>'''''''''''''''''<br/>'''''''''''''''''This script creates flat file for ORC research.<br/>'''''''''''''''''<br/>Dim objOutputFile, s, objFS, objArgs, strInputFile, objFile, strOutputfile<br/>Dim objFSO, boolMatchCaseSensitive, objInputFile, intArgCount, s1<br/>Dim Count, dater, Branchn, a<br/>Dim StrDate, StrTime<br/><br/>Dim origBatch, origTrans, origOLD, origISN, origAUX, origRT<br/>Dim origwAUX, origACCT, origTC, origAMT<br/><br/>Dim storBatch, storTrans, storOLD, storISN, storAUX, storRT<br/>Dim storwAUX, storACCT, storTC, storAMT<br/><br/>Dim storAUXn, storRTn, storwAUXn, storACCTn, storTCn<br/><br/>Dim strOutputFilePath<br/><br/>Dim editFlds, stroutput<br/><br/>Call intialize()<br/><br/><br/>Set objFS=CreateObject("Scripting.FileSystemObject")<br/> Const intForReading = 1<br/> Const intForWriting = 2<br/> Const intForAppending = 8<br/><br/>Set objArgs = WScript.Arguments<br/>'strInputFile = objArgs(0)<br/>intArgCount = WSCript.Arguments.Count<br/> 'Check for at least first two arugments. First arugment is input, second is outputfile(DAT format)<br/> if intArgCount &lt; 2 then <br/> MsgBox ("Need input file as first argument")<br/> MsgBox ("Need ouput file as second argument")<br/> WScript.Quit(99)<br/> Else<br/> strInputFile = objArgs(0)<br/> 'strOutputfile = "???.???"<br/> strOutputFile = objArgs(1)<br/> End If<br/> 'Check if Inputfile (argument1) exist, if not then exit with Error<br/> If objFS.fileExists(strInputFile) Then <br/> ELSE<br/> MsgBox (strInputfile &amp; " does not exist")<br/> WScript.Quit(99)<br/> End If<br/>'MsgBox strOutputFile<br/>'Set objFile = objFS.OpenTextFile(strInputFile)<br/> If objFS.fileExists(strOutputfile) Then objFS.DeleteFile(StrOutputfile)<br/>strOutputFilePath = objFS.GetParentFolderName(strOutputfile)<br/> 'If Not exists(strOutputFilePath) then<br/> 'CreateFolder(strOutputFilePath)<br/> 'End If <br/>Set objInputFile = objFS.OpenTextFile(strInputFile, intForReading, False) 'Open Inputfile to read line by line (s variable in do loop)<br/>Set objOutputFile = objFS.OpenTextFile(strOutputFile,intForAppending, True) 'Open Outputfile and set append mode for writing to file<br/> <br/>Do While objInputFile.AtEndOfStream &lt;&gt; True<br/> s = objInputFile.ReadLine()<br/> <br/> If InStr (s,"Original") &gt; 0 Then <br/> If Count &lt;&gt; 0 Or editFlds &lt;&gt; 00000000 Then <br/> If Count = 0 Then<br/> objOutputFile.WriteLine origISN &amp; " " &amp; "01" &amp; " " &amp; editFlds<br/> Else <br/> objOutputFile.WriteLine stroutput &amp; " " &amp; editFlds<br/> End If <br/> End If <br/> 'stroutput = ""<br/> 'Count = 0<br/> 'editFlds = 00000000<br/> Call intialize()<br/><br/> 'origBatch = Trim(Mid(s,1,7))<br/> 'origBatch = Replace(origBatch, " ", "")<br/> 'origBatch = Replace(origBatch, " ", "")<br/> 'origTrans = Trim(Mid(s,7,8))<br/> 'origTrans = Replace(origTrans, " ", "")<br/> 'origTrans = Replace(origTrans, " ", "")<br/> 'origOLD   = Trim(Mid(s,15,9))<br/> 'origOLD = Replace(origOLD, " ", "")<br/> 'origOLD = Replace(origOLD, " ", "")<br/> origISN   = Trim(Mid(s,25,10))<br/> origISN = Replace(origISN, " ", "")<br/> origISN = Replace(origISN, " ", "")<br/> origAUX   = Trim(Mid(s,35,9))<br/> origAUX = Replace(origAUX, " ", "")<br/> origAUX = Replace(origAUX, " ", "")<br/> origRT    = Trim(Mid(s,44,11))<br/> origRT = Replace(origRT, " ", "")<br/> origRT = Replace(origRT, " ", "")<br/> origwAUX  = Trim(Mid(s,54,9))<br/> origwAUX = Replace(origwAUX, " ", "")<br/> origwAUX = Replace(origwAUX, " ", "")<br/> origACCT  = Trim(Mid(s,63,18))<br/> origACCT = Replace(origACCT, " ", "")<br/> origACCT = Replace(origACCT, " ", "")<br/> origTC    = Trim(Mid(s,81,7))<br/> origTC = Replace(origTC, " ", "")<br/> origTC = Replace(origTC, " ", "")<br/> 'Not using DESCRIPTION at this time<br/> origAMT   = Trim(Mid(s,99,14))<br/> origAMT = Replace(origAMT, " ", "")<br/> origAMT = Replace(origAMT, " ", "")<br/><br/> End If <br/> <br/> If InStr (s,"ChangedTo") &gt; 0 Then <br/> 'storBatch = Trim(Mid(s,1,7))<br/> 'storBatch = Replace(storBatch, " ", "")<br/> 'storBatch = Replace(storBatch, " ", "")<br/> 'storTrans = Trim(Mid(s,7,8))<br/> 'storTrans = Replace(storTrans, " ", "")<br/> 'storTrans = Replace(storTrans, " ", "")<br/> 'storOLD   = Trim(Mid(s,15,10))<br/> 'storOLD = Replace(storOLD, " ", "")<br/> 'storOLD = Replace(storOLD, " ", "")<br/> storISN   = Trim(Mid(s,25,11))<br/> storISN = Replace(storISN, " ", "")<br/> storISN = Replace(storISN, " ", "")<br/> storAUX   = Trim(Mid(s,36,10))<br/> storAUX = Replace(storAUX, " ", "")<br/> storAUX = Replace(storAUX, " ", "")<br/> storRT    = Trim(Mid(s,45,10))<br/> storRT = Replace(storRT, " ", "")<br/> storRT = Replace(storRT, " ", "")<br/> storwAUX  = Trim(Mid(s,55,9))<br/> storwAUX = Replace(storwAUX, " ", "")<br/> storwAUX = Replace(storwAUX, " ", "")<br/> storACCT  = Trim(Mid(s,63,19))<br/> storACCT = Replace(storACCT, " ", "")<br/> storACCT = Replace(storACCT, " ", "")<br/> storTC    = Trim(Mid(s,81,7))<br/> storTC = Replace(storTC, " ", "")<br/> storTC = Replace(storTC, " ", "")<br/> 'Not using DESCRIPTION at this time<br/> storAMT   = Trim(Mid(s,100,14))<br/> storAMT = Replace(storAMT, " ", "")<br/> storAMT = Replace(storAMT, " ", "")<br/> 'MsgBox "origAMT:"&amp; origAMT &amp;"storAMT:"&amp; storAMT<br/> If Count = 0  Then<br/> If origAMT = 0.00 And storAMT &gt; origAMT Then<br/> 'objOutputFile.WriteLine origISN &amp; " " &amp; "00"<br/> stroutput = origISN &amp; " " &amp; "00"<br/> Count = Count + 1<br/> ElseIf origAMT &lt;&gt; storAMT Then<br/> 'objOutputFile.WriteLine origISN &amp; " " &amp; "02"<br/> stroutput = origISN &amp; " " &amp; "02"<br/> Count = Count + 1<br/> End If <br/> End If <br/> <br/> If origAUX &lt;&gt; storAUX And storAUXn = 0 Then <br/> editFlds = editFlds + 01000000<br/> storAUXn = storAUXn + 1<br/> End If <br/> 'If origRIC &lt;&gt; storRIC Then <br/> 'editFlds = editFlds + 00100000<br/> 'End If<br/> If origRT &lt;&gt; storRT And storRTn = 0 Then <br/> editFlds = editFlds + 00010000<br/> storRTn = storRTn + 1<br/> End If<br/> If origwAUX &lt;&gt; storwAUX And storwAUXn = 0 Then <br/> editFlds = editFlds + 00001000<br/> storwAUXn = storwAUXn + 1<br/> End If<br/> If origACCT &lt;&gt; storACCT And storACCTn = 0 Then <br/> editFlds = editFlds + 00000100<br/> storACCTn = storACCTn + 1<br/> End If<br/> If origTC &lt;&gt; storTC And storTCn = 0 Then <br/> editFlds = editFlds + 00000010<br/> storTCn = storTCn + 1<br/> End If<br/> If origAMT &lt;&gt; storAMT Then<br/> If Count = 1 Then<br/> editFlds = editFlds + 00000001<br/> Count = Count + 1<br/> End If <br/> End If<br/> editFlds = Right("00000000" &amp; editFlds, 8)<br/> <br/> End If<br/>Loop<br/> objOutputFile.Close<br/>        Set objOutputFile = Nothing<br/>        Set objFS = Nothing<br/> 'WScript.Echo "Completed"<br/>WScript.Quit()<br/><br/>Function intialize()<br/><br/>editFlds = 00000000<br/>' ^Empty <a href="https://interviewquestions.tuteehub.com/tag/serial-1201712" style="font-weight:bold;" target="_blank" title="Click to know more about SERIAL">SERIAL</a> RIC(Pos44) TR FLD4 AMT TC AMT<br/>Count = 0<br/><br/>storAUXn = 0<br/>storRTn = 0<br/>storwAUXn = 0<br/>storACCTn = 0<br/>storTCn = 0<br/><br/>End Function<br/><br/>An array isn't a bad idea, considered it but as you can see above still very messy.</body></html>


Discussion

No Comment Found