Jump to content

Recommended Posts

Posted (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 by zeenmakr
Posted

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. 😄

Posted (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 by zeenmakr
Posted

@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 :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted

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

 

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

 

Posted (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 by zeenmakr
Posted

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.

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...