Jump to content
Sign in to follow this  
roeselpi

consolewrite can read ini values but why can i not use them in script?

Recommended Posts

hello,

 

i am really getting frustrated. i have read lots and lots of topics on how to read an ini file and the reading of my data is not the problem. the console will output the data without problems but why can i not use that data in my script. like in a message box or anywhere else?

 

here is what i have got:

[mode 1]
m1k00=$m1k00 = "@@0@@"
m1k01=$m1k01 = "@@1@@"
m1k02=$m1k02 = "@@2@@"
m1k03=$m1k03 = "@@3@@"
m1k04=$m1k04 = "@@4@@"
m1k05=$m1k05 = "@@5@@"
m1k06=$m1k06 = "@@6@@"
m1k07=$m1k07 = "@@7@@"
m1k08=$m1k08 = "@@8@@"
m1k09=$m1k09 = "@@9@@"

here is the reading of the ini file and the output into the console:

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Array.au3>
#include <String.au3>


$ini = "settings.ini"
 $sections = IniReadSectionNames($ini)
 For $a = 1 To UBound($sections) - 1
    $keys = IniReadSection($ini, $sections[$a])
    For $b = 1 To UBound($keys) - 1
        ConsoleWrite($keys[$b][1] & @CRLF)
        
    Next
 Next

MsgBox(0,0,$m1k00)

now the consolewrite will output this data:

$m1k00 = "@@0@@"
$m1k01 = "@@1@@"
$m1k02 = "@@2@@"
$m1k03 = "@@3@@"
$m1k04 = "@@4@@"
$m1k05 = "@@5@@"
$m1k06 = "@@6@@"
$m1k07 = "@@7@@"
$m1k08 = "@@8@@"
$m1k09 = "@@9@@"

so obviously the data has been transfered from my ini file into the running script or rather more the console. 

since the data is processed, why can do i get an error with my messagebox?

warning: $m1k00: possibly used before declaration. error: $m1k00: undeclared global variable.

but i have declared it. i have read it into the console and so the data must be there, but it is not. i can not understand that at all. there obviously is something missing for the script to accept my ini data and then to output that into the messagebox. i have no idea what is missing and most of all i do not even know why it is missing. it obviously is there.

please shed some light on this issue.

 

thanks in advace.

 

and most of all i am sorry for making yet another ini-file-related topic.

 

kind regards

Share this post


Link to post
Share on other sites
16 minutes ago, roeselpi said:

but i have declared it.

Where? I do not see it in the script you posted. In there you only have an array which contains the value: $m1k00 = "@@0@@".
Maybe you want to Execute that returned value so it does become a variable?

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
50 minutes ago, Jos said:

Maybe you want to Execute that returned value so it does become a variable?

Won't work: Execute doesn't process variables declarations nor assignments.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

@roeselpi
You should use a combination of Assign() and Eval() functions to do what you're trying to do:

#include <Array.au3>
#include <StringConstants.au3>

Global $strFileContent = '$m1k00 = "@@0@@"' & @CRLF & _
                         '$m1k01 = "@@1@@"' & @CRLF & _
                         '$m1k02 = "@@2@@"' & @CRLF & _
                         '$m1k03 = "@@3@@"' & @CRLF & _
                         '$m1k04 = "@@4@@"' & @CRLF & _
                         '$m1k05 = "@@5@@"' & @CRLF & _
                         '$m1k06 = "@@6@@"' & @CRLF & _
                         '$m1k07 = "@@7@@"' & @CRLF & _
                         '$m1k08 = "@@8@@"' & @CRLF & _
                         '$m1k09 = "@@9@@"' & @CRLF, _
       $arrVariableNames, _
       $arrVariableValues

$arrVariableNames = StringRegExp($strFileContent, '\$([^\s]+)\s=\s"[^"]+"', $STR_REGEXPARRAYGLOBALMATCH)
$arrVariableValues = StringRegExp($strFileContent, '\$[^\s]+\s=\s("[^"]+")', $STR_REGEXPARRAYGLOBALMATCH)

For $i = 0 To UBound($arrVariableNames) - 1 Step 1
    ConsoleWrite("Assign(" & $arrVariableNames[$i] & ", " & $arrVariableValues[$i] & "): " & _
                (Assign($arrVariableNames[$i], $arrVariableValues[$i]) = 1 ? "OK" : "ERR" & @error) & @CRLF)
Next

For $i = 0 To UBound($arrVariableNames) - 1 Step 1
    ConsoleWrite($arrVariableNames[$i] & " = " & Eval('m1k0' & $i) & @CRLF)
Next

:)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
1 hour ago, jchd said:

Won't work: Execute doesn't process variables declarations nor assignments.

Thanks, guess I've never used that as I don't like these types of declarations. :) 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I don't either, except that I'd really like it to work this way (#3702).


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

hello,

so if i understand you all correctly, then there is no way i can get any data from an ini for further use?

i thought the whole idea of an ini file was to outsource data that can be altered and that the program will still use the data even if it has been altered.

obviously i was wrong and there seems to be no way arround hardcoding the data into the script. that is very annoying. that way i could never compile an exe file of my program because every time i would want to change the ini or add something to it, i would  have to compile a new program. that is a shame.

what is the point in reading an ini file then if you can not use the data?

kind regards

Share this post


Link to post
Share on other sites

@roeselpi, Realize that the content of an INI file is just data. That the data ressembles AutoIt variable(s) name(s) doesn't turn inert data into code!

Just IniRead the parts you want and process the content read.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
3 minutes ago, roeselpi said:

hello,

so if i understand you all correctly, then there is no way i can get any data from an ini for further use?

i thought the whole idea of an ini file was to outsource data that can be altered and that the program will still use the data even if it has been altered.

obviously i was wrong and there seems to be no way arround hardcoding the data into the script. that is very annoying. that way i could never compile an exe file of my program because every time i would want to change the ini or add something to it, i would  have to compile a new program. that is a shame.

what is the point in reading an ini file then if you can not use the data?

kind regards

No you did not understand correctly.  You can have variables set in ini file, but you will have to use Assign to actually create the variables.

Share this post


Link to post
Share on other sites

Assign() is a terrible to organize data.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Just now, jchd said:

Assign() is a terrible to organize data.

Agree, but feasible, if OP wants it like that.  I am just responding to the fact he was under the impression that it was not possible.

Share this post


Link to post
Share on other sites

@roeselpi,

Make your .INI file like that:

[mode 1]
m1k00=@@0@@
m1k01=@@1@@
m1k02=@@2@@
m1k03=@@3@@
m1k04=@@4@@
m1k05=@@5@@
m1k06=@@6@@
m1k07=@@7@@
m1k08=@@8@@
m1k09=@@9@@

Then IniReadSection "mode 1". You get an array of key-value like the help explains.

After that process the array to your needs.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

hello,

so if i declare in my script the 

$m1k00
$m1k01
$m1k02
$m1k03
$m1k04
$m1k05
$m1k06
$m1k07
$m1k08
$m1k09

as being there for example as a global thing, then how do i get the ini data "@@0@@" from the ini combined with the $m1... 

i do not grasp the concept of the ini and how to use it in my script

if i alter the ini file now to the following

[mode 1]

m1k00=@@1@@

m2k01=@@2@@

etc.

then i want my script to get the content from the ini and place it into the variable/string/whatever dollar sign thing.

$m1k00 = "inifilecontent"

so that i can for example just do a message box msgbox(0,0,$m1k00) and get the @@1@@ as result.

i just do not understand why or how the data can be read and processed to be used in the script. 

 

sorry for not being able to undstand properly. but somehow i was under the impression that an ini file would make my script universal and that i just go ahead and change the ini to get a totaly different result within seconds.

 

frustrating somewhat.

 

kind regards

Share this post


Link to post
Share on other sites

A variable name is nothing more than sugar for your eyes! Your variable containing the precious data doesn't HAVE to be named $m1k00. Instead it can be $aSection[$i + 1]. You really should read and study the examples under IniReadSection of the help file.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

i have been reading the help file for about 2 days now concerning everything to do with ini read/write but am no where near to what i was hoping for. the examples (like almost all examples in the autoit helpfile) are not very easy to understand or follow. half the time for me it is just guessing what the hell is ment and then just trying a hundred different things until by chance i get it right. 

it is not the fact that i do not try to read and understand the help file. it might be great if you already basicly know what you are doing but for a noob it is not easy to understand. and no, i am not some young kid trying to mess around with stuff he knows nothing about. i might not grasp the concept very quickly but i do not give up. however the help files are in my opinion not very well done or lets say not very clear and transparent. the examples usually have nothing to do with reality and never ever go into depth with a common problem or are just to plain complicated. suddenly things apear in the examples that are not mentioned anywhere before and then i find myself asking: where does that come from now and how am i supposed to understand that?

i can well imagine that the writers of the help file had best intensions and probably it is very clear to people who have basic programming skills but for a novice it is not always helpful.

 

anyway, i will read it all again and see if i can get any further.

 

thanks anyway.

Share this post


Link to post
Share on other sites

Basically your problem is that you cannot express clearly you intents.  We are all trying to figure out the technical issues, but the real problem resides on the organisational challenges you are facing.  We will continue (I am sure) to suggest some solutions, but none will give you the right one, because you simply cannot tell us the real situation.  And it takes a real talent to express a problem clearly.

Share this post


Link to post
Share on other sites

hello,

 

a part-succes but that might show what i want better. i have edited the ini file now to just resemble the actual setting and it looks like this now:

[mode 1]
m1k00=@@0@@
m1k01=@@1@@
m1k02=@@2@@
m1k03=@@3@@
m1k04=@@4@@
m1k05=@@5@@
m1k06=@@6@@
m1k07=@@7@@
m1k08=@@8@@
m1k09=@@9@@

then i replaced the consoleread part where all the data is outputed into the console and it looks like this now:

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Array.au3>
#include <String.au3>


$ini = "illogic3.ini"
 $sections = IniReadSectionNames($ini)
 For $a = 1 To UBound($sections) - 1
    $keys = IniReadSection($ini, $sections[$a])
    For $b = 1 To UBound($keys) - 1
        ;ConsoleWrite($keys[$b][1] & @CRLF) ;---------------old-line-of-code
        $m1k00 = $keys[$b][0] ;-----------------------------new-line-of-code

    Next
 Next

MsgBox(0,0,$m1k00)

now my message box will give me data but for some reasoen it will give me the data of m1k09 meaning "@@9@@" even though it should give me the data from $m1k00.

basicly what i want do do is something like this (this following will probably not be a valid code:

$m1k00 = $keys[$b][number0] ;get/store data from ini file that resembles m1k00 AND mode 1 as a section
$m1k01 = $keys[$b][number1] ;get/store data from ini file that resembles m1k01 AND mode 1 as a section
$m1k02 = $keys[$b][number2] ;get/store data from ini file that resembles m1k02 AND mode 1 as a section
[...]
;--- and most important of all, get the same data from mode 2, mode 3, mode 4 ... mode 10 ... etc

once the data has been read, please output it into a message-box

MsgBox(0,0,$m1k00 & @CRLF & $m1k01 & @CRLF & $m1k02 .... ) ;--- get all the data from ini combined with the right $m1kNUMBER and display.
MsgBox2(0,0,$m2koo & @CRLF & $m2k01 & @CRLF & $m2k02 .... ) ;--- get all the data from ini combined with the right $m2kNUMBER and display.

as long as i can see that the data has been read and as long as i see that the data has been placed with the right m1k variable, then i know that i can use the outputed data somewhere else in the script. that means that the hardcoding that is there at the moment can be deleted and i can replace it with the read data from the ini file.

goal is to change this:

Global $Mode1Array = ["@@0@@", "@@1@@", "@@2@@", "@@3@@", "@@4@@", "@@5@@", "@@6@@", "@@7@@", "@@8@@", "@@9@@"] ; Mode 1

into this:

Global $Mode1Array = [$m1k00, $m1k01, $m1k02, $m1k03, $m1k04, $m1k05, $m1k06, $m1k07, $m1k08, $m1k09] ; Mode 1

 that way i can just edit the ini file and the result will always be different then the result is now at this moment in time.

i must see that the key values match and that the system works in the messagebox first before i go and change the script. that is why i want to just see that the messagebox can read the correct values and then i know that i can get them put where they have to go.

i can not explain it any better than this.

kind regards

Share this post


Link to post
Share on other sites
46 minutes ago, roeselpi said:

now my message box will give me data but for some reasoen it will give me the data of m1k09 meaning "@@9@@" even though it should give me the data from $m1k00.

That's because you are overwriting the content of $m1k00, which, in the first cycle of loop, is set to "@@0@@", but then, since it's a single variable, and you are assigning to it an indexed value, it changes as the loop continues.

50 minutes ago, roeselpi said:

basicly what i want do do is something like this

You could do it, but it could be easily replaced with an array.

The thing is that if you are going to use always the same number of variables in the same way, then you can use static variables' names and so on, without using Assign() and Eval(); but if you need something more dynamic, then that's not the solution for your script :)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

This is what I would use :

#include <StringConstants.au3>
#include <Array.au3>
#include <String.au3>

Global Const $MAX_MODE = 100
Global Const $MAX_KEY = 20

Global $aMode[$MAX_MODE][$MAX_KEY]

 $ini = "illogic3.ini"
 $sections = IniReadSectionNames($ini)
 For $a = 1 To UBound($sections) - 1
    $keys = IniReadSection($ini, $sections[$a])
    For $b = 1 To UBound($keys) - 1
        ConsoleWrite($keys[$b][1] & @CRLF)
        $mode = Number(StringRegExp ($keys[$b][0],"m(\d+)",$STR_REGEXPARRAYMATCH)[0])
        $key = Number(StringRegExp ($keys[$b][0],"m\d+k(\d+)",$STR_REGEXPARRAYMATCH)[0])
        $aMode[$mode][$key] = $keys[$b][1]
    Next
 Next

$aMode[0][0]=$mode ; (save number of modes) assuming here that ini file has mode number increasing correctly - otherwise a counter should be used
Redim $aMode[$aMode[0][0]+1][$MAX_KEY] ; to save space if $MAX_MODE is larger than required
MsgBox ($MB_SYSTEMMODAL,"",$aMode[1][0])
_ArrayDisplay ($aMode)
if $aMode[1][10] = "" then MsgBox ($MB_SYSTEMMODAL,"","Key is empty")

 

Edited by Nine

Share this post


Link to post
Share on other sites

hello,

i think i have got it now: 

 

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Array.au3>
#include <String.au3>


$ini = "illogic3.ini"

$readtheini = IniReadSection($ini, "mode 1")

$m1k00 = $readtheini[1][1]
$m1k01 = $readtheini[2][1]
$m1k02 = $readtheini[3][1]
$m1k03 = $readtheini[4][1]
$m1k04 = $readtheini[5][1]
$m1k05 = $readtheini[6][1]
$m1k06 = $readtheini[7][1]
$m1k07 = $readtheini[8][1]
$m1k08 = $readtheini[9][1]
$m1k09 = $readtheini[10][1]

MsgBox(0,0,$m1k00 & " " & $m1k01 & " " & $m1k02 & " " & $m1k03  & " " & $m1k04 & @CRLF & $m1k05  & " " & $m1k06 & " " & $m1k07 & " " & $m1k08 & " " & $m1k09)

that is exactly what i want. now i can use the data somewhere else in the script because they are all inside.

it might not be a super solution but it works. i guess you could somehow do a loop but that a different cup of tea.

kind regards

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...