AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

IniRead with dynamic loop

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
ImprisonedPride



Joined: 30 Apr 2008
Posts: 28

PostPosted: Thu May 15, 2008 8:25 pm    Post subject: IniRead with dynamic loop Reply with quote

Hey. I have an app where the user is allowed to add to a .ini file. The [Heading] of this section can be anything the user desires, so I need a way to be able to loop through the ini file for x number of sections who's names I don't know. Is this at all possible? I didn't see anything under IniRead.
_________________
Call me IP.
Back to top
View user's profile Send private message
Icarus



Joined: 24 Nov 2005
Posts: 440

PostPosted: Thu May 15, 2008 8:54 pm    Post subject: Reply with quote

Thats a good question.

I also needed to do something similar many times, and I have two methods of solving it for you:

1. You do your own reading of the file using FileRead - you then go and look for lines that start with "[" to recognize a section, lines that look like *=* to recognize a key=value and so on. If you are just starting with AHK, this may be too much to start with, unless you have experience in other pgoramming languages.

2. Another way, is to have a known key that contains a comma separated list of the sections to read. For example

Code:

[General]
Sections=Section 1,Operations,Nikita

[Section 1]
SomeKey=SomeValue

[Operations]
...

[Nikita]
...


Then, in your script, you first read the Generatl->Sections key, StringSplit it and loop through its contents, like so:
Code:

StringSplit Section, Sections, `,
Loop %Section0% {
  Msgbox % Section%A_Index%
}


hth
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
ImprisonedPride



Joined: 30 Apr 2008
Posts: 28

PostPosted: Thu May 15, 2008 9:37 pm    Post subject: Reply with quote

Thanks a ton icarus, I had already started to develop a method as you described as #1. The problem with #2 is that the general list would be dynamic as well, since the user is able to add/delete things from the file. So then I'd have to rewrite that value to omit the value deleted from the list by splitting said value and looping through it... ugh. You can probably see why I'll do it with method #1. I have strong backgrounds in other languages like Flash Actionscript and C++, but I'm still very new to function names of AHK. Thanks again.
_________________
Call me IP.
Back to top
View user's profile Send private message
Icarus



Joined: 24 Nov 2005
Posts: 440

PostPosted: Thu May 15, 2008 9:47 pm    Post subject: Reply with quote

With what you describe, I would still go with method #2 - it is much easier to manage. The full "MyIniRead" method is an overkill for your task.

You do not need to delete things from the list - you can just recreate it whenever it changes by the users input.

If your code does not allow for such implementation (although I cant imagine why), you may still be able to delete stuff from the list easily.

Two methods I use:
1. Is to use a RegExReplace that looks for a word that is between commas or between a comma and an end/beginning of a line
2. You make the list so that each word ALWAYS has two blockers around it, and then you use StringReplace (e.g. Sections=|Section 1||Nikita||Operations|

I would recommend to first try the easier solutions.

EDIT: Another suggestion, is to drop the use of INI altogether in this case, and just use your own format - so that each line contains all the information a section would contain, and then you just read the file, and parse each line as you need.

Example:

Section 1:param1,param2,param3
Nikita:param1,param2,param3
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
ImprisonedPride



Joined: 30 Apr 2008
Posts: 28

PostPosted: Thu May 15, 2008 10:19 pm    Post subject: Reply with quote

I wound up going with method 2 because I'd already compiled the .ini file. It was a ton easier to implement than I thought it would be... took about 15 minutes. Thanks a lot!

Also, I think deleting from the INI would be a cinch case:

1.Read in sections
2.Loop through, adding each one to a new string var; one that should be deleted gets skipped
3. write it back to the key in general setion
4. use IniDelete to "drop" the whole section to be deleted.
_________________
Call me IP.
Back to top
View user's profile Send private message
Guest






PostPosted: Fri May 16, 2008 11:39 am    Post subject: Reply with quote

You might check out this thread, it is about how to get all
section names from an ini file.

http://www.autohotkey.com/forum/topic9093.html
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group