Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Get all keys and values from a section of ini


  • Please log in to reply
4 replies to this topic
Yarbuck
  • Members
  • 3 posts
  • Last active: Oct 31 2005 06:14 PM
  • Joined: 27 Oct 2005
Here's a function might be handy if you don't know how many keys are in a sectionof an .ini, eg: if the ini file has been updated. It takes startstring, stopstring and inifile for parameters, and creates 2 arrays of all keys and values in between the lines found to contain startstring and stopstring. One is for the list of keys, the other for the values, and it also creates a variable called mirindex that is the number of keys found.
ManualIniRead(StartString, StopString, iniFile)
{  
	Global
	Continue = True
	Record = False
	ArrayIndex = 0
	Loop, Read, %iniFile%
	{
		If Continue = True
		{
			If Record = True
			Loop, parse, A_LoopReadLine, =,
			{
				If a_index = 1
				{				
					MirIndex++
					ManKey%MirIndex% := A_LoopField					
				}
				Else If a_index = 2
				{					
					ManVal%MirIndex% := A_LoopField				
				}
				If a_loopfield = %StartString%
				{
					Record = True
				}
				Else If a_loopfield = %StopString%
				{
					Record = False
					Continue = False
				}			
			}
			Else
			Loop, parse, A_LoopReadLine, =,
			{
				IfInString, A_LoopField, %StartString%
				{				
					Record = True
				}
				Else
				{
				  IfInString, A_LoopField, %StopString%
				  {
					 Record = False
					 Continue = False
					}
				}			
			}
		}			
		Else
		{
			Break
		}
	}
	MirIndex := MirIndex - 1
	Return
}
And here's how to retrieve it all. In this example I was using a dictionary file to replace words that the text to speech had trouble pronouncing with phonetic versions.
ManualIniRead("[Pronounce]","##EndOfWords##","dictionary.dic")
Loop %IniReadIndex%
{
  Word :=ManKey%A_Index%
  Replacement :=ManVal%A_Index%
  StringReplace, Text, Text, %word%, %replacement% ,All
  Sleep, 20
}

I'd like some feedback if it's not too much trouble. I'm relatively new at this so if there are better ways to do what I've done here I'd like to know. :lol:

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I know some people have wanted better INI reading capabilities. I think this script will be quite useful.

Thanks for posting it.

Joy2DWorld
  • Members
  • 562 posts
  • Last active: Jun 30 2014 07:48 PM
  • Joined: 04 Dec 2006
(don't have much time... saw this... and write this in case it helps anyone...)


If Record = True 
         Loop, parse, A_LoopReadLine, =, 
         { 
            If a_index = 1 
            {             
               MirIndex++ 
               ManKey%MirIndex% := A_LoopField                
            } 
            Else If a_index = 2 
            {                
               ManVal%MirIndex% := A_LoopField             
            } 
            If a_loopfield = %StartString% 
            { 
               Record = True 
            } 
            Else If a_loopfield = %StopString% 
            { 
               Record = False 
               Continue = False 
            }          
         } 


looks like is breaking line based on '=''s... problem if value is like "love = hate" or whatever...

so...
instead.. maybe...

If Record = True and (regexmatch(A_LoopReadLine,"^(\s*[^;=][^=]*)=(.*)",V))
{
	MirIndex++ 
	ManKey%MirIndex% := V1
	ManVal%MirIndex% := V2
	If A_LoopReadLine contains %StartString% 
            { 
               Record = True 
            } 
	Else If A_LoopReadLine contains %StopString% 
            { 
               Record = False 
               Continue = False 
            }          

}


or something
Joyce Jamce

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Well, one could use DllCall to the original INI functions which allow such iteration. I believe this has been posted somewhere on this forum.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
Yup, in this thread: VisualINI :wink: