Jump to content
Sign in to follow this  
EmptySpace

Add names/surnames into list using ini..

Recommended Posts

EmptySpace

Hello. All the time I have been working with same things and wanted to try something new. I hope you can understand me.

So i want to make list where I could add names/surnames + delete them from list + to use in future when ill add more things.

Base of code:

(Basic GUI and some nonsenses which I wrote)

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>$i = 0
#Region ### START Koda GUI section ### Form=
GUICreate("Checker v1.0", 396, 442, 192, 124)
$List = GUICtrlCreateList("", 8, 8, 177, 422)
GUICtrlCreateGroup("ADD ACCOUNT", 200, 8, 185, 81)

$addname1 = GUICtrlCreateInput("NAME", 208, 32, 121, 21)
$addsurname1 = GUICtrlCreateInput("SURNAME", 208, 56, 121, 21)
$addnam = GUICtrlCreateButton("ADD", 336, 32, 35, 49)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$delname = GUICtrlCreateButton("DELETE NAME", 200, 104, 187, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $addnam
_addacc()
EndSwitch
WEnd

Func _addacc()
$i+=1
$addname = GUICtrlRead($addname1)

$addsurname = GUICtrlRead($addsurname1)
IniWrite ( @ScriptDir & "\namelist.ini", "NAME", $i, $addname )
IniWrite ( @ScriptDir & "\namelist.ini", "SURNAME", $i, $addsurname )


$1name = IniRead( @ScriptDir & "\namelist.ini", "Name",$i,"default")
GUICtrlSetData($List,$1name)
Endfunc

So its very basic. So now I made something like name adder. I dont need to show surname in list. Firstly I need to make smooth adder which would write in ini and update everytime when i click "ADD" button. + when start program again it would read and write all values which is in ini file.

I need that someone could give me a FISH or FISHING ROD. Its same for me as I would use info all the time and learn it but with a FISH it would be faster at start. Thanks.

Share this post


Link to post
Share on other sites
Mechaflash

Create sections [Last Name] and [First Name]

For each entry, match them to a number. So

IniWrite($sFile, "First Name", "1", "Mecha")
IniWrite($sFile, "Last Name", "1", "Flash")

So then when you query for a matching name, you use the "1" as a simple index.

Does that help?

Derp...

Remove $i = 0 From the top of the code. In the _addacc() function, set

Static Local $i = 0
$i+=1

That way $i will hold its value even after it breaks out of the function.

Edited by Mechaflash

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
EmptySpace

I guess no. When I run program at first I need to add more than 5 names at list. So I need to make new numbers and put it in ini automatically...

[NAME]
1=john
2=Mecha
[SURNAME]
1=jognssurname
2=Flash

and so on...

Make new number for every name. If you use only one number for all then it overwrites last name.... I need to make some kinda func which would read whole ini and search for unused numbers and use them for making new pair of name/last name.

Edited by EdgarT

Share this post


Link to post
Share on other sites
MilesAhead

To associate the names with each other you could combine them when the user hits a Submit button or whatever. Perhaps come up with a method to create a unique key similar to SS#. Something like:

$name = $lastname & "|" & $firstname
$key = CreateUniqueKey($name)

then just write the ini with $key as the key and $name as the value.

Ini files are an easy way until you hit the limit. For a small utility it's fine. But for more than around 32KB of text data you should use a plain text file. Alternating KeyLine/DataLine works for me.

Share this post


Link to post
Share on other sites
EmptySpace

MilesAhead, good idea! Ill try to use this and maybe it will work. But I didint understond your part about "

CreateUniqueKey"

MechaFlash, i think you didnt understood me becouse of my poor english.

Edited by EdgarT

Share this post


Link to post
Share on other sites
Mechaflash

Try this on for size

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ### Form=
GUICreate("Checker v1.0", 396, 442, 192, 124)
$List = GUICtrlCreateList("", 8, 8, 177, 422)
GUICtrlCreateGroup("ADD ACCOUNT", 200, 8, 185, 81)

$addname1 = GUICtrlCreateInput("NAME", 208, 32, 121, 21)
$addsurname1 = GUICtrlCreateInput("SURNAME", 208, 56, 121, 21)
$addnam = GUICtrlCreateButton("ADD", 336, 32, 35, 49)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$delname = GUICtrlCreateButton("DELETE NAME", 200, 104, 187, 25)
$aIni = IniReadSection(@ScriptDir & "namelist.ini", "NAME")
If Not @error Then
    For $i = 1 To $aIni[0][0]
        GUICtrlSetData($list, $aIni[$i][1])
    Next
EndIf
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $addnam
            _addacc()
    EndSwitch
WEnd

Func _addacc()
    $aIni = IniReadSection(@ScriptDir & "namelist.ini", "NAME")
    If @error Then
        Static Local $i = 0
    Else
        Static Local $i = $aIni[$aIni[0][0]][0]
    EndIf
    $i += 1
    $addname = GUICtrlRead($addname1)

    $addsurname = GUICtrlRead($addsurname1)
    IniWrite(@ScriptDir & "namelist.ini", "NAME", $i, $addname)
    IniWrite(@ScriptDir & "namelist.ini", "SURNAME", $i, $addsurname)


    $1name = IniRead(@ScriptDir & "namelist.ini", "Name", $i, "default")
    GUICtrlSetData($List, $1name)
EndFunc   ;==>_addacc

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
EmptySpace

Woot. I wouldnt manage to get this code. I cant learn arrays and for to loop. Now I need to make func which would delete checked name from ini and update list.

Share this post


Link to post
Share on other sites
Mechaflash

What is it that you have a hard time understanding about For... loops and arrays? Maybe I can help explain it better so you can further your script.


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
MilesAhead

MilesAhead, good idea! Ill try to use this and maybe it will work. But I didint understond your part about "

CreateUniqueKey"

In AutoIt it's very using to use the system Scripting.Dictionary. On XP and later it should be there. If you have a unique key then you can combine the first name last name into an array as the data part of the dictionary. Saves on searches. The dictionary does all the work.

These wrapper functions make it easy to create and destroy a dictionary object to use as an Associative Array. It's easy to use.

Inserting values is easy. In this example I use hard coded numbers to create a color list where I can access the colors by name

#include-once
#include 

Func _ColorList()
    Local $colorList = _AssocArray()
    If @error Then
        Return SetError(1, 0, 0)
    EndIf
    $colorList("AntiqueWhite") = 0xFAEBD7
    $colorList("Black") = 0x000000
    $colorList("Blue") = 0x0000FF
    $colorList("Brown") = 0xA52A2A
    $colorList("CadetBlue") = 0x5F9EA0
    $colorList("Chocolate") = 0xD2691E
    $colorList("Coral") = 0xFF7F50
    $colorList("CornflowerBlue") = 0x6495ED
    $colorList("DarkBlue") = 0x00008B
    $colorList("DarkCyan") = 0x008B8B
    $colorList("DodgerBlue") = 0x1E90FF
    $colorList("ForestGreen") = 0x228B22
    $colorList("Gold") = 0xFFD700
    $colorList("Gray") = 0x808080
    $colorList("Green") = 0x008000
    $colorList("HotPink") = 0xFF69B4
    $colorList("LightGreen") = 0x90EE90
    $colorList("LightPink") = 0xFFB6C1
    $colorList("LightSeaGreen") = 0x20B2AA
    $colorList("Lime") = 0x00FF00
    $colorList("Magenta") = 0xFF00FF
    $colorList("Maroon") = 0xB03060
    $colorList("MediumTurquoise") = 0x48D1CC
    $colorList("MediumVioletRed") = 0xC71585
    $colorList("MistyRose") = 0xFFE4E1
    $colorList("Navy") = 0x000080
    $colorList("Olive") = 0x808000
    $colorList("Orchid") = 0xDA70D6
    $colorList("PaleGreen") = 0x98FB98
    $colorList("PaleVioletRed") = 0xDB7093
    $colorList("Pink") = 0xFFC0CB
    $colorList("Plum") = 0xDDA0DD
    $colorList("Purple") = 0x800080
    $colorList("Red") = 0xFF0000
    $colorList("RoyalBlue") = 0x4169E1
    $colorList("Sienna") = 0x00A0522D
    $colorList("Silver") = 0xC0C0C0
    $colorList("SkyBlue") = 0x87CEEB
    $colorList("SteelBlue") = 0x004682B4
    $colorList("Tan") = 0xD2B48C
    $colorList("Teal") = 0x008080
    $colorList("Violet") = 0xEE82EE
    $colorList("Wheat") = 0xF5DEB3
    $colorList("White") = 0xFFFFFF
    $colorList("Yellow") = 0xFFFF00
    Return $colorList
EndFunc   ;==>_ColorList

Func _ColorListDestroy(ByRef $cList)
    Return _AssocArrayDestroy($cList)
EndFunc   ;==>_ColorListDestroy

;set text either white or black to contrast
;with background color
Func _CalcTextColor($bColor)
    If $bColor = 0x00FF00 Then  Return 0x000000
    If $bColor > (0xFFFFFF * 0.9) Then
        Return 0x000000
    Else
        Return 0xFFFFFF
    EndIf
EndFunc   ;==>_CalcTextColor
Edited by MilesAhead

Share this post


Link to post
Share on other sites
EmptySpace

What is it that you have a hard time understanding about For... loops and arrays? Maybe I can help explain it better so you can further your script.

I cant understand them becouse i dont use in my scripts. I prefer to write 50lines with vars when i know that i can write it in 3lines using arrays.

Share this post


Link to post
Share on other sites
Mechaflash

I think every project I've created utilizes For loops and arrays MANY times to process data.

Think of a simple 1D array as a variable with a number.

Local $var1, $var2
$var1 = "one" ; Tomato
$var2 = "two" ; Tomato
;It can be created in two ways
;Local $var[3]
;$var[0] = "" ; Leave the 0 index as a blank value.
;$var[1] = "one" ; Tomâto
;$var[2] = "two" ; Tomâto
;Or
Local $var[3] = [0, "one", "two"]

However, if you want to iterate over both doing it with arrays is MUUUUUUCCCHHHH easier.

With arrays, it's as simple as

For $i = 1 To 2
  ConsoleWrite($var[$i] & @CRLF)
Next

Then you have the case of a 2D array. It allows you the ability to group information in a simple, easier to manage way.

So let's say we need to store a recipe, and let's say we want information such as "Name of recipe", "Cooking Time", then "Ingredients". We can store ingredients as CSVs since it can be unknown what the maximum number of ingredients will ever be for a newer recipe.

Here's a cute little program for ya to play with.

#include <Array.au3>
Global $aRecipes[1][3] = [["Grandma's Goulash",20,"Small Shell Pasta,Tomato,Onion,Pasta Sauce,Salt,Extra Virgin Olive Oil"]]

_ArrayDisplay($aRecipes)

While 1
    $sInput = InputBox("Recipe Script", "Which recipe do you want to show?", "Grandma's Goulash")
    If $sInput <> "Grandma's Goulash" Then
        msgbox(16 + 262144, "Bad Boy", "Just choose Grandma's Goulash. It may not taste great, but this is just an example ;)")
        ContinueLoop
    Else
        ExitLoop
    EndIf
WEnd

_ShowRecipe($sInput)

Func _ShowRecipe(Const $sRecipe)
    Local $sMsg = "", $aIngredients
    $iIndex = _ArraySearch($aRecipes, $sRecipe)
    For $i = 0 To 2
        Switch $i
            Case 0
                $sMsg&= "Recipe Name: " & $aRecipes[$iIndex][$i] & @CRLF
            Case 1
                $sMsg&= "Cooking Time: " & $aRecipes[$iIndex][$i] & " Minutes" & @CRLF
            Case 2
                $aIngredients = StringSplit($aRecipes[$iIndex][$i], ",")
                $sMsg&= " Ingredients: " & @CRLF
                For $i = 1 To $aIngredients[0]
                    $sMsg&= "   -" & $aIngredients[$i] & @CRLF
                Next
        EndSwitch
    Next
    msgbox(0,"",$sMsg)
EndFunc
Edited by Mechaflash

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
EmptySpace

Thanks! It helped me to understand few things but I need to start using arrays from basic.

Func _ShowRecipe(Const $sRecipe) ;why need to declare "const" ?

Hmm, I think you wont teach me using arrays but maybe you know some good tutorials? I want to try to make basic script using it. I still find it difficult to understand them. I would better do that program with normal variables and while loop... Scripts would be about 100 lines lol

btw what @CRLF do?

Share this post


Link to post
Share on other sites
Mechaflash

Const keeps $sRecipe from being changed inside the function. It's a habit of mine to declare Const/ByRef for security purposes.

AutoIt Wiki

@CRLF creates a "Carriage Return Line Feed" character. Basically it's a line break. So when creating a variable, I can make it multi-line:

$sMsg = "Hello!" & @CRLF & "My name is:" & @CRLF & "Mechaflash"
msgbox(0,"",$sMsg)

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites
EmptySpace

All clear. I will try to do some exercises to get known with it.

Share this post


Link to post
Share on other sites
Mechaflash

Since I made that little recipe program... I started expanding on it. Damn you edgar for side-tracking me lol


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×