Missing initializer in const declaration

Hi,
I am trying to write a program but I am getting missing initializer problem. Following is my code:

var assert = require('assert');
 const path = require("path");
     const fs = require("fs");
//const  F5 = artifacts.require("F5")
//const ST5= artifacts.require("ST5")

module.exports = async function(callback) {
try {
     const files = new Array("A1.sol", "A2.sol", "A3.sol");
     //const keyWords = new Array("pragma Solidity",  "contract", "address","function", 'call", "send", "transfer");
     const keyWords = new Array("string1",  "string2", "string3","string4", "string5", "string6", "string7");
     //const files = new Array("tx_origin1.sol");
     const str = 0;
     for (let i = 0; i < files.length; i++) {   
        const contractPath = path.resolve('/home/zulfi/Truffle_programs/search_opcode/','contracts',files[i]);
        const contractCode2 = fs.readFileSync(contractPath, "utf8");
        var lines = this.contractCode2.split(/\r\n|\n/);
        for(var line = 0; line < lines.length-1; line++){
           const keyWordStr[ctr] = (lines[line].search(keyWords[ctr] >= 0 ? lines[line] : false);
           console.log(line + " --> "+ lines[line]);
        }//for (var...)   
        //const selfdestructExists2 = (contractCode2.search("pragma Solidity") >= 0 ? true : false);
        //console.log('selfdestruct exists:', selfdestructExists2); 
     }//for(let..)     

    
        
}//try



  catch(error) {
    console.log(error)
  }

  callback()
}

I am getting following error:

/tool2.js:19
const keyWordStr[ctr] = (lines[line].search(keyWords[ctr] >= 0 ? lines[line] : false);
^^^^^^^^^^

SyntaxError: Missing initializer in const declaration
at new Script (vm.js:83:7)
at Object.createScript (vm.js:277:10)
at Object.file (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/require/require.js:93:1)
at Object.exec (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/require/require.js:121:1)
at Promise (internal/util.js:274:30)
at new Promise ()
at bound exec (internal/util.js:273:12)
at Object.run (/home/zulfi/.nvm/versions/node/v10.23.3/lib/node_modules/truffle/build/webpack:/packages/core/lib/commands/exec.js:80:1)
at process._tickCallback (internal/process/next_tick.js:68:7)
Truffle v5.1.67 (core: 5.1.67)
Node v10.23.3

Somebody please guide me.
I am trying to retrieve lines of a file. Then search a string line by line and retrieving the entire line for the matched string.

Zulfi.

This is because const cannot be an arrays;
So, your line should be:

const keyWordStr = lines[line].search(keyWords[ctr] >= 0 ? lines[line] : false);

Please see this page

1 Like

I might be wrong, but it looks like you are trying to do some dynamic naming. You can do that with keys of an object.

example.

const keyWords = ["string1", "string2", "string3", "string4", "string5", "string6", "string7"]
const obj = {}

for (let ctr = 0, len = keyWords.length; ctr < len; ctr++) {
  obj[keyWords[ctr]] = ctr + 1
}

console.log(obj)
/*
{
  string1: 1
  string2: 2
  string3: 3
  string4: 4
  string5: 5
  string6: 6
  string7: 7
}
*/

I’ve presumed ‘ctr’ is counter. Your loop is using ‘i’.

1 Like

Hi @rpg_digital,
Thanks.
keyWords is an array and post by @wojciechlisik says that const cannot be an arrays.

I am searching “string1”, “string2” in a file. Once I find these values, I want to store the found line in the array. Looks like my syntax:

const keyWordStr = lines[line].search(keyWords[ctr] >= 0 ? lines[line] : false);

modified by @wojciechlisik is correct. How can I store the matched string in the array?

Zulfi.

I can’t see where you have defined keyWordStr or ctr in your code.

I’m maybe missing the mark — this possibly?

const keyWordStr = [] // that's fine

for (let i = 0; i < files.length; i++) {
    ...
    ...
    for(let line = 0; line < lines.length-1; line++){
        keyWordStr[i] = (lines[line].search(...
}
1 Like

When I define an array constant I do:

const keyWordStr = [];

What you are doing is defining an array with with one index ctr.

I can’t see where you have declared or assigned a value to ctr.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.