Jump to content


Photo

Applying (or not applying) TAB after a variable


  • Please log in to reply
4 replies to this topic

#1 MedX172

MedX172
  • Members
  • 23 posts

Posted 19 June 2012 - 09:25 PM

I'm creating a large script that has numerous variables, that is going to input information into a system. I've written the parsing portion of the script as follows:

FileName = S:\Pwlis\AutoHotKey\Scripts\TEST\P-Data.csv
FileRead, File, %FileName% 
loop, Parse, File, `n, `r
 { 
 	IfNotInString, A_LoopField, `, 
  	Continue 
 	StringSplit,Temp, A_LoopField, `, 
	PrintNumber = %Temp1%
	Mnemonic = %Temp2%
	AltMnemonic = %Temp3%
	Name = %Temp4%
	Abbrev = %Temp5%
	Type = %Temp6%
	Container = %Temp7%
	RefCode = %Temp8%
	ResCode = %Temp9%
	Charge = %Temp10%
	BillingCode = %Temp11%
	CPTCode = %Temp12%
	EMRID = %Temp13%
	CollectInst = %Temp14%
	TMem1 = %Temp15%
	TMem2 = %Temp16%
	TMem3 = %Temp17%
	TMem4 = %Temp18%
	TMem5 = %Temp19%
	TMem6 = %Temp20%
	TMem7 = %Temp21%
	TMem8 = %Temp22%
	TMem9 = %Temp23%
	TMem10 = %Temp24%
	TMem11 = %Temp25%
	TMem12 = %Temp26%
	TMem13 = %Temp27%
	TMem14 = %Temp28%

 	Gosub, Pbuild
	
 }
Return

The problem arises when I get to the "TMem" variables. The system requires a {TAB} if the variable is present, but doesn't if it's a blank field. There can be up to 14 entries in the system. Some entries have 2 of the 14 Tmem variables and others have the full 14. When I get to this point, I have problems:

Send %TMem1%{TAB}
Send %TMem2%{TAB}
Send %TMem3%{TAB}
Send %TMem4%{TAB}
Send %TMem5%{TAB}
Send %TMem6%{TAB}
Send %TMem7%{TAB}
Send %TMem8%{TAB}
Send %TMem9%{TAB}
Send %TMem10%{TAB}
Send %TMem11%{TAB}
Send %TMem12%{TAB}
Send %TMem13%{TAB}
Send %TMem14%{TAB}

After each variable, AHK sends a {TAB}. I realize that in the initial setup of the script, I tell the parsing to "IfNotInString, A_LoopField, `,
Continue" so it's continuing on to the next entry {TAB}. How can I construct the script in such a way to only have the {TAB} occur if there's something populating that variable in the CSV field, and not do it if it's blank?

Thank You in advance!

#2 0x150--ISO

0x150--ISO
  • Members
  • 657 posts

Posted 19 June 2012 - 10:45 PM

This will condense the 14 sends into 2 lines plus if the variable is not blank it will include a tab.
Loop 14

  Send % (t:=TMem%A_Index%) ( t ? A_Tab : "" )
:?:

#3 MedX172

MedX172
  • Members
  • 23 posts

Posted 20 June 2012 - 05:06 PM

Wow, thanks 0x150||ISO! :)

I'll test and report back. Can you explain to me how the statement and syntax work? I've never seen anything like that before! :shock:

#4 0x150--ISO

0x150--ISO
  • Members
  • 657 posts

Posted 20 June 2012 - 05:19 PM

The 1st part of the expression (t:=TMem%A_Index%),
assigns the variable content in the pseudo array TMem to t
and Sends the contents of the current array.
The 2nd part is a condition( t ? A_Tab : "" ),
if t is not false ( or blank ) append A_Tab ( or tab ) to the entire Send.

It folds out to:
Loop 14 {
  t:=TMem%A_Index%
  If ( t )
    Send %t%{TAB}
  Else
    Send %t%
}
I assigned TMem to t so that I wouldnt have to repeat TMem.
Send % TMem%A_Index% ( TMem%A_Index% ? A_Tab : "" )

#5 MedX172

MedX172
  • Members
  • 23 posts

Posted 20 June 2012 - 08:54 PM

It worked beautifully in the application. Thanks for your help and the education lesson.