How Do I Take Only Some Lines From An Array?

edited August 2018 in Questions about Code

Hello,

I have been looking for a how to guide on this as I suspect this is a simple question but I am having trouble.

I am using the loadStrings() function to load data in from a standard .txt file. What I would like to do is isolate lines from that text file and save them as either separate arrays or objects that can be tracked individually. For example, I have a document where I want lines 0 through 36 to be "copied and pasted" into some other space where I can do specific research on that section of the data. I have found out how to locate the specific lines I need to copy, now what is the best way to do this? Can I just take this array subset and put it somewhere? Thanks for any help.

UPDATE: Thank everyone for the feedback. I have gotten most of the way to the result I want. The purpose of this project is to create a customized poker tracker software which takes in a .txt file and organizes the information in it for data processing. Below is my current code.

String [] rawhandhistory;
String [] currenthand;
int i = 0;
int currentline = 0;
int firstline = 0;
int lastline = 0;

void setup() {
  background(255);
  size(500, 900);
  String[] loadedtextfile = loadStrings("test.txt");
  rawhandhistory = loadedtextfile;
  currenthand = new String [90];
}

void draw() {
  if (i < rawhandhistory.length) {
    if (rawhandhistory[i].indexOf("Bovada Hand #") >= 0) {
      firstline = i;
      i++;
    } else if (rawhandhistory[i].indexOf("*** SUMMARY ***") >= 0) {
      lastline = i;
      //Copy all hands between first line and last line
      CopyHand();
      i++;
    } else {
      i++;
    }
  }
}

void CopyHand() {
  for (int x = firstline; x < lastline + 1; x++) {
    currenthand[currentline] = rawhandhistory[x];
    currentline++;
  }
  currentline = 0;
}

I have tested this code using a .txt document that contains 5 different hands. The CopyHand() does successfully copy each individual hand. What I am still hung up on is I would like to save the current hand somewhere before the next hand gets copied. What is the best way to do this? Should I be saving each hand into an object that takes in an array? Is it possible to just save this array into an array of String[]?

Thank you all for the help so far.

Tagged:

Answers

  • Do you want to do this programmatically? You are talking about copying and pasting as if you were going to do these actions. If it is so, you should consider checking note organizer applications. There are tons available online. You could even explore browser's web extensions.

    If you question is about coding, please provide the code you have so far.

    Kf

  • edited August 2018 Answer ✓

    you can make a new String[] array from the extracted data. Save array as txt file using saveStrings - see reference

    In theory you trying to split up a txt file into several other txt files. When you know the line numbers to use for each section you might want to use a for loop doing what I said in the previous paragraph

  • there is a new forum where we might want to continue

Sign In or Register to comment.