Data from File / Variable - Use the same function()

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Data from File / Variable - Use the same function()

20 Feb 2018, 11:14

Sometimes data is retrieved from a file and sometimes from a variable.
Now I have two functions, but is it possible to use the same function?

Example of a datafile Test.csv

Code: Select all

"Field1";"Field2";"Field3"
"aaa";"bbb";"ccc"
and the program (if I want to read data from a file)

Code: Select all

InputFile = Test.csv
Result := ReadFile(InputFile)
MsgBox %Result%

ReadFile(FileName)
{	Loop Read, %FileName%
		MsgBox % A_LoopReadLine
	Return "Result from File"
}
And if I want to read data from a variable, I have to have another function similar to this .:

Code: Select all

TestValue =
(LTrim Join
	"Field11";"Field12";"Field13"`n
	"AAA";"BBB";"CCC"
)
Result1 := ReadData(TestValue)
MsgBox %Result1%

ReadData(Data)
{	Loop Parse, Data, `n
		MsgBox % A_LoopField
	Return "Result from Data"
}
The only thing that separates these methods is how data is read and the variable name.
For a file, this is used .: Loop Read,... and A_LoopReadLine and
for Data, this is used .: Loop Parse,... and A_LoopField
Is it possible to do both thing with the same function?
User avatar
boiler
Posts: 17051
Joined: 21 Dec 2014, 02:44

Re: Data from File / Variable - Use the same function()

20 Feb 2018, 11:21

Sure. Just use the parameter as either the data or the file name, and use a second parameter to indicate which it is. Then in the function run the appropriate lines of code based on the setting of the second parameter. I don't see how that really helps since you still need separate lines of code since one reads from a file and one parses a variable. Those can't be made to be the same.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Data from File / Variable - Use the same function()

20 Feb 2018, 12:19

Code: Select all

text := "Field11,Field12,Field13"
MsgBox % var := showData(text)
FileRead, content, my.csv
MsgBox % var := showData(content)

showData(var) {
   Return var
   }
Not tested.
User avatar
boiler
Posts: 17051
Joined: 21 Dec 2014, 02:44

Re: Data from File / Variable - Use the same function()

20 Feb 2018, 13:33

@BoBo: What value is a function that returns does nothing but return what it was sent?
Albireo
Posts: 1756
Joined: 16 Oct 2013, 13:53

Re: Data from File / Variable - Use the same function()

20 Feb 2018, 14:02

Maybe this is the only way to solve this on??

Code: Select all

#NoEnv
#SingleInstance	force

TestValue =
(LTrim Join
	"Field11";"Field12";"Field13"`n
	"AAA";"BBB";"CCC"
)
InputFile = Test.csv

Result := ReadData(InputFile, "File")
MsgBox 0, Row.: %A_LineNumber% -> %A_ScriptName%, From File .: %Result%
Result := ReadData(TestValue, "Data")
MsgBox 0, Row.: %A_LineNumber% -> %A_ScriptName%, From Data .: %Result%

ExitApp
; -._.- -._.- -._.- -._.- -._.-


ReadData(InputData, DataType)
{	If ( DataType = "File" )	; PartFile
	{	Loop Read, %InputData%
		{	LineInfo := A_LoopReadLine
			Gosub ReadDataSub
		}
		Return "Result from File"
	}
	else	; Part Data
	{	Loop Parse, InputData, `n
		{	LineInfo := A_LoopField
			Gosub ReadDataSub
		}
		Return "Result from Data"
	}

	ReadDataSub:
		MsgBox 0, Row.: %A_LineNumber% -> %A_ScriptName%, % "DataType .: " DataType "`nLineInfo .: " LineInfo
	Return	; Return from sub	
}
User avatar
boiler
Posts: 17051
Joined: 21 Dec 2014, 02:44

Re: Data from File / Variable - Use the same function()

20 Feb 2018, 14:05

Yes, that's what I was thinking. Not sure it's better than two functions, but if you want one, that's a way to do it.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mikeyww, Sarhad and 303 guests