youtuber Posted August 18, 2018 Share Posted August 18, 2018 Friends, if the characters I have identified are, how can I change the first four characters? $aID = "BLUS12345" $aID = StringTrimLeft($aID, 4) If StringInStr($aID, 'BCES') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPEA" & $aID & @CRLF) EndIf If StringInStr($aID, 'BLES') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPEB" & $aID & @CRLF) EndIf If StringInStr($aID, 'BCUS') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPUA" & $aID & @CRLF) EndIf If StringInStr($aID, 'BLUS') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPUB" & $aID & @CRLF) EndIf If StringInStr($aID, 'BCJS') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPJA" & $aID & @CRLF) EndIf If StringInStr($aID, 'BLJM') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPJB" & $aID & @CRLF) EndIf If StringInStr($aID, 'BCAS') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPHA" & $aID & @CRLF) EndIf If StringInStr($aID, 'BLAS') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPHB" & $aID & @CRLF) EndIf Link to comment Share on other sites More sharing options...
pixelsearch Posted August 18, 2018 Share Posted August 18, 2018 Hi youtuber, this could do it : Global $aArray[8][2] = _ [["BCES", "NPEA"], _ ["BLES", "NPEB"], _ ["BCUS", "NPUA"], _ ["BLUS", "NPUB"], _ ["BCJS", "NPJA"], _ ["BLJM", "NPJB"], _ ["BCAS", "NPHA"], _ ["BLAS", "NPHB"]] Global $aID = "BLUS12345", $aID_New = "", $aID_Left = StringLeft($aID, 4) For $i = 0 To 7 If $aID_Left = $aArray[$i][0] Then $aID_New = $aArray[$i][1] & StringMid($aID, 5) ExitLoop EndIf Next If $aID_New <> "" Then MsgBox(0, "Found", $aID & " => " & $aID_New) Else MsgBox(0, "Not Found", "No replacement done") EndIf Good luck youtuber 1 Link to comment Share on other sites More sharing options...
pixelsearch Posted August 18, 2018 Share Posted August 18, 2018 Or you can keep your script with a little change to have it work $aID = "BLUS12345" $aID2 = StringTrimLeft($aID, 4) If StringInStr($aID, 'BCES') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPEA" & $aID2 & @CRLF) EndIf If StringInStr($aID, 'BLES') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPEB" & $aID2 & @CRLF) EndIf If StringInStr($aID, 'BCUS') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPUA" & $aID2 & @CRLF) EndIf If StringInStr($aID, 'BLUS') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPUB" & $aID2 & @CRLF) EndIf If StringInStr($aID, 'BCJS') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPJA" & $aID2 & @CRLF) EndIf If StringInStr($aID, 'BLJM') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPJB" & $aID2 & @CRLF) EndIf If StringInStr($aID, 'BCAS') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPHA" & $aID2 & @CRLF) EndIf If StringInStr($aID, 'BLAS') Then ConsoleWrite(@ScriptLineNumber & " : " & "NPHB" & $aID2 & @CRLF) EndIf youtuber 1 Link to comment Share on other sites More sharing options...
Zedna Posted August 20, 2018 Share Posted August 20, 2018 $aID = "BLUS12345" $aID1 = StringLeft($aID, 4) $aID2 = StringMid($aID, 5) If $aID1 = 'BCES' Then ConsoleWrite(@ScriptLineNumber & " : " & "NPEA" & $aID2 & @CRLF) ElseIf $aID1 = 'BLES' Then ConsoleWrite(@ScriptLineNumber & " : " & "NPEB" & $aID2 & @CRLF) ElseIf $aID1 = 'BCUS' Then ConsoleWrite(@ScriptLineNumber & " : " & "NPUA" & $aID2 & @CRLF) ElseIf $aID1 = 'BLUS' Then ConsoleWrite(@ScriptLineNumber & " : " & "NPUB" & $aID2 & @CRLF) ElseIf $aID1 = 'BCJS' Then ConsoleWrite(@ScriptLineNumber & " : " & "NPJA" & $aID2 & @CRLF) ElseIf $aID1 = 'BLJM' Then ConsoleWrite(@ScriptLineNumber & " : " & "NPJB" & $aID2 & @CRLF) ElseIf $aID1 = 'BCAS' Then ConsoleWrite(@ScriptLineNumber & " : " & "NPHA" & $aID2 & @CRLF) ElseIf $aID1 = 'BLAS' Then ConsoleWrite(@ScriptLineNumber & " : " & "NPHB" & $aID2 & @CRLF) EndIf youtuber 1 Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Malkey Posted August 20, 2018 Share Posted August 20, 2018 Here are two methods. The first is a little esoteric in establishing the meaningful, temporary array. The second is a modified version of pixelsearch's example of post #2 using _ArraySearch() to find the appropriate index. expandcollapse popup#include <Array.au3> ; Ref: https://www.autoitscript.com/forum/topic/195350-how-to-first-four-characters-changes/ Local $sFind = "BCES, BLES, BCUS, BLUS, BCJS, BLJM, BCAS, BLAS" ; Sub-strings to be found. Local $sRepl = "NPEA, NPEB, NPUA, NPUB, NPJA, NPJB, NPHA, NPHB" ; Corresponding replacement sub-strings. Local $sID = "321BLUS12345" ; Not necessarily only the first few characters in the string are to be found. Local $sREPattern = "(" & StringRegExpReplace($sFind, "\h*,\h*", ")|(") & ")" Local $aReplace = StringRegExp($sRepl, "(.+?)(?:\h*,\h*|$)", 3) ; Will not capture spaces either side of the separator coma when those spaces are present. ; The following array shows:- ; The found sub-string - "$aTemp[UBound($aTemp) - 1]"; and, ; The corresponding replacement sub-string, "$aReplace[UBound($aTemp) - 1]". Local $aTemp = StringRegExp($sID, $sREPattern, 1) ;_ArrayDisplay($aTemp) ; Display result If Not @error Then ;$sID_New = StringRegExpReplace($sID, "(" & $aTemp[UBound($aTemp) - 1] & ")", $aReplace[UBound($aTemp) - 1]) ; or $sID_New = StringReplace($sID, $aTemp[UBound($aTemp) - 1], $aReplace[UBound($aTemp) - 1]) MsgBox(0, "StringRegExp() Found", $sID & " => " & $sID_New) Else MsgBox(0, "Not Found", "No replacement done") EndIf ; --------------- Or, another method ---------------- Global $aArray[8][2] = _ [["BCES", "NPEA"], _ ["BLES", "NPEB"], _ ["BCUS", "NPUA"], _ ["BLUS", "NPUB"], _ ["BCJS", "NPJA"], _ ["BLJM", "NPJB"], _ ["BCAS", "NPHA"], _ ["BLAS", "NPHB"]] Global $sID = "BLUS12345" Local $iIndex = _ArraySearch($aArray, StringLeft($sID, 4)) ; Display result If Not @error Then $sID_NewA = $aArray[$iIndex][1] & StringTrimLeft($sID, 4) MsgBox(0, "_ArraySearch() Found", $sID & " => " & $sID_NewA) Else MsgBox(0, "Not Found", "No replacement done") EndIf youtuber 1 Link to comment Share on other sites More sharing options...
iamtheky Posted August 22, 2018 Share Posted August 22, 2018 (edited) First 2 characters of the replacement are NP. If C is the 2nd character of the match then A is the last letter of the replacement. If L is the 2nd character of the match then B is the last letter of the replacement, and the 3rd character is the same as the previous replacement. Meaning the only replacement information you really need is the 3rd character of every other entry. If the live data allows that character to be derived this entire operation gets real quick. Edited August 22, 2018 by iamtheky IAMK and youtuber 2 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) 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