Help with making an array in a text file.

Discussion in 'Server and Client Scripting' started by Kase, Nov 20, 2014.

  1. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    So I made a death command.
    Here is the code:
    Code (text):
    1.     if (command == "d") {
    2.         if (SESSION.channels(channel).dieoff === true) {
    3.             normalbot.sendMessage(src, "/d was turned off.", channel);
    4.             return;
    5.         }
    6.         var death = new Array(sys.getFileContent(Config.dataDir + "death.txt"));
    7.         var y = Math.floor(death.length * Math.random());
    8.         sys.sendHtmlAll(death[y], channel);
    9.         sys.kick(src);
    10.         return;
    11.     }
    I didn't want to add 170 different deaths into the script, thats why I tried making an array in a text file. but instead of sending one of the death links. It sends all of them.
    From that I can see that this:
    Code (text):
    1.         var death = new Array(sys.getFileContent(Config.dataDir + "death.txt"));
    is worng.
     
    Raducan likes this.
  2. Crystal Moogle

    Crystal Moogle Ayaya~ Administrator Administrator

    Joined:
    Jul 19, 2010
    Messages:
    3,205
    Likes Received:
    531
    PO Trainer Name:
    Hanako
    Make sure each item in the text file is separated by a new line.
    So
    This is line 1
    This is line 2
    This is line 3
    etc

    Then just do sys.getFileContent(Config.dataDir + "death.txt").split("\n") or .split("\r\n") depending on your OS and what text editor you use. (Windows is \r\n, unless you use notepad++ then it's \n). That'll return an array with each item been a separate line.
     
  3. SongSing

    SongSing KILLL

    Joined:
    Jan 2, 2013
    Messages:
    641
    Likes Received:
    191
    PO Trainer Name:
    SongSing
    Code (javascript):
    1. String.prototype.splitLines = function()
    2. {
    3.     return this.replace(/\r/g, "").split("\n");
    4. };
    i makey this for my client scripts
    you can just put this at the very top of the file and do like
    Code (javascript):
    1. var death = sys.getFileContent(Config.dataDir + "death.txt").splitLines();
     
    Last edited: Nov 21, 2014
  4. Crystal Moogle

    Crystal Moogle Ayaya~ Administrator Administrator

    Joined:
    Jul 19, 2010
    Messages:
    3,205
    Likes Received:
    531
    PO Trainer Name:
    Hanako
    You'd have to extend the String prototype to do .splitLines() like that (and then the function would be wrong). I'm assuming you mean this.splitLines(stuff) or splitLines(stuff)
     
  5. SongSing

    SongSing KILLL

    Joined:
    Jan 2, 2013
    Messages:
    641
    Likes Received:
    191
    PO Trainer Name:
    SongSing
    oh i cp'd wrong

    ill fix

    yea i accidentally pulled from another thingy
     
  6. Kase

    Kase Member

    Joined:
    Mar 12, 2014
    Messages:
    148
    Likes Received:
    3
    Thanks.