Help creating a number of variables without miles of code... Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
perhapslevi
Posts: 3
Joined: 07 Aug 2022, 18:29

Help creating a number of variables without miles of code...

Post by perhapslevi » 07 Aug 2022, 20:01

Hi there,

I'm trying to create a little tool to help make .csv files based on user input.
I have ~40 edit controls, where the user inputs ID numbers for staff. These write to variables %Id1%, %Id2%, %Id3% . . . %Id38%, %Id39%, %Id40%.
Then I have a section of the script which creates a .csv file like this:

Code: Select all

FileAppend,
(
%FirstName1%,%LastName1%,%Id1%
%FirstName2%,%LastName2%,%Id2%
%FirstName3%,%LastName3%,%Id3%
. . .
%FirstName38%,%LastName38%,%Id38%
%FirstName39%,%LastName39%,%Id39%
%FirstName40%,%LastName40%,%Id40%
), %Filename%.csv
This all seems to work fine, and I'm sure it's not the most elegant way of accomplishing this, but it works...

What I'm now trying to do is add a third variable using the ID numbers, to create email addresses. This will be a case of adding @emailaddress.com to the end of each ID variable, however I've encountered two issues with this.

First of all, from what I can tell, I would have to process each ID variable separately with something like this:

Code: Select all

Email1 := Id1 "@emailaddress.com"
Email2 := Id2 "@emailaddress.com"
Email3 := Id3 "@emailaddress.com"
. . .
Email38 := Id38 "@emailaddress.com"
Email39 := Id39 "@emailaddress.com"
Email40 := Id40 "@emailaddress.com"
The second issue this presents is that it effects all variables including those that are empty, meaning that any fields left blank will appear in the .csv as just "@emailaddress.com"

I'm aware that I can fix this by doing something like

Code: Select all

if (Id1 != "")
	Email1 := Id1 "@emailaddress.com"
if (Id2 != "")
	Email2 := Id2 "@emailaddress.com"
if (Id3 != "")
	Email3 := Id3 "@emailaddress.com"
. . .
if (Id38 != "")
	Email38 := Id38 "@emailaddress.com"
if (Id39 != "")
	Email39 := Id39 "@emailaddress.com"
if (Id40 != "")
	Email40 := Id40 "@emailaddress.com"
But this all feels very clunky...
Is there a way to write the script more simply so that every Id# variable gets appended with the email address if its not empty? I feel like there should be a way to do this with a loop? But unfortunately I don't know enough about what I'm doing to know where to really start.

Please let me know if I haven't explained anything properly!
Thanks in advance :)

AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Help creating a number of variables without miles of code...

Post by AHKStudent » 08 Aug 2022, 00:21

How will the GUI look, like this?

Code: Select all

[First]  [Last]  [ID]  [Email]
[First]  [Last]  [ID]  [Email]
[First]  [Last]  [ID]  [Email]
[Submit]
or

Code: Select all

[First]  [Last]  [ID]  [Email]
[Submit]
If it's the first one, you can use a loop, the second one you just append to the csv each time or you keep appending to a variable and then have a save button. The latter is better if there will b a lot of data posting.

Email addresses that are empty have to have a , or the csv will not work correctly unless its the last field.

If I know how the GUI looks it will be easier to help.

perhapslevi
Posts: 3
Joined: 07 Aug 2022, 18:29

Re: Help creating a number of variables without miles of code...

Post by perhapslevi » 08 Aug 2022, 00:56

Thanks for the reply - I probably could have explained it all a bit better...

The GUI looks something like this:

[First] [Last] [Id]
[First] [Last] [Id]
[First] [Last] [Id]
...etc.

There are 40 controls for first names, 40 for last names, and 40 for IDs.

Rather than have the user input the email addresses manually, my plan was to create the email variables using the ID variables, since all emails are [ID]@etc.com.

Then, the .csv is generated later with a file append like this:

Code: Select all

FileAppend,
(
%FirstName1%,%LastName1%,%Id1%,%Email1%
%FirstName2%,%LastName2%,%Id2%,%Email2%
%FirstName3%,%Lastname3%,%Id3%,%Email3%
. . .
%Firstname38%,%Lastname38%,%Id38%,%Email38%
%Firstname39%,%Lastname39%,%Id39%,%Email39%
%Firstname40%,%Lastname40%,%Id40%,%Email40%
), %Filename%.csv
The .csv works fine, and I presume I can create the variables for the emails the way I suggested above, but I'm hoping for a way to effectively target all of the variables that begin with "ID", and add a string (which would be the "@etc.com" part) to the ones that aren't empty in order to create a respective Email variable for each.

I hope that explains my question a bit better!

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Help creating a number of variables without miles of code...

Post by BoBo » 08 Aug 2022, 01:53

Code: Select all

#SingleInstance, Force
SetWorkingDir, A_ScriptDir

fileName := "my.csv"
email    := "@company.com"

Gui -caption +border
Gui, Add, Text,   x10      y10      w100                    , First Name
Gui, Add, Edit,   xp+110   yp       w100 vfirstName         , 
Gui, Add, Text,   x10      yp+22    w100                    , Last Name
Gui, Add, Edit,   xp+110   yp       w100 vlastName          , 
Gui, Add, Text,   x10      yp+22    w100                    , ID
Gui, Add, Edit,   xp+110   yp       w100 vid                ,
Gui, Add, Button, x10      yp+22    w100  default  gSubmit  , Submit
Gui, Add, Button, xp+109   yp       w102           gReset   , Create File
Gui, Show, h108, 
return

Submit:
   Gui, Submit, NoHide                                                                                               ;get access to all variables content
   ToolTip,% content .= """" firstName """;""" lastName """;""" id """;""" ((id != "") ? id . email """`n" : """`n") ;create CSV line
   GuiControl,, firstName                                                                                            ;reset edit field
   GuiControl,, lastName
   GuiControl,, id
   GuiControl, Focus, firstName                                                                                      ;reset input focus to first edit control
   return

Reset:
;  FileAppend,% content,% fileName                                                                                   ;append/write content to file (disabled)
   MsgBox % content                                                                                                  ;show content while testing
   ExitApp
It's preferable you post your GUI code so your supporters won't have to create something out of the blue for testing/to match your settings. HTH

This GUI is working with a single set of edit controls. Once you press 'Submit' you'll see a ToolTip (at your mouse position) that shows your previous entry.
Consecutive entries will be appended. If you press 'Create File' the created list of lines will be appended/written to a file. Well, while testing a MsgBox appears instead of that. Simply uncomment the 'FileAppend,...' command and remove the 'MsgBox' after testing.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Help creating a number of variables without miles of code...  Topic is solved

Post by Xtra » 08 Aug 2022, 03:48

But this all feels very clunky...
When you have a lot of repeat code writing a function simplifies it.

Code: Select all

Email1 := EmailAddress(Id1)
Email2 := EmailAddress(Id2)
Email3 := EmailAddress(Id3)

EmailAddress(ID) {
    return ID ? ID "@emailaddress.com" : ""
}
Since your variables are named with sequential numbers you can do this:

Code: Select all

Loop, 40
    Email%A_Index% := EmailAddress(Id%A_Index%)

perhapslevi
Posts: 3
Joined: 07 Aug 2022, 18:29

Re: Help creating a number of variables without miles of code...

Post by perhapslevi » 09 Aug 2022, 18:58

Xtra wrote:
08 Aug 2022, 03:48
But this all feels very clunky...
When you have a lot of repeat code writing a function simplifies it.

Code: Select all

Email1 := EmailAddress(Id1)
Email2 := EmailAddress(Id2)
Email3 := EmailAddress(Id3)

EmailAddress(ID) {
    return ID ? ID "@emailaddress.com" : ""
}
Since your variables are named with sequential numbers you can do this:

Code: Select all

Loop, 40
    Email%A_Index% := EmailAddress(Id%A_Index%)
This is exactly what I was looking for! It didn't occur to me that I could use the %A_Index% like that. Thanks so much :)

Post Reply

Return to “Ask for Help (v1)”