Ordered Array Help Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Ordered Array Help

17 Jul 2018, 08:52

Hello, I am working with an array that the order of the file I read matters. For example I will use the first two options in my text file to show the issue.

Code: Select all

AIS, AIS , AIS
Aero, Aero , Aeronautical
Then when I read the text file to parse the CSV contents I get Aero before AIS, but the way everything is set up I need to have the order remain. So AIS needs to come before Aero
Code that I'm working with

Code: Select all

dict := {}
	Loop, Read, Test.txt
	{
		row := StrSplit(A_LoopReadLine, ",")
		key := row[1]
		row.RemoveAt(1)
		dict[key] := row
	}
	Counter := 0
	; TEST
	For Key,Val in dict
	{
		Loop % Val.Length()
		{
			If InStr(Clipboard,Val[A_Index], CaseSensitive = False)
			{
				match = %Key%
				FileAppend, % Counter "|" match ",", Apply.txt
				FileAppend, % match "`n", FeatureTypes.txt
				break
			}
		}
		Counter ++
	}
Thanks!
Freire
Posts: 16
Joined: 10 Dec 2017, 11:30

Re: Ordered Array Help  Topic is solved

17 Jul 2018, 12:54

The issue you are facing is due to the order of reading the dictionary.

The dicitonary will not answer the for loop in the order the keys were added, instead the dictionary will sort the keys.

If you want the order you read to be the order you analyse the text, you would need an Array.

The Array will keep the order of the stuff you add into it, and you will use the indexed to navigate through.

This is a small change to your code.

Code: Select all

array:=[]

Loop, Read, Test.txt
{
  row := StrSplit(A_LoopReadLine, ",")
  key := row[1]
  row.RemoveAt(1)
  array.Push([key,row]) ; [["AIS",[" AIS "," AIS"]],[...]]
}

Counter := 0

Loop, % array.Length()
{
  Key := array[A_Index][1]
  Val := array[A_Index][2]
  Loop % Val.Length()
  {
    MsgBox, % Val[A_Index]
    If InStr(Clipboard,Val[A_Index], CaseSensitive = False)
    {
      match = %Key%
      FileAppend, % Counter "|" match ",", Apply.txt
      FileAppend, % match "`n", FeatureTypes.txt
      break
    }
  }
  Counter ++
}
I have changed the path of Test.txt before, just edited to the original.
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Ordered Array Help

17 Jul 2018, 13:48

That indeed did work. Normally I would be happy with the autosort but the list I am dealing with has to remain in the order of the text file otherwise later the list will be offset slightly.
But basically all you made it do was start with the first array then move on instead of having it change slightly via the autosort. Thanks!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Holarctic, Rohwedder and 197 guests