Jump to content

Recommended Posts

Posted

With the help of experts on this forum, I'm putting together a script that reads a configuration file and copies lines from it for use in another file. The file includes lines like this:

FONT = CONSOLA
rem LINS = 25

etc.

I've written a script to extract those lines that aren't REMmed out at the start, and I wonder if there's a more efficient way to accomplish the assignment of variables that you'll see in a long list near the end of the script:

$vdosroot = "f:\users\edward\vdoswp"
$vdosdir = $vdosroot & "\programs"
$Source = $vdosroot & "\51VDMConfig\config.txt"

$sText = FileRead($Source)

Local $aArray[22] = ["scale", "window", "low", "xmem", "colors", "mouse", "lins", _
                    "cols", "title", "frame", "timeout", "font", "wp", "euro", _
                    "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8"]

Local $varArray[22]

For $i = 0 to UBound($aArray) -1 Step 1
    $varArray[$i] = GetLine($aArray[$i])
Next

$scale = $varArray[0]
$window = $varArray[1]
$low = $varArray[2]
$xmem = $varArray[3]
$colors = $varArray[4]
$mouse = $varArray[5]
$lins = $varArray[6]
$cols = $varArray[7]
$title = $varArray[8]
$frame = $varArray[9]
$timeout = $varArray[10]
$font = $varArray[11]
$wp =   $varArray[12]
$euro = $varArray[13]
$lpt1 = $varArray[14]
$lpt2 = $varArray[15]
$lpt3 = $varArray[16]
$lpt4 = $varArray[17]
$lpt5 = $varArray[18]
$lpt6 = $varArray[19]
$lpt7 = $varArray[20]
$lpt8 = $varArray[21]

; MsgBox(0,'', $lpt1)

Func GetLine($setting)
    $aFound = StringRegExp($sText, "(?im)^\s*" & $setting & "\s*.*$", 3)
    If @error Then
        $aFound = 0
    Else
        $aFound = $aFound[0]
    EndIf
    Return $aFound
EndFunc   ;==>GetLine

I'd like to be able to save some time and make the script easier to maintain, and would be grateful for any advice.

  • Moderators
Posted

emendelson,

I take it you are intending to use those variables later in the script - so why not just leave them within the array and reference them directly when needed?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

Well (mild slap on forehead) of course you're right - that would definitely work. The reason I wanted to give them descriptive names is that it would make them a lot easier to manage. Also, at some point in the future, I'll offer a GUI for changing the values, and descriptively-named variables would also make it easier to keep track of which ones take strings, which ones take integers, which are boolean, etc. But, as you say, this is not necessary at all.

Posted

Do a search for Scripting Dictionary, which is similar to the beta feature of Maps. It's a way of giving the elements of a 1D array a name instead of a number.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

  Reveal hidden contents

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted

Thank you - I think that's what I'm looking for, though it's going to take some concentration to figure out how to make it work. I think the sample here points in the direction I need - does that look right?

 

Posted

This is very interesting, BetaLeaf. The file I want to read is a unix-style configuration file (lots of REM explanations and  a few SETTING = VALUE lines), and your code gives me lots of ideas that I think I can use. Thank you!

Posted

You could also use Enumerations:

Local Enum $eScale, $eWindow, $eLow, $eXmem, $eColors ; $eScale = 0, $eWindow = 1, etc

$varArray[$eScale]
$varArray[$eWindow]
$varArray[$eLow]
$varArray[$eXmem]
$varArray[$eColors]

 

  • Moderators
Posted

emendelson,

I tend to do as dmob suggests - Enums are very useful (and rather underused) and if execution time is critical you can very easily replace them with their numeric value before compiling.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

Enums are exactly what I was looking for, and I didn't know until now that they exist (though I've been using AutoIt for a dozen years...). Thank you for telling me about this.

  • Moderators
Posted (edited)

emendelson,

A common complaint. Although there is an example showing the usage of Enums as array indices in the Help file, I might well add an additional remark highlighting their utility in this regard.

M23

Edit: And done.

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

The example for Enum demonstrates its usage without over complication.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

You can look in my UDF to see how I do that:

 

for example"
using ENUMs/Const in Function declaration:

...
Func _XML_TransformNode($sXML_FileFullPath, $sXSL_FileFullPath, $sHTML_FileFullPath, $iEncoding = $FO_UTF8_NOBOM)
....

 

You can also check $__g_eARRAY_ATTR_* and $__g_eARRAY_NODE_*


or in my:

Check for:    $ADO_RS_ARRAY_*

....
        Else
            ReDim $aResult[$ADO_RS_ARRAY_ENUMCOUNTR]
            Local $aFiledNames_Temp[$iColumns_count]

            For $iCol_idx = 0 To $iColumns_count - 1 ;get the column names and put into 0 array element
                $aFiledNames_Temp[$iCol_idx] = $oRecordset.Fields($iCol_idx).Name
            Next
            $aResult[$ADO_RS_ARRAY_GUID] = $ADO_RS_GUID
            $aResult[$ADO_RS_ARRAY_FIELDNAMES] = $aFiledNames_Temp
            $aResult[$ADO_RS_ARRAY_RSCONTENT] = $aRecordset_GetRowsResult
            Return SetError($ADO_ERR_SUCCESS, $iRows_count, $aResult)
        EndIf
....

Or in my:

first example:

$oQP.SetOrigin($__eQPDF_SORIGIN_TopLeft);
        $oQP.SetMeasurementUnits($__eQPDF_MUNITS_Milimeters)

second example:

check for : $__eQPDF_SetupCustomPrinter_*

 

EDIT:
also:

 

Local $aPDFCreator_New_Config[$g__ePDFCreator_CONFIG_COUNTER]
        With $g__oPDFCreator
            $aPDFCreator_New_Config[$g__ePDFCreator_CONFIG_Visible] = False ; Hide PDFCreator Console/GUI
            $aPDFCreator_New_Config[$g__ePDFCreator_CONFIG_DefaultPrinter] = 'PDFCreator' ; Set PDFCreator as Default Printer
            $aPDFCreator_New_Config[$g__ePDFCreator_CONFIG_PrinterStop] = True ; STOP PDFCreator Printer
            $aPDFCreator_New_Config[$g__ePDFCreator_CONFIG_UseAutoSave] = 1 ; Set PDFCreator to AutoSave file
            $aPDFCreator_New_Config[$g__ePDFCreator_CONFIG_UseAutoSaveDirectory] = 1 ; Use PDFCreator with AutoSave
            $aPDFCreator_New_Config[$g__ePDFCreator_CONFIG_AutoSaveDirectory] = $sAutoSaveDirectory ; Set AutoSave Directory
            $aPDFCreator_New_Config[$g__ePDFCreator_CONFIG_AutoSaveFilename] = $sAutoSaveFileName ; Set AutoSave FileName
            $aPDFCreator_New_Config[$g__ePDFCreator_CONFIG_AutoSaveFormat] = $g_ePDFCreator_Format_PDF ; Set AutoSave Document Type
        EndWith

 

btw.

this quick review shows to me that there is coming time to rewrite/refactoring some of my old UDF :)

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • Moderators
Posted

mLipok,

  Quote

this quick review shows to me that there is coming time to rewrite/refactoring some of my old UDF

Expand  

Always the way.....

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)
  On 6/12/2016 at 1:39 PM, Melba23 said:

Always the way.....

Expand  

You mean: everything changes - this is the price of progress ?

Edited by mLipok
wording

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • Moderators
Posted

mLipok,

Dokładnie.

Every time you look at old code you see ways you could have made it more efficient, cleaner, better. I remember when I rewrote my own version of the Abbreviation and CallTip Managers so Jos could add them to SciTEConfig - embarrassing is not the word.....

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

I think it was some time ago, because recently when I checked Abbreviation Manager, I noticed that it was already a bit old style, and I know now you write programs differently.

EDIT:
:offtopic:

I'm going to eat dinner, and then I will be watching the match Poland <> Ireland.
I wish a nice afternoon for the community - today, for me it is beautiful weather.

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • Moderators
Posted

mLipok,

  Quote

I noticed that it was already a bit old style

Expand  

I love you too. Enjoy your dinner.....

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...