Javascript reading from file error

// Basic error checking for correct number of command line arguments
if (process.argv.length < 4 || process.argv.length > 4) {
    console.log("Usage:");
    console.log("node fileToConvertFrom fileToConvertTo");
    process.exit(-1);
}

// Capture commnad line arguments. Used get filenames for reading and writing from.
var convertFrom = process.argv[2];
var convertTo = process.argv[3];

var fs = require("fs");
var importFile = fs.readFileSync("./" + convertFrom).toString('utf-8');

//var arraySeperate = text.split("<ExternalTagHere>");
var arrayByLine = importFile.split("\n");

// without manually setting index 25 to what I am searching for, and in fact what it should be already be,
// this does not work and returns index "-1" which means not found. very fucking weird.
// comment out arrayByLine[25] = "" to not have this work
// something to do with cariage returns/tabs from stupid ass fucking windows?
arrayByLine[25] = "    <ExternalTagHere>";
var getIndex = arrayByLine.indexOf("    <ExternalTagHere>");
console.log(getIndex);

//console.log(arraySeperate[0]);

//arrayEcho(arrayByMachine);

function arrayCount(arr) {
    console.log("Array count: " + arr.length);
}

function arrayEcho(arr) {
    for (var i = 0; i <= arr.length; i++) {
            console.log(arr[i]);
    }
}

Edit: @Miguel_Sensacion Thank you! Posted code.

So the file I am importing/parsing is generated on Windows. I am parsing it on a Linux box with Node on the command line.

When I manually set the element, index 25, in the array indexOf method returns succesfully. However when I do not set it manually, indexOf can not find it. This is very odd to me, just looking at the text they look exactly the same. So, I am thinking it is Windows spacing versus Linux spacing? Or tabs? Sort of lost on this one.

@dot404 did you highlight your code then hit ctrl+shift+c ? that's pretty much how I do it... like so:

var player = true;
        var squares = document.querySelectorAll('.square');
        var listRed = [];
        var listBlue = [];
        var winningCombinations = [
          [1, 2, 3],[4, 5, 6],[7, 8, 9],
          [1, 4, 7],[2, 5, 8],[3, 6, 9],
          [1, 5, 9],[3, 5, 7]
        ];
        squares.forEach(
          function(square) {
            square.addEventListener("click", function() {
              //console.log("clicked");
              if (player) {
                this.classList.add("blue");
                listBlue.push(Number(this.dataset.idx));
                //console.log(typeof Number(this.dataset.idx));
                console.log(`listBlue ${listBlue}`);
                player = false;
              } else {
                this.classList.add("red");
                listRed.push(Number(this.dataset.idx));
                console.log(`listRed ${listRed}`);
                player=true;
              }
            })
          });

Thank you for the kind response. See edited above. :slight_smile:

1 Like

I'm at work so I can't try the code out... Not supposed to be on Level1Techs.com while running SQL queries :smile:

1 Like

Is cool, eagerly awaiting response this evening!

var arrayByLine = importFile.split("\n").map(function(s){return s.replace(/[\n\r]/g, '')});

So it maps the array and replaces each new line. Not entirely sure how yet.

1 Like

regex
\n newline
\r nowhitespace

so replace newline and whitespace

1 Like

There needs to be buy-able, unique stickers on L1 that you can gift people. It may not seem like much to you, but help like this means a lot.

1 Like

It does. This community is about that, Community. Lord Wendell encourages it we all do. It's why we are here all the time :slight_smile: helping / conversing with people

1 Like

If ever there is something I can do to return, let me know.

1 Like

Keep posting code, Never stop learning, Help others with their code :slight_smile:

1 Like