Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

#include $variablename


  • Please log in to reply
14 replies to this topic

#1 ankitstr

ankitstr

    Seeker

  • Active Members
  • 37 posts

Posted 20 September 2008 - 01:35 AM

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.







#2 Champak

Champak

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,043 posts

Posted 20 September 2008 - 01:41 AM

No...I believe. Why?

#3 Valuater

Valuater

    www.PayFreeWireless.com

  • MVPs
  • 11,214 posts

Posted 20 September 2008 - 01:44 AM

FileInstall() is another route


8)

Posted Image

Clic The Pic!!!


#4 ankitstr

ankitstr

    Seeker

  • Active Members
  • 37 posts

Posted 20 September 2008 - 01:58 AM

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??

#5 Champak

Champak

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,043 posts

Posted 20 September 2008 - 02:07 AM

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, 20 September 2008 - 02:19 AM.


#6 ankitstr

ankitstr

    Seeker

  • Active Members
  • 37 posts

Posted 20 September 2008 - 02:34 AM

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:

Plain Text         
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, 20 September 2008 - 02:35 AM.


#7 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,251 posts

Posted 20 September 2008 - 03:26 AM

Here is one way to do this:

AutoIt         
#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 :)
Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

Posted Image AutoIt Russian CommunityPosted Image Projects: ATT - Application Translate Tool [new] | BlockIt - Block files & folders [new] | SIP - Selected Image Preview [new] | SISCABMAN - SciTE Abbreviations Manager [new] | AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramPosted Image UDFs: OnAutoItErrorRegister - Handle AutoIt critical errors [new] | AutoIt Syntax Highlight [new] | Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDFPosted Image Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation DemoLike the examples/UDFs? Please rate the topic (up-right corner of the post header: Rating Posted Image)* === My topics === *

==========================================================Posted Image==========================================================

AutoIt is simple, subtle, elegant. © AutoIt Team


#8 ankitstr

ankitstr

    Seeker

  • Active Members
  • 37 posts

Posted 20 September 2008 - 03:44 AM

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, 20 September 2008 - 03:46 AM.


#9 Richard Robertson

Richard Robertson

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 10,333 posts

Posted 20 September 2008 - 03:48 AM

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.

#10 ankitstr

ankitstr

    Seeker

  • Active Members
  • 37 posts

Posted 20 September 2008 - 03:57 AM

@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.

#11 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,251 posts

Posted 20 September 2008 - 04:16 AM

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? :)

AutoIt         
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

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

Posted Image AutoIt Russian CommunityPosted Image Projects: ATT - Application Translate Tool [new] | BlockIt - Block files & folders [new] | SIP - Selected Image Preview [new] | SISCABMAN - SciTE Abbreviations Manager [new] | AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramPosted Image UDFs: OnAutoItErrorRegister - Handle AutoIt critical errors [new] | AutoIt Syntax Highlight [new] | Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDFPosted Image Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation DemoLike the examples/UDFs? Please rate the topic (up-right corner of the post header: Rating Posted Image)* === My topics === *

==========================================================Posted Image==========================================================

AutoIt is simple, subtle, elegant. © AutoIt Team


#12 Zedna

Zedna

    AutoIt rulez!

  • MVPs
  • 8,786 posts

Posted 20 September 2008 - 10:58 AM

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, 20 September 2008 - 10:59 AM.


#13 Valuater

Valuater

    www.PayFreeWireless.com

  • MVPs
  • 11,214 posts

Posted 20 September 2008 - 04:30 PM

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

8)

Posted Image

Clic The Pic!!!


#14 PsaltyDS

PsaltyDS

    Most Venerable Penguin

  • MVPs
  • 13,279 posts

Posted 20 September 2008 - 05:11 PM

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.

:)
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

#15 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,251 posts

Posted 20 September 2008 - 06:03 PM

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) :) .
Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

Posted Image AutoIt Russian CommunityPosted Image Projects: ATT - Application Translate Tool [new] | BlockIt - Block files & folders [new] | SIP - Selected Image Preview [new] | SISCABMAN - SciTE Abbreviations Manager [new] | AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramPosted Image UDFs: OnAutoItErrorRegister - Handle AutoIt critical errors [new] | AutoIt Syntax Highlight [new] | Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDFPosted Image Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation DemoLike the examples/UDFs? Please rate the topic (up-right corner of the post header: Rating Posted Image)* === My topics === *

==========================================================Posted Image==========================================================

AutoIt is simple, subtle, elegant. © AutoIt Team





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users