I was chatting with Maestrith the other day and he saw a couple of lines of code in one of my scripts that he thought could be optimized. I decided to record the session because I think it is a great way for people to learn by watching different approaches on how to solve a coding problem. Below is the code demonstrated in the video. I’ve also uploaded the large “baby names” file in case you wish to test the code.
Code demonstrated in the Video
/* Tick:=A_TickCount FileRead,Text,%A_AhkPath%\..\License.txt m(A_TickCount-Tick) */ FileName:="I:\Users\maest_mcwl8s3\Dropbox\Joe\baby-names2.csv" Tick:=A_TickCount SetBatchLines,-1 if(){ Obj1:=[],aa:=0 Loop,Read,%FileName% { aa++ ;increment rows- Need to know what the last row is /* Text.=A_LoopReadLine "`r`n" Obj1.Push(A_LoopReadLine) */ } Random,Random,1,%aa% FileReadLine,Text,%FileName%,%Random% m(A_TickCount-Tick) ExitApp }else if(){ FileRead,Text,%FileName% }else{ File:=FileOpen(FileName,"R","UTF-8"),Text:=File.Read(),File.Close() Random,Random,1,% (Obj:=StrSplit(Text,"`n","`r`n")).MaxIndex() m(A_TickCount-Tick,Trim(Obj[Random],"`t"),Obj.MaxIndex()) } if(){ Random,Random,1,% Obj1.MaxIndex() m(Trim(Obj1[Random],"`t")) }