2
votes

Happy to be back in the forums!

So after some extensive searching both in the AHK documentation, stackexchange and the ahk_forum, I have decided to seek help from the experts

I will explain the code I am working on and then post some examples.

I have written a script that pulls the value of untranslated and unverified words of 6 different word files and does this for 23 different languages via WebTranslateIt (site). I have managed to get it to work by coping the block of code and setting the "fileid" variable to one of the 6 files before each new query, however when I arrived at the language iteration part, I quickly noticed it would be a lot more efficient (and significantly less code) to simply have one loop within a loop iterate through two arrays until end of array.

This script accesses a website using Google Chrome, then proceeds to inject several jQueries into the developer tools and extract the results into independently named variables (which I would like to assign as part of an associate array).

This is the part that I am having issues with... I can loop through an array succesfully via:

langid := ["bg", "cs", "da", "el", "eu", "fi", "hr" , "ja", "ko", "lt", "nb", 
"nl", "pl", "pt", "ro" , "ru", "sk", "sl", "sv", "th" , "tr", "zh", "zh-Hant"]

For Key, Value in langid
;   MsgBox, %A_Index% = %Value%    ; the MsgBox displays index values correctly but when putting my script in a loop start after the For Key part, it does not provide a value when sending the var inside a url. This works fine when I set the var manually before sending the URL.

But when I tried running this right before Looping the script that does the main querying, the 'langid' variable in the URL I send to change the language is blank.

I would like to loop the script 23 times through all the languages. When the script enters the loop there is a part that enters page URL with a %langid% and I cannot get this part to work with the above code.

Further to this, I would also like to assign further values to each key in the index as follows:

langid := ["bg", "cs", "da", "el", "eu", "fi", "hr" , "ja", "ko", "lt", "nb", 
"nl", "pl", "pt", "ro" , "ru", "sk", "sl", "sv", "th" , "tr", "zh", "zh-Hant"]

lang := ["Bulgarian", "Czech", "Danish", "Greek", "Basque", "Finnish", 
"Hungarian", "Japanese", "Korean", "Lithuanian", "Norwegian", "Dutch", 
"Polish", "Portuguese", "Romanian", "Russian"]

The 'lang' array should reference the index position in 'langid', as I use it as a descriptive variable in some MsgBoxes that show word count progress and a function at the end that writes the total results of the 6 files per language. (This is purely aesthetic but I would love to get it working in the loop)

I also need something very similar with the wordfiles:

filenum := ["342553", "342582", "342411", "342367", "342467", "342502"]

wrdfile := ["OHFrontend", "OHFrontendListing", "HouseTypeAndSubType", "GuestType", "RoomTypeFeatureName", "RoomTypeFaturePrefixAndBracketOption"]

The 'wrdfile' array is also purely aesthetic, as I rather display the filename and not filenum in the totals confirmations. The 'filenum' however IS important and needs to iterate through each value in the array once until arriving at the end, at which point it needs to restart but with the next language in the 'lang' array.

I am thinking the following structure would work:

langid := [val1, val2, etc..]
lang := [val1, val2, etc..]
For Key, Value in langid     ; iterate through Languages
    Loop
    {
    filenum := [val1, val2, etc...]
    wrdfile := [val1, val2, etc...]
    Run Chrome
    Open Site
    For Key, Value in filenum    ;iterate through Wordfiles
        Loop
        {
        Send URL containing 'langid' var & 'filenum' var
        Open DevTools and send jQuery and store totals
        Confirm totals in MsgBox and save in txt file
        }
Return

It is worth noting that all languages need to iterate through the same 6 wordfiles, so perhaps the file array can also be part of the first associative array instead of two different ones...? Not sure what is the best approach here.

Please help me me find the correct structure and syntax for the loops and if possible point me in the right direction as far as the arrays go. (I know I am not associating them correctly)

  1. I need assistance with forming associative arrays... the documentation and examples I´ve looked up is not thorough enough. Please help atleast with a push in the right direction :oops:

  2. I need assistance with the For Key command. It should go through the loop underneath it serving up the next 'langid' value on each iteration until the last value ("zh-Hant").

  3. Once I have point 2 working then I will tweak the script to also loop through the 6 wordfiles and then reiterate language. My intention is to loop through the 6 wordsfile for each language. Once that is working I will add a small GUI prompt to enable going through the entire loop or just through a specific language.

Link to AHK_Forum post containing full code.

THANKS AGAIN!

1

1 Answers

0
votes

You could use an associative array for your language and files variables. The correct syntax for associative arrays is as follows.

object := { "key1" : "value", "key2" : "value 2" }

You can iterate over the array with a for loop like so.

for key, value in object
    MsgBox key: %key% value: %value%

The for loop already iterates through your object so you don't need the loop beneath it. If I'm understanding you correctly you could try something like this.

; Associative arrays
Langs := { "bg" : "Bulgarian", "cs" : "Czech" } 
Files := { "342553" : "OHFrontend", "342582" : "OHFrontendListing"}

; Open Chrome
For langId, langName in Langs
{
  For fileId, fileName in Files
  {
    url := "example.com/" . langId . "/" . fileId
    ; Send url with langId & fileId
    ; Open DevTools and send jQuery and store totals
    ; Confirm totals in MsgBox and save in txt file
    msgbox %url%
  }
}

Return