Problem with understanding: (lines[line].search(keyWords[ctr] >= 0)

Hi,
I developed the following code by google search and by help of the people on this forum:

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 {
     var ctr =0;
     const keyWordStr = [];
     const files = new Array("A1.sol");
     const keyWords = new Array("string1",  "string2", "string3","string4", "string5", "string6", "string7");
     const str = 0;
     for (let i = 0; i < files.length; i++) {   
        const contractPath = path.resolve('/home/zulfi/Truffle_programs/search_opcode/','contracts',files[i]);
        console.log(files[0]);
        const contractCode2 = fs.readFileSync(contractPath, "utf8");
        let lines =   contractCode2.split(/\r\n|\n/); 
        for(var line = 0; line < lines.length-1; line++){
           keyWordStr[ctr] = (lines[line].search(keyWords[ctr] >= 0) ? lines[line] : false);
           ctr++;
           console.log("ctr=" + ctr+ "line =" + line + " --> "+ keyWordStr[ctr]);
        }//for (var...)   
       }//for(let..)     
}//try
  catch(error) {
    console.log(error)
  }

  callback()
}

Please tell me the difference between:

lines[line].search(keyWords[ctr] >= 0)` 
lines[line].search(keyWords[ctr]) >= 0

Actually I am trying to look for some strings which are not in my file:"I am trying to retrieve the lines related to the keywords i.e. “string1”, and “string2” and so on which are not in my file ."I don’t know what is the meaning of:
(lines[line].search(keyWords[ctr] >= 0)
I am new. I copied the code. I want if the lines array’s index at line contains the keyWord[0] i.e. “string1”. Should I change search line to:
keyWordStr[ctr] = (lines[line].search(keyWords[ctr] == 0) ? lines[line] : false);

Does “==0” means equal or “false”?
Somebody please guide me.

Zulfi.

You can test it out in the console of your browser(f12).

0 == false -> true
0 === false -> false

It’s generally recommended that you go with the more exact operator === (equals to and equals type)

Here’s a link that maybe useful

Where did you get this from? Is lines an array? I see you are checking it’s length line < lines.length-1. search is not a built in Js array method, and most methods take a function as an argument. All a bit odd!!

1 Like

I assume that the test is an analog to strpos; the search returns the index of the string at which the term was found, or -1 if it was not present in the string. Hence the test for >= 0 (or “was present” - 0 would mean its at the start of the string.)

2 Likes

With lines[line].search(keyWords[ctr] >= 0) which I believe is incorrect and not useful to you, as the search is searching the line for a string value of either “true” or “false”. It will be searching for “true” when keyWords[ctr] >= 0 evaluates to a truthy result which is basically all of the time, and will be searching for “false” when it evaluates to a falsey result, which is an empty string, or -1, or null, or undefined.

Here’s the other way that the line could be, which I think is more useful for you.

With lines[line].search(keyWords[ctr]) >= 0 it will instead search the line for a keyword from the keyWords array. If it finds that keyword the >= 0 will result in the comparison being true, and if it doesn’t find the keyword it will result in the comparison being false

For example, with the line:
"Peter Piper picked a peck of pickled peppers"
and a keyWords array of
["Peter", "pickled", "peppered", "peppers"]

You will end up with search results of 0, 29, -1, 37. When a search is not found it gives the value -1.
The >= 0 comparison then gives you a result of true, true, false, and true.

The rest of your code is adding lines in which keywords have been found, to a keyWordStr array.

1 Like

Here’s a deeper dive into how the two variations are evaluated.

For this example the lines array has the following string:
"Peter Piper picked a peck" at an index of 0, with line being an index of 0
and the keyWords array is:
["picked", "peck"] with ctr being an index of 1

The two lines being compared are:

  • lines[line].search(keyWords[ctr] >= 0)
  • lines[line].search(keyWords[ctr]) >= 0

The first one is not useful, and the second one is useful.

Here’s an in-depth examination of how the first line is evaluated:

lines[line].search(keyWords[ctr] >= 0)
      \--/ Evaluates to an index value such as 0
lines[0].search(keyWords[ctr] >= 0)
\------/ Evaluates to a line from the lines array
"Peter Piper picked a peck".search(keyWords[ctr] >= 0)
                                            \-/ A loop counter
"Peter Piper picked a peck".search(keyWords[1] >= 0)
                                   \---------/ Evaluates to a string
"Peter Piper picked a peck".search("peck" >= 0)
                                   \---------/ Comparison evaluates as false
"Peter Piper picked a peck".search(false)
                                   \---/ Search treats boolean as a string
"Peter Piper picked a peck".search("false")
\-----------------------------------------/ Not found, search value is -1
-1 (the final result, "false" was not found in the line being searched)

From the above breakdown, it should be clear that nothing useful is achieved there.

By contrast, here’s how the second line in question is evaluated:

lines[line].search(keyWords[ctr]) >= 0
      \--/ Evaluates to an index value such as 0
lines[0].search(keyWords[ctr]) >= 0
\------/ Evaluates to a line from the lines array
"Peter Piper picked a peck".search(keyWords[ctr]) >= 0
                                            \-/ A loop counter
"Peter Piper picked a peck".search(keyWords[1]) >= 0
                                   \---------/ Evaluates to a string
"Peter Piper picked a peck".search("peck") >= 0
\----------------------------------------/ Search is found at string index 21
21 >= 0
\-----/ Comparison evaluates as being true
true (the final result, "peck" was found in the line being searched)
1 Like

Hi Mr. @Paul_Wilkins l,

I could not understand your deeper example but I would try again. Thanks.

Zulfi.

Does spacing apart the lines help to make it easier to understand the different parts?

lines[line].search(keyWords[ctr]) >= 0
      \--/ Evaluates to an index value, such as 0

lines[0].search(keyWords[ctr]) >= 0
\------/ Evaluates to a line from the lines array

"Peter Piper picked a peck".search(keyWords[ctr]) >= 0
                                            \-/ A loop counter

"Peter Piper picked a peck".search(keyWords[1]) >= 0
                                   \---------/ Evaluates to a string

"Peter Piper picked a peck".search("peck") >= 0
\----------------------------------------/ Search is found, at index 21

21 >= 0
\-----/ Comparison evaluates as being true

result: true ("peck" was found in the line being searched)

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