Level-up your programming by having a genius refactor your code!

Level-Up

While I’m making progress using objects in my code, I knew there was a more efficient way to write this but couldn’t put my finger on it. I asked Maestrith to take my code and “Maestrithize it”. (It’s humbling to watch someone do this. I feel like I had NASA do my science project) lol   You can help Maestrith out here.

Remember, we are all at different stages of our skills & abilities.  There’s nothing wrong to admit that someone is superior to you at something.  Just keep plugging-along and improve!

Level-up your programming by having a genius refactor your code!

Here’s the finished code from Level-up your programming by having a genius refactor your code!


#Include  
;**************************************
#SingleInstance,Force
SetBatchLines,-1 ;Set run w/o any sleeping between things
#MaxMem,4095 ;Increase the variable allowance to max just in case a massive file
;***********Get row count of file and display headers*******************
Path:= Explorer_GetSelected() ;Explorer Function that gets paths of selected files
If (Path="ERROR") ;Didn't get file, just stop moving forward
return
;~ Folder to use for examples "\Desktop\Header example file\"
SplitPath,Path,File_Name ;grab the file name w/o the path
Loop, read, % Path
last_line++ ;increment lines

rows:=RegExReplace(last_line,"(?:^[^1-9.]*[1-9]\d{0,2}|(?<=.)\G\d{3})(?=(?:\d{3})+(?:\D|$))", "$0,") ;Add commas to the number
MsgBox,4,, % rows " lines found in: " File_Name "`n`nDisplay headers?" ;Display rows and ask if want to see headers
IfMsgBox Yes
{
FileReadLine,Header, % Path, 1 ;read first row
;~ you have to wrap in () any Key in a Key/Value pair if it is a variable.
;~ other than that it's just creating a Key/Value pair on the Count and {Name:"Comma",Character:","}
Obj:={(StrSplit(Header,",").count() -1):{Name:"Comma",Character:","},(StrSplit(Header,"|").count() -1):{Name:"Pipe",Character:"|"},(StrSplit(Header,"`t").count() -1):{Name:"Tab",Character:"`t"}}
Item:=Obj[Obj.MaxIndex()]
MsgBox,,% "The delimmiters are probaly a " Item.Name ,% StrReplace(Header,Item.Character,"`n" Item["Character"])
return
}

Nesting an Object within an Object in AutoHotkey

During the webinar yesterday someone mentioned working with Multi-dimensional arrays. I thought I’d go ahead and post my template of nesting objects within objects and talk through it in the hopes that it helps.

Here are a list of the functions I used:

ExploreObj from Learning one

ObjectToString from Capn Odin

m() from Maestrith (Author of AHK Studio)

Nesting an Object within an Object in AutoHotkey

Here is the code that I work through Nesting an Object within an Object in AutoHotkey

#SingleInstance,Force
var=
(
Email First_Name Last_Name Phone
[email protected] Joy Senilg 123.456.7801
[email protected] Joe Glines 234.654.4321
[email protected] Jon Seling 567.890.6543
[email protected] Jon Seling 345.234.1322
)
obj:={} ;Need to declare object first
;*********Use For loop over Var going line by line*********************
for i, row in Loopobj:=StrSplit(var,”`n”,”`r`n”) { ;Parse Var on each new line
IfEqual, i,1,continue ;Skip header row
;~ MsgBox % StrSplit(row,”`t”).1
Obj[StrSplit(row,”`t”).1]:={} ;Need to instanciate the object under the key so you can stuff data into it
Obj[StrSplit(row,”`t”).1].First_Name :=StrSplit(row,”`t”).2
Obj[StrSplit(row,”`t”).1].Last_Name :=StrSplit(row,”`t”).3
Obj[StrSplit(row,”`t”).1].Phone :=StrSplit(row,”`t”).4
}
MsgBox % Obj[“[email protected]”].Phone
m(obj)
;~ DebugWindow(“`n______Object to String____________`n” ObjectToString(Obj),1,1,0,0) ;show content
;~ DebugWindow(“`n______Explore Object____________`n” ExploreObj(Obj),0,1,400,0) ;show structure
;~ return

;********************Use a for loop to loop over all 2nd-level objects contained in OBJ***********************************
for k, v in Obj
data.= k a_tab v.First_name a_tab v.Last_Name a_tab v.Phone “`n”

DebugWindow(“`n______FOR LOOP____________`n” data,0,1,100,0)