Hello, long time no see! :

Nomkid and I are working on a program that one core feature is to parse and index file to multiple program readable arrays, separated at each time a line contains a certain content.

This is written in node.js :

Code:
const fs = require('fs');
const homedir = require('os').homedir();
const prgmdir = homedir+'/.program_name';
const tmpdir = prgmdir+'/tmp';
const indexcache = tmpdir+'/master.index'

var index = fs.readFileSync(indexcache).toString();
index = index.split('\n');
var index_filtered = index.filter(Boolean);
console.log(index_filtered);

^^^ that's what i have right now.

My intention is to split the massive array (index_filtered), into smaller and more program readable mini array chunks at each time a line starts with certain characters.

how would we go about doing this, would i use a regex like :

Code:
var index_lbls = index_filtered.indexOf(/Index of /gi);

but that works to no avail, (it returns -1).

any help would be greatly appreciated
indexOf compares via strict equality only. Probably best just to iterate yourself to get the index.
Tari & Jacobly first wrote:
Probably best just to iterate yourself to get the index.

so now i'm iterating through the array with the .forEach(); tag


Code:
const fs = require('fs');
const homedir = require('os').homedir();
const prgmdir = homedir+'/.program_name';
const tmpdir = prgmdir+'/tmp';
const indexcache = tmpdir+'/master.index'

var index = fs.readFileSync(indexcache).toString();
index = index.split('\n');
var index_filtered = index.filter(Boolean);
console.log(index_filtered);
index_filtered.forEach(function(value) {
    const index_true = value.match(/Index of /gi);
    console.log(index_true);
});


now it's just spewing out lots of nulls and occasionally an array containing only ['Index of ']
it looks something like this :

Code:
[ 'Index of ' ]
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
[ 'Index of ' ]
null
null
null
null
null
null
null
null
null
null
null


shouldn't it only be returning 'true' when it sees a line that matches the regex /Index of/gi?
String.match returns an array, so that looks correct. Sounds like you wanted RegExp.test instead.
Tari wrote:
Sounds like you wanted RegExp.test instead.


Code:
const fs = require('fs');
const homedir = require('os').homedir();
const prgmdir = homedir+'/.program_name';
const tmpdir = prgmdir+'/tmp';
const indexcache = tmpdir+'/master.index'

var index = fs.readFileSync(indexcache).toString();
index = index.split('\n');
var index_filtered = index.filter(Boolean);
console.log(index_filtered);
const regex = RegExp('Index of*');
const globalRegex = RegExp('Index of*', 'g');
index_filtered.forEach(function(value) {
    const index_true = regex.test(value);
    console.log(index_true);
});

^^so this is what I got
so i'd need to test if it's true every time and return the index of it? I don't know how to go about it..

as to tari's help i got this :

Code:
const fs = require('fs');
const homedir = require('os').homedir();
const prgmdir = homedir+'/.CalcBucket';
const tmpdir = prgmdir+'/tmp';
const indexcache = tmpdir+'/master.index'

var index = fs.readFileSync(indexcache).toString();
index = index.split(/^Index of/g).map(s => s.split('\n'));
console.log(index);

but it's unformatted, can i split this into separate vars to fix that?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement