Sign in to follow this  
Followers 0

#include $variablename

15 posts in this topic

Posted

Hi,

Since #include "filename" doesn't allow filename to be a variable, I was wondering if there is any workaround to pass a string variable to #include directive, instead of coding every single file to be included.

Thanks in advance.

Share this post


Link to post
Share on other sites



Posted

No...I believe. Why?

Share this post


Link to post
Share on other sites

Posted

FileInstall() is another route

8)

Share this post


Link to post
Share on other sites

Posted

FileInstall() doesn't like variables either. I am working on a code which includes a particular piece of code based on the function input. But since, I can't use a variable with #include, i am forced to writing each and every single includefile names in the code, which is stupid, long and inflexible.

Is there any other workaround for that??

Share this post


Link to post
Share on other sites

Posted (edited)

Either I'm not understanding what you need or I don't think you understand includes. Includes are included in the script from the start of the script to make sure the end user has all the functions the script needs. It has to be this way because it is not like the includes are being polled in the loop based on a function being called, or the end user of a compiled script are going to have all the necessary include files that can be called based on input. Besides that, only the functions being called in the script will be included when you compile your script...I believe. Your other option would be to not include the files and just copy the needed functions from the include files into your script....which is redundant because only the needed functions will be included anyway at compile time.

I don't see the tedium of including a few files at the top of your script....are you including every single file? And even so, it is still a simple one time step. Just my opinion.

Edited by Champak

Share this post


Link to post
Share on other sites

Posted (edited)

well.. that is one of the options, but for my script i wanted it not to load the functions in advance and include them as and when needed, somewhat like dlls but i was thinking to implement #include, since if we do a #include in the middle of the code, it executes that included code and then continues to the rest of the program.

But I guess its not possible to use a variable there, so no point wasting time in that direction. Thanks to all you for replying and clearing up my doubts.

here is my finished script implementing #includes:

Global $nMsg = 10
Global $lessonArray[$nMsg]
For $n=0 To $nMsg-1
	$lessonArray[$n]=0
Next

Func lesson($RandomNo)
$flag=1
	For $n = 0 To $nMsg-1
		If $lessonArray[$n]=$RandomNo And $lessonArray[$nMsg-1]=0 Then
			$flag=2
		EndIf
		If $lessonArray[$n]==0 And $flag==1 Then
			$lessonArray[$n]=$RandomNo
			$flag=0
		Switch $RandomNo
			Case 1
				#include "L1.AU3"
			Case 2
				#include "L2.AU3"
			Case 3
				#include "L3.AU3"
			Case 4
				#include "L4.AU3"
			Case 5
				#include "L5.AU3"
			Case 6
				#include "L6.AU3"
			Case 7
				#include "L7.AU3"
			Case 8
				#include "L8.AU3"
			Case 9
				#include "L9.AU3"
			Case 10
				#include "L10.AU3"
			Case Else
				Exit 0
		EndSwitch
		EndIf
	Next
If $flag=1 Then
	Return 0
Else
	Return 1
EndIf
EndFunc

While  lesson(Random(1,$nMsg,1))
	Sleep(100)
WEnd
Edited by ankitstr

Share this post


Link to post
Share on other sites

Posted

Here is one way to do this:

#include <GUIConstantsEx.au3>

$sIncludeFiles = "MyIncl_1.au3;MyIncl_2.au3;MyIncl_3.au3"
_IncludeScripts($sIncludeFiles, False) ;Set $iForCompile (last param) as True to write the includes without removing them

$GUI = GUICreate("Test Script", 300, 200)

$Button = GUICtrlCreateButton("Button", 20, 40, 60, 20)

GUISetState(@SW_SHOW, $GUI)

Call("Test") ;Some function from MyIncl_N.au3

While 1
	Switch GUIGetMsg()
		Case $GUI_EVENT_CLOSE
			Exit
	EndSwitch
WEnd

Func _IncludeScripts($sIncludeScripts, $iForCompile=False)
	If @Compiled Then Return ;We can't write to compiled script!
	
	Local $sIncludesContent = "", $sCurrInclContent = ""
	Local $iIncludeMode = StringInStr($CmdLineRaw, "/Include")
	Local $aSplit_Includes = StringSplit($sIncludeScripts, ";")
	Local $sReadScript = FileRead(@ScriptFullPath)
	
	If $iForCompile And $iIncludeMode Then Return
	
	For $i = 1 To UBound($aSplit_Includes)-1
		$sCurrInclContent = "#include <" & $aSplit_Includes[$i] & ">"
		
		If Not FileExists($aSplit_Includes[$i]) Or _
			(Not $iIncludeMode And StringInStr($sReadScript, $sCurrInclContent)) Then ContinueLoop
		
		$sIncludesContent &= $sCurrInclContent & @CRLF
	Next
	
	If $sIncludesContent <> "" Then
		Local $hFOpen = FileOpen(@ScriptFullPath, 2)
		
		If Not $iIncludeMode Then
			FileWrite($hFOpen, $sIncludesContent & $sReadScript)
		Else
			FileWrite($hFOpen, StringRegExpReplace($sReadScript, "(?s)\A" & $sIncludesContent & "(\r\n+|)(.*?)$", "\2"))
		EndIf
		
		FileClose($hFOpen)
	EndIf
	
	If $iIncludeMode Then Return
	
	Run(@AutoItExe & ' /AutoIt3ExecuteScript "' & @ScriptFullPath & '" /Include')
	Exit
EndFunc

The idea here is to write temporary includes to the script (can be changed), execute the script again with those includes, and remove them from the script. To compile the script with the includes just set last parameter in the _IncludeScripts() function to True, run once the script, and then you can compile it.

Btw, the same can be done for FileInstall() limits :)

Share this post


Link to post
Share on other sites

Posted (edited)

this is interesting, but can these #includes be added at a line of code, like an interrupt? or in a way that the script doesn't run itself over again, and continue from where we left it after the function call, not just at the top?

Thanks for your post, its really helpful.

Edited by ankitstr

Share this post


Link to post
Share on other sites

Posted

Includes are the same as copying the file right into the executing script where the #include directive is. It will execute any file level code and load any functions contained as well. They aren't meant to be dynamic. Just like #include in C++. It's a static directive.

Share this post


Link to post
Share on other sites

Posted

@richard: well.. would have been nice if they were, anyways,

@MrCreator: I was thinking if there was some command to jump to a particular line of code, then that could be manipulated to continue the program from where it left after doing the teporary include.

Share this post


Link to post
Share on other sites

Posted

can these #includes be added at a line of code, like an interrupt? or in a way that the script doesn't run itself over again, and continue from where we left it after the function call, not just at the top?

To certain line, yes it can. But without runing again no, we can not execute script from specific line.

But as i understood, you only need to execute part of code (that included in seperate script) at particular point, so why you not just Execute() it? :)

Global $nMsg = 10
Global $lessonArray[$nMsg]

For $n = 0 To $nMsg - 1
	$lessonArray[$n] = 0
Next

While Lesson(Random(1, $nMsg, 1))
	Sleep(100)
WEnd

Func Lesson($RandomNo)
	$flag = 1
	
	For $n = 0 To $nMsg - 1
		If $lessonArray[$n] = $RandomNo And $lessonArray[$nMsg - 1] = 0 Then $flag = 2

		If $lessonArray[$n] == 0 And $flag == 1 Then
			$lessonArray[$n] = $RandomNo
			$flag = 0
			$RandomNo = 3
			Switch $RandomNo
				Case 1 To 10
					$sReadIncl = FileRead("L" & $RandomNo & ".au3")
					_ExecuteCode($sReadIncl)
				Case Else
					Exit 0
			EndSwitch
		EndIf
	Next
	
	If $flag = 1 Then Return 0
	
	Return 1
EndFunc

Func _ExecuteCode($sCode)
	$aExecuteCode = StringSplit(StringStripCR($sCode), @LF)
	
	For $i = 1 To $aExecuteCode[0]
		If $aExecuteCode[$i] <> "" Then Execute($aExecuteCode[$i])
	Next
EndFunc

Share this post


Link to post
Share on other sites

Posted (edited)

You seem to use #include directive where functions should be used.

Make one include file: lessons.au3 with functions L1(), L2(), L3(), ...

and then use:

#include "lessons.au3"

...
Switch $RandomNo
  Case 1
	L1()
  Case 2
	L2()
...
Edited by Zedna

Share this post


Link to post
Share on other sites

Posted

I used a zip file for Welcome to Autoit 1-2-3 to "include" all of the lessons

8)

Share this post


Link to post
Share on other sites

Posted

On the general topic of variables in #include or FileInstall() source lines, remember that has to be parsed and interpreted at COMPILE-TIME, not at RUN-TIME.

What if you had a different include file per user and tried: #include @UserName & "_Stuff.au3"

Or, perhaps: FileInstall("C:\Temp\" & @UserName & "_Stuff.txt", @ScriptDir & "\Stuff.txt")

How would the compiler know who @UserName was supposed to be? (The @ScriptDir above would be parsed at Run-Time and is okay.)

I also wrote a more lengthy example with #include for the modular lessons idea here.

:)

Share this post


Link to post
Share on other sites

Posted

On the general topic of variables in #include or FileInstall() source lines, remember that has to be parsed and interpreted at COMPILE-TIME, not at RUN-TIME

Yes, it would be better...

How would the compiler know who @UserName was supposed to be?

Well, at run time we just write this name literally to the script (with my first method) :) .

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0