zeenmakr Posted February 5, 2020 Posted February 5, 2020 (edited) Hi, I have a list of couple hundred of words that needed to be convert into something else. This is a working example I came up with below. Is there anyway to improve this or a better way of create the $FruitList that could result in slick lookup table. Also I really don't feel like the For loop is efficient either. Any Alternatives like StringRexExp that could do the trick? Thanks $rtn = WalkBackward('Banana') MsgBox(0,'Found', $rtn) ;return Ananab Func WalkBackward($fruit) Local $FruitList[7] = [''& _ 'Berry|Yrreb', ''& _ 'Apple|Elppa', ''& _ 'Orange|Egnaro', ''& _ 'Banana|Ananab', ''& _ 'Grape|Eparg', ''& _ 'Carrot|Torrac'] For $i=0 To UBound($FruitList) $iGroup = StringSplit($FruitList[$i], '|', 2) If $fruit = $iGroup[0] then ConsoleWrite('At index:'&$i&' Found: '&$iGroup[0]&'='&$iGroup[1]&@LF) ExitLoop EndIf Next Return $iGroup[1] EndFunc Edited February 5, 2020 by zeenmakr
Danp2 Posted February 5, 2020 Posted February 5, 2020 I would suggest storing your word list outside of your script (INI file, Excel spreadsheet, etc). Then you can just perform a lookup to find the desired entry and it's associated translation. If you insist on doing it as described above, you could do something like this -- Local $FruitList[7] = [''& _ '|Berry|Yrreb|', ''& _ '|Apple|Elppa|', ''& _ '|Orange|Egnaro|', ''& _ '|Banana|Ananab|', ''& _ '|Grape|Eparg|', ''& _ '|Carrot|Torrac|'] and then use _ArraySearch to perform a partial search like this -- _ArraySearch($FruitList, "|" & $fruit & "|", 0, 0, 0, 1) All of this is untested, so YMMV. 😄 Latest Webdriver UDF Release Webdriver Wiki FAQs
zeenmakr Posted February 5, 2020 Author Posted February 5, 2020 (edited) 19 minutes ago, Danp2 said: _ArraySearch($FruitList, "|" & $fruit & "|", 0, 0, 0, 1) All of this is untested, so YMMV. 😄 Well that doesn't do it. what are the modified Pipes suppose to do? $rtn = _ArraySearch($FruitList, "|" & $fruit & "|", 0, 0, 0, 1) Msgbox(0,'$rtn', $rtn) ;return -1 Edited February 5, 2020 by zeenmakr
FrancescoDiMuro Posted February 5, 2020 Posted February 5, 2020 @zeenmakr You could use an "ad hoc" function to do such thing, like the example below: Global $arrWords[6] = ["Berry", "Apple", "Orange", "Banana", "Grepe", "Carrot"] For $i = 0 To UBound($arrWords) - 1 Step 1 ConsoleWrite("The reverse word of '" & $arrWords[$i] & "' is '" & ReverseWord($arrWords[$i]) & "'." & @CRLF) Next Func ReverseWord($strText) Return Execute(StringRegExpReplace($strText, '^(\w{1})(.*)(\w{1})$', 'StringUpper("$3") & StringReverse("$2") & StringLower("$1")')) EndFunc As @Danp2 said, you may want to store the different words in an INI file, and then use the "reverse" function to do whatever you need zeenmakr 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
water Posted February 5, 2020 Posted February 5, 2020 Or use the Dictionary object as mentioned in the threads title. How to fill the dictionary depens where you get the item/value pairs from (should be easy to retrieve the values from a file and add them to the Dictionary in just a few lines of code). So this is just a simple example: Global $oDict = ObjCreate("Scripting.Dictionary") ; Create the Dictionary Global $sItem = "Berry" Global $sValue = "Yrreb" $oDict.Add($sItem, $sValue) ; Add a member to the Dictionary MsgBox(0, "Dictionary", "Item: " & $sItem & " = " & "Value: " & $oDict.Item($sItem)) ; Retrieve a value from the Dictionary zeenmakr 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
zeenmakr Posted February 5, 2020 Author Posted February 5, 2020 (edited) @FrancescoDiMuro haha that's sure is cool little trick! Sorry for the poor example. I did not intent to do Word Reverse. I need it to look up 'Rock' and return 'Blue Gem' for example. I meant what it said in the title 'Dictionary Words Look Up Table'. Sorry for the confusion. $Table = [''& _ 'Rock|Blue Gem', & _ 'Crytal|Clear', & _ 'Monkey see|Monkey do', & _ 'I Heart|You', & _ 'Hello|Aloha'] Edited February 5, 2020 by zeenmakr
iamtheky Posted February 5, 2020 Posted February 5, 2020 following @water's route, this example and the one below it build a dictionary and then flex it to do things like return the language. zeenmakr 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
Danp2 Posted February 5, 2020 Posted February 5, 2020 35 minutes ago, zeenmakr said: what are the modified Pipes suppose to do? Prevent you from getting a false match on a partial word. Without the delimiters, you could search on "Ape" and get a match on "Grape" zeenmakr 1 Latest Webdriver UDF Release Webdriver Wiki FAQs
zeenmakr Posted February 7, 2020 Author Posted February 7, 2020 @water & @iamtheky & @FrancescoDiMuro - thanks. will check back if needed as im working this out
FrancescoDiMuro Posted February 7, 2020 Posted February 7, 2020 @zeenmakr Glad I could be of help Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
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