• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Type mismatch ?

Community Expert ,
Feb 25, 2016 Feb 25, 2016

Copy link to clipboard

Copied

Dear experts,

I have an ini-file and want to read the value of a distinct entry. Within the script the item is found and its value also. Outside the script the value is undefined.

What happens? Any ideas are welcome.

var value;

GetIniValue ("FM-biblio.ini", "04_DoingBook", value);
alert ("value read is " + value);

function GetIniValue (sIniFile, sValueName, sValue) {
// ----------------------------------------------- Read an ini item into a variable
// iniFile must end with a line containing some blanks.
// sValueName is that used in the ini-file (with the prepending #_ (e.g. "04_DoingBook")
  var thisLine, match, notFound = true;
  var regex = {
    section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
    param: /^\s*([\w\.\-\_]+)\s*=\s*(.*?)\s*$/,
    comment: /^\s*;.*$/ } ;
  settingsFile = new File($.fileName.replace (/[^\\\/]+$/i , sIniFile)); // method Ric Quatro
  settingsFile.open("r");

  while(true) {
    thisLine = settingsFile.readln();                 
    if (settingsFile.eof) {                       // at end of the file we are done
      break;
    }
    if (thisLine === null || thisLine === "") {   // skip blank lines
      continue;
    }
    if (regex.comment.test(thisLine))  {          // skip comment lines
      continue;
    }
    if (regex.section.test(thisLine))  {          // ignore section lines
      continue;
    }
    match = thisLine.match(regex.param);          // ignore undefined contents

    if (match[1] == sValueName) {
      sValue = match[2];
alert ("GetIniValue: sValueName  " + sValueName + ", sValue read = " + sValue);
      notFound = false;
      break;                                      // no need to continue while loop
    }
  }
  if (notFound) {
    alert ("GetIniValue program error - sValueName " + sValueName + " not found in sIniFile " + sIniFile);
    stop;
  }
  settingsFile.close();
} // --- end GetIniValue

The alert on line 34 reports "GetIniValue: sValueName  04_DoingBook, sValue read = no", but outside the value is undefined.

The ini file contains these lines:

...

03_ListItems      = yes
04_DoingBook      = no
05_Template       = FM-biblio-tpl.fm
...

TOPICS
Scripting

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Feb 25, 2016 Feb 25, 2016

Hi Klaus,

  1. while(true) {  
  2.     thisLine = settingsFile.readln();                   
  3.     if (settingsFile.eof) {                       // at end of the file we are done 
  4.       break;  
  5.     } 

This way you can never access the last line:

If you read the last line      ->  you have reached eof. (eof is true)

So you never can access the data.

So you can try this:

while(!settingsFile.eof)

    {

    // your commands ........

    }  

Votes

Translate

Translate
Explorer ,
Feb 25, 2016 Feb 25, 2016

Copy link to clipboard

Copied

Don't you have to either return value from the function and use that, or declare the function as a variable that's inside the same scope as the value variable?  Just guessing...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 25, 2016 Feb 25, 2016

Copy link to clipboard

Copied

Hi Klaus,

  1. while(true) {  
  2.     thisLine = settingsFile.readln();                   
  3.     if (settingsFile.eof) {                       // at end of the file we are done 
  4.       break;  
  5.     } 

This way you can never access the last line:

If you read the last line      ->  you have reached eof. (eof is true)

So you never can access the data.

So you can try this:

while(!settingsFile.eof)

    {

    // your commands ........

    }  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 26, 2016 Feb 26, 2016

Copy link to clipboard

Copied

Thank You Klaus (Göbel) for that comment: I had to add an emty line at the end of the files with my method ...

Why I do not get the value of the parameter outside of the function was not clear to me. The obvious solution for just one output parameter is:

value = GetIniValue ("FM-biblio.ini", "04_DoingBook");

...

function GetIniValue (sIniFile, sValueName {

...

retrun sFoundValue;

}

But I thought that parameters can both be input and output to a function.

Again I fell into this trap: Arguments are passed by value, hence changes within the function are not reflected outside. Only objects are passed by reference...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 26, 2016 Feb 26, 2016

Copy link to clipboard

Copied

LATEST

In javascript you can use closure to refer to variables from within a function.  But the variable and the function have to both be in the same scope.  So you would do something like this:

function doClosure() {

     var myArray[];

     var loadArray = function() {

          myArray.push("item 1");

          myArray.push("item 2");

          myArray.push("item 3");

     };

     loadArray();

     return myArray;

}

The result is an array with three items in it.

In this case the loadArray function has the same scope as the array itself, and both are just (did I say "just"?) variables.  This uses what javascript calls closure...  It's still kind of mysterious to me.  You can pass these things around as well.  And so that's how you set up recursive functions in javascript...  You create a variable and a callback in the same scope -- Then you call your recursive worker function from the same scope and pass it your callback function.  The callback function has access to the variable no matter where it is in the call stack of the recursive worker.  When it's all done, your variable is loaded with the results of the recursion...  So you return that.

The more I use javascript, the more I like it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines