Jump to content


Photo

DropDownList from a file


  • Please log in to reply
24 replies to this topic

#1 Deverex

Deverex
  • Members
  • 10 posts

Posted 24 March 2012 - 12:42 AM

Hi! I'm really new to AutoHotKey (as in, started learning it today). I've been reading through a bunch of the documentation and couldn't find an example that I understood.

My question is: how would I put the contents of a text or .ini file into a DropDownList?

Also would there be an easy way to read every other line into the drop down list. The idea behind this is that the first line would be an account name, and the second line would be a password for the first account and the third line would be the second account name, and so on.

#2 rtcvb32

rtcvb32
  • Members
  • 542 posts

Posted 24 March 2012 - 01:08 AM

This reminds me of something I did, a few years back I had config files where I could have specific actions added, and regexes to act on (I need to finish the update sometime soon). The basic idea is.. (I Hope I got this right)

FileRead, in, file.txt

;create 'list' text that uses | for separators from your input.
;in this case the loop/preparing is skipped
list := in

Gui, Add, DropDownList, vVariable, %list%

Updating it is the same as updating most other gui's contents.
guiControl,, Variable, %newlist%


#3 MilesAhead

MilesAhead
  • Members
  • 429 posts

Posted 24 March 2012 - 01:18 AM

Alternating lines key/data are easy to read from text file.

First read the whole file into a variable, assuming it's not a huge file. Then use Mod operator to determine if you are on odd lines(keys) or even lines(values)

FileRead,Contents,%Filename%

Loop, parse, Contents, `n, `r
{
 if Mod(A_Index,2)
    keystr := A_LoopField
 else
  valuestr := A_LoopField
}


#4 Deverex

Deverex
  • Members
  • 10 posts

Posted 24 March 2012 - 01:49 AM

I tried putting the code together in a little test file and I couldn't get it to work. I might be an idiot, but this was my best attempt. Could someone just run through why it's not working and how to make it work? It would be much appreciated


FileRead, Contents, Accounts.txt

Loop, parse, Contents, `n, `r
{
 if Mod(A_Index,2)
    keystr := A_LoopField
 else
  valuestr := A_LoopField
}

list := Contents

Gui, Add, DropDownList, vDropdown, %list%
Gui, Show, w200 h200, Test
guiControl,, DropDown, %newList%
return

GuiClose:
ExitApp


#5 rtcvb32

rtcvb32
  • Members
  • 542 posts

Posted 24 March 2012 - 06:36 AM

I see two problems. First you took our partial examples and threw them together. Second, MilesAhead's example for the loop is broken, since you'd only get the last key and or value. But we also don't know 'what' format you have your information in. Comma separated? Tab? Every other line (even odd)?

I will assume it's comma separated.


;FileRead, file, file.txt
;simulation
file =
(
intro.txt, Introductions
ch1.txt, Chapter 1 - The land before time
ch2.txt, Chapter 2 - Oil
ch3.txt, Chapter 3 - Electricity
)

key0 = 0
list =
Loop, parse, file, `n, `r
{
	StringSplit, parts, a_loopField, `,
	if (parts0 == 2) {
;simulated array
;		key0++
;		key%key0% := parts1
		list .= parts2 . "|"
	}
}

;list in a    one|two|three type of format.
Gui, Add, DropDownList, gddSelect vDropdown, %list%
Gui, Show, w200 h200, Test
return

GuiClose:
ExitApp

ddSelect:
gui, submit, nohide
msgbox %DropDown%
return

I don't think the dropdown's have an Id number of which one they were, so actually having the key array is almost moot, meaning you'd have to search for the dropdown to get the key value. Mmmm... I think listview was better on that, but I don't remember. I just don't use this all that often.

#6 MilesAhead

MilesAhead
  • Members
  • 429 posts

Posted 24 March 2012 - 07:02 AM

I see two problems. First you took our partial examples and threw them together. Second, MilesAhead's example for the loop is broken,


It was merely a snippet to show how to determine odd or even lines in a file. I was using Scripting.Dictionary. Since the OP is obviously new I didn't want to get into explaining all that business. But the point about throwing fragments together is valid. One should use the snippets as a jumping off point to read the docs. Maybe to functions one wasn't aware of initially.

The actual loop I used is here. "sd" is an instance of Scripting.Dictionary. I wrote it before native associative array support via AHK_L


  Loop, parse, Contents, `n, `r
  {
   if Mod(A_Index,2)
      keystr := A_LoopField
   else
      sd.item(keystr) := A_LoopField
  }
Return

So to avoid all the associative array mumbo jumbo I should have posted something like this:


  Loop, parse, Contents, `n, `r
  {
     if Mod(A_Index,2)
    {
       ; A_LoopField has content of odd line from file here.
       ; Do something with it. If alternating key/value then
       ; this line would be the key.
    }
     else
    {
     ; A_LoopField has content of even line from file here.
     ; Do something with it. If alternating key/value then
     ; this line would be the value.
    }
  }
Return


#7 rtcvb32

rtcvb32
  • Members
  • 542 posts

Posted 24 March 2012 - 07:46 AM

It was merely a snippet to show how to determine odd or even lines in a file. I was using Scripting.Dictionary. Since the OP is obviously new I didn't want to get into explaining all that business. But the point about throwing fragments together is valid. One should use the snippets as a jumping off point to read the docs. Maybe to functions one wasn't aware of initially.


Understood and agreed. But sometimes you just need a working example. If he had tried to adapt it rather than throw it all together with it making no sense code-wise then I'm sure part of this wouldn't have come up.

I wonder how many of them are just lazy and Hope we will write it all for them. I'm sure there's hundreds, nay, thousands of fully working examples inside the scripts and functions section of the forum.

#8 Deverex

Deverex
  • Members
  • 10 posts

Posted 24 March 2012 - 07:50 AM

First off, thank you both for understanding I'm a beginner at this. I realized I was wrong to just slam them together basically right after I posted it.

In your second post, rtcvb32m, your code is very helpful. I would ask just one last thing of you though. While I mostly understand what you've written, code wise, now that I've read a bunch more and just tinkering around with the code, I can't seem to find out how to store the "intro.txt", "ch1.txt" ,ch2.txt", "ch3.txt" into a variable and use it.

For example, if I were to use the "intro.txt" as what shows in the DropDownList, and then after selecting that, I would want to show the "Introductions" in an Edit element just below it.

Again, thank you both so much for helping me.

And since I'm such a beginner at this, I am kinda hoping you'd give me a pretty solid working example and I'd try and learn from that, haha. The documentation gets pretty hard to read after a while, especially being pretty new at this.

#9 rtcvb32

rtcvb32
  • Members
  • 542 posts

Posted 24 March 2012 - 08:35 AM

If the text ones are the dropdown, then change the parts2 to parts1. And If you kept the file in memory, then you use loop and stringSplit to cycle through them. So...

ddSelect:
gui, submit, nohide
loop, parse, file, `n, `r
{
   StringSplit, parts, a_loopField, `,
  if (parts0 == 2 && parts2 = DropDown) { 
    msgbox %parts1%
    break
  }
}
return


#10 Deverex

Deverex
  • Members
  • 10 posts

Posted 24 March 2012 - 06:02 PM

Thank you. I thought for sure I had it working just the way I wanted it to, but now I'm getting a strange error that I hadn't been getting before. And now it's happening with your programs (they were working fine just last night). Any insight would be cool. I've attached a picture of the error.

Posted Image

#11 rtcvb32

rtcvb32
  • Members
  • 542 posts

Posted 24 March 2012 - 06:09 PM

The two ?'s makes me wonder...

Two things to check/ask. Are you using the Ansi or Unicode version of AHK? And if aren't using plain text, what utf/format are you saving it as?

Third, sometimes for whatever reason I notice the script(s) sometimes don't like uneven \r\n references, so I tend to use notepad++, remove all \r's, then replace \n's with \r\n's.

#12 Deverex

Deverex
  • Members
  • 10 posts

Posted 24 March 2012 - 07:09 PM

I'm using the Unicode 32 bit version and I'm using the SciTE4AutoHotkey editor, which saves them as .ahk's.
I replaced all of them, and I'm still getting the same error.

#13 MilesAhead

MilesAhead
  • Members
  • 429 posts

Posted 24 March 2012 - 07:38 PM


It was merely a snippet to show how to determine odd or even lines in a file. I was using Scripting.Dictionary. Since the OP is obviously new I didn't want to get into explaining all that business. But the point about throwing fragments together is valid. One should use the snippets as a jumping off point to read the docs. Maybe to functions one wasn't aware of initially.


Understood and agreed. But sometimes you just need a working example. If he had tried to adapt it rather than throw it all together with it making no sense code-wise then I'm sure part of this wouldn't have come up.

I wonder how many of them are just lazy and Hope we will write it all for them. I'm sure there's hundreds, nay, thousands of fully working examples inside the scripts and functions section of the forum.


Well, sometimes you don't know the name of the function. So I can understand things not being found. But I'm not a believer in writing the program for people unless they are non-programmers. Afa working example it was easier for me to find my snippet than go on a search myself. Some people act like they are paying a consulting fee. I had one guy on another forum who wanted to do a complex task. I gave him references on MS site that showed examples in 4 programming languages. The response amounted to "I can't figure that crap out. Here's what I want. Write it for me." Of course I dropped the thread at that point.

#14 MilesAhead

MilesAhead
  • Members
  • 429 posts

Posted 24 March 2012 - 08:02 PM

I'm using the Unicode 32 bit version and I'm using the SciTE4AutoHotkey editor, which saves them as .ahk's.
I replaced all of them, and I'm still getting the same error.


It's best to post the code that's giving the error now. Not expect the reader to guess how it's changed. Especially with programming if you expect to see something, sometimes you do. That's why programmers used to print stuff out and ask another programmer to look at it. If you expect to see a comma and it's really a period, your eyes might still see a comma.

Note if this is your first programming language it can be a bit of tough sledding. You might want to try something with more Basic like syntax. Or a teaching language like a Pascal based language. It helps if you already know one language and it's generally easier to pick something up that has more functional syntax. For example if ParseFile(filename) then whatever... instead of parse,filename, yadda yadda. The comma syntax and quoted sometimes strings make it tough on beginners(well, makes it tough on me too and I've been programming PCs since 1986. AHK_L helps a lot. But it's still a bit more elusive than most languages.)

#15 Deverex

Deverex
  • Members
  • 10 posts

Posted 24 March 2012 - 09:56 PM

Ah, I thought I mentioned that it was giving me that error with rtcvb32's code as well.
The original code is giving me that error. Though, it was working the first time I tried it.

;FileRead, file, file.txt
;simulation
file =
(
intro.txt, Introductions
ch1.txt, Chapter 1 - The land before time
ch2.txt, Chapter 2 - Oil
ch3.txt, Chapter 3 - Electricity
)

key0 = 0
list =
Loop, parse, file, `n, `r
{
   StringSplit, parts, a_loopField, `,
   if (parts0 == 2) {
;simulated array
;      key0++
;      key%key0% := parts1
      list .= parts2 . "|"
   }
}

;list in a    one|two|three type of format.
Gui, Add, DropDownList, gddSelect vDropdown, %list%
Gui, Show, w200 h200, Test
return

GuiClose:
ExitApp

ddSelect:
gui, submit, nohide
msgbox %DropDown%
return

I modified it a little bit, and was toying around with it to see if I could get the results I wanted, and where's what I ended up with. Eventually I started getting the error on both the code and rtcvb32's, I kept the part where the error said it was originating from the same.

This isn't my first programming language, although it is a bit frustrating for me still. I wanted to use AutoHotkey because figured I could quickly make the program I wanted (which I figured every part out from reading around, except this part).

I am still somewhat stuck in trying to figure this out, so I'll try to clearly state exactly what I want my program to do.
The program I'm trying to make is going to be used to automatically log me into a game I play often. The only problem I'm having is trying to figure out how to grab the account details from a file, which is formatted as such:
Account1,Password1
Account2,Password2
ect...

Thanks to the both of you, I'm able to cycle through the file, and get the account name into the DropDownList, but I'm not sure how to store the password that goes with the account name into a variable so I can use both to log into the game.

Thank you both for what is probably a lot of patience in trying to help me figure this out.