Trouble with Append()

Hello,

I am writing a basic poker tracker program. The program takes in a poker hand history file (a .txt file type) which contains multiple hands within it, isolates each hand, saves it into the data folder as a unique .txt file, and then creates an object that uses that single hand history .txt file to run different analysis on it.

The first time I run the program on the .txt file everything works as expected. The second time I am running into some problems.

Table variables;
String [] rawhandhistoryfile; //This is where the raw hand history .txt is loaded into
//This stores the names of existing hands in the database to check for duplicates
String [] namesofhands;
//This scrapes the current hand name from the .txt file being uploaded
String currenthandname;
boolean copyhand; //When this is true, the current line of the hand is copied into a String []
boolean duplicate; //Is true if curenthandname exist in the namesofhands String []

Hand [] overalldatabase; //This array of Hand objects stores every hand added into the database
//This Hand object is where the currently loading hand .txt file is placed so it can be
//loaded into the overalldatabase Hand []
Hand newhand;

int numberofhands; //Store the number of hands already in the database at start of program

void setup() {
  size(400, 400);
  //This loads the stored .csv file into the program, Variables.csv must exist in the "data"
  //directory in the processing 3 sketch before running the program for the code to work
  variables = loadTable("Variables.csv", "header");
  //This loads the overall hand history file from Bovada into the program as a String []
  rawhandhistoryfile = loadStrings("Test hands.txt");
  //This loads in the existing list of saved hands into the program
  namesofhands = loadStrings("Names of hands.txt");
  //This loads the existing number of hands in the database into the program
  numberofhands = variables.getInt(0, "Value of variable");
  //This is the existing overalldatabase at the start of the program
  overalldatabase = new Hand[numberofhands];
  //This initializes the currenthand String [] to be empty at the start of the program
  String [] currenthand = new String[0];
  //This resethand Sring [] will stay empty so the currenthand String [] can be reset
  String [] resethand = new String[0];

  //Make sure the hand history file has a valid input
  if (rawhandhistoryfile != null) {
    //Search all lines of the rawhandhistoryfile String []
    for (int i = 0; i < rawhandhistoryfile.length; i++) {
      //If the copyhand boolean is true
      if (copyhand) {
        //Add the current line of the rawhandhistory String [] into currenthand
        currenthand = append(currenthand, rawhandhistoryfile[i]);
      }
      //If the current line of rawhandhistory has the words "Bovada Hand #" in it
      if (rawhandhistoryfile[i].indexOf("Bovada Hand #") >= 0) {
        //Set the currenthandname to be "Bovada Hand #SAMPLE_NUMBER"
        currenthandname = trim(rawhandhistoryfile[i].substring(0, rawhandhistoryfile[i].indexOf("Zone")));      
        //Turn the String [] namesofhands into one long String
        String joinedhandnames = join(namesofhands, " : ");
        //If the current hand name exists in the joinedhandnames String
        if (joinedhandnames.indexOf(currenthandname) >= 0) {
          //Turn copyhand false
          copyhand = false;
          //The currenthandname is a duplicate and shouldn't be added to the database
          duplicate = true;
          //Print a message to the consol that the hand already exists
          println(currenthandname + " already exists in database");
          //If the currenthandname is not in the joinedhandnames String
        } else {
          //Set copyhand to true
          copyhand = true;
          //The hand is not a duplicate
          duplicate = false;
          //Add the currenthandname to the namesofhands String [] so it isn't added a second time
          namesofhands = append(namesofhands, currenthandname);
          //Save over the existing Names of Hands.txt
          saveStrings("data/Names of hands.txt", namesofhands);
          //Add the current line of rawhandhistory to the currenthand
          currenthand = append(currenthand, rawhandhistoryfile[i]);
        }
      }
      //If the current line of the rawhandhistory String [] has "*** SUMMARY ***" in it
      if (rawhandhistoryfile[i].indexOf("*** SUMMARY ***") >= 0) {
        //If that hand is not a duplicate
        if (!duplicate) {
          //Set copyhand to false because that is the last line of the current hand
          copyhand = false;
          //Save the hand history text in its own .txt file
          saveStrings("C:/Users/steve/Documents/Processing/My Saves/Poker_Tracker_v2/data/Saved Hands/hand" + numberofhands + ".txt", currenthand);
          //Increase the numberofhands by 1
          numberofhands++;
          //Print a message to the consol that the hand has been added
          println(currenthandname + " has now been added to the database");
          //Save the numberofhands variable in the variables spread sheet so when the program
          //opens next time that value is saved (this comment is for the next two lines)
          variables.setInt(0, "Value of variable", numberofhands);
          saveTable(variables, "data/Variables.csv");
          //Reset the current hand back to being empty
          currenthand = resethand;
        }
      }
    }
  } else { //If the file uploaded has some kind of error
    println("Invalid file location, no hands uploaded");
  }


  //This for-loop adds the new hand elements into the database
  //For each number 0 to the numberofhands integer
  for (int i = 0; i < numberofhands; i++) {
    //Create a new Hand object from the corresponding hand.txt file
    newhand = new Hand(loadStrings("Saved Hands/hand" + i + ".txt"));
    //Add that hand into the database
    overalldatabase = (Hand[])append(overalldatabase, newhand);
  }

class Hand{
String [] rawtext;
String name;

Hand(String[] handtext) {
    rawtext = handtext;
    name = trim(rawtext[0].substring(0, rawtext[0].indexOf("Zone")));
  }
}

The sample hands I am testing the program with is a .txt file with 26 different hands in it, no duplicates. When I run the program the first time, I get 26 .txt files saved into the data/Saved Hands directory, overalldatabase has 26 Hand objects in it, the listofnames.txt file has 26 hand names in it, and my Variables table has a value of 26 in the location where I store the int numberofhands. When I run the program a second time, all of that remains the same except the overalldatabase Hand [] doubles to 52 with each overalldatabase[0] through overalldatabase[25] being duplicated in overalldatabase[26] to overalldatabase[51]. When I run the program a third time, there is no change from the results the second time.

How can I fix my code so the overallhanddatabase[] does not duplicate on the second usage?

Can you share the contents of Variables.csv?

What do lines in a rawhandhistoryfile or a namesofhands file look like?
Can you list, briefly, what your intended inputs and outputs are?

Is it your intention for your saving to be progressive, or to wipe out and replace all contents with the latest contents each time you save?

1 Like

First, I do want to say I believe I solved the problem and it had nothing to do with append(), it was a problem with the logic of my code. In setup() I was setting the overalldatabase Hand [] to have a size of the numberofhands variable. I don’t need to do that. If I just hard code the size to zero I can have the program run the for() loop below each time the program runs starting with the hand0.txt file:

//This for-loop adds the new hand elements into the database
  //For each number 0 to the numberofhands integer
  for (int i = 0; i < numberofhands; i++) {
    //Create a new Hand object from the corresponding hand.txt file
    newhand = new Hand(loadStrings("Saved Hands/hand" + i + ".txt"));
    //Add that hand into the database
    overalldatabase = (Hand[])append(overalldatabase, newhand);
  }

That said, here are the answers to your questions. Feel free to tell me if you think there is a better way of achieving the results I’m looking for.

The Variables.csv file only has a header and one row of values. The header row has the string values “Name of Variable” and “Value of Variable”. It has the numberofhands string name and then the number of hands variable value at location (0, “Value of Variables”). Right now, it is just there to count the existing number of hands from previous usages of the program, which are “saved” by the program as individual .txt files in the “data/Saved Hands” directory.

The .txt files that will be uploaded into the rawhandhistoryfile String [] will all look something like this.

Bovada Hand #3632516319 Zone Poker ID#1286 HOLDEMZonePoker No Limit - 2018-07-20 13:52:31
Seat 1: UTG+1 ($23.40 in chips)
Seat 2: UTG+2 ($33.64 in chips)
Seat 3: Dealer ($28.76 in chips)
Seat 4: Small Blind ($31.10 in chips)
Seat 5: Big Blind [ME] ($12.50 in chips)
Seat 6: UTG ($23.62 in chips)
Dealer : Set dealer [3]
Small Blind : Small Blind $0.10
Big Blind [ME] : Big blind $0.25
*** HOLE CARDS ***
UTG+1 : Card dealt to a spot [Kh 8h]
UTG+2 : Card dealt to a spot [Qd Jd]
Dealer : Card dealt to a spot [Ah 4s]
Small Blind : Card dealt to a spot [3h Js]
Big Blind [ME] : Card dealt to a spot [2c 5c]
UTG : Card dealt to a spot [8s 3c]
UTG : Folds
UTG : Leave(Auto)
Small Blind : Leave(Auto)
UTG+1 : Raises $0.75 to $0.75
Big Blind [ME] : Leave(Auto)
UTG+2 : Calls $0.75
Dealer : Calls $0.75
Small Blind : Folds
Big Blind [ME] : Folds
*** FLOP *** [Kd 7h Ks]
UTG+1 : Checks
UTG+2 : Checks
Dealer : Checks
*** TURN *** [Kd 7h Ks] [6s]
UTG+1 : Bets $2.35
Dealer : Leave(Auto)
UTG+2 : Folds
UTG+2 : Leave(Auto)
Dealer : Folds
UTG+1 : Return uncalled portion of bet $2.35
UTG+1 : Does not show [Kh 8h] (Three of a kind)
UTG+1 : Hand result $2.47
UTG+1 : Leave(Auto)
Enter(Auto)
Enter(Auto)
Enter(Auto)
UTG+1 : Enter(Auto)
Enter(Auto)
UTG : Enter(Auto)
*** SUMMARY ***

Bovada Hand #3632516360 Zone Poker ID#1286 HOLDEMZonePoker No Limit - 2018-07-20 13:52:39
Seat 1: UTG+1 ($97.57 in chips)
Seat 2: UTG+2 ($62.06 in chips)
Seat 3: Dealer ($12.35 in chips)
Seat 4: Small Blind [ME] ($12.25 in chips)
Seat 5: Big Blind ($27.59 in chips)
Seat 6: UTG ($21.65 in chips)
Dealer : Set dealer [3]
Small Blind [ME] : Small Blind $0.10
Big Blind : Big blind $0.25
*** HOLE CARDS ***
UTG+1 : Card dealt to a spot [Ad Ts]
UTG+2 : Card dealt to a spot [3c 6d]
Dealer : Card dealt to a spot [9c Jd]
Small Blind [ME] : Card dealt to a spot [Th 8s]
Big Blind : Card dealt to a spot [4h Tc]
UTG : Card dealt to a spot [7d 7c]
UTG : Calls $0.25
UTG+1 : Raises $1.10 to $1.10
Dealer : Leave(Auto)
UTG+2 : Folds
UTG+2 : Leave(Auto)
Dealer : Folds
Big Blind : Leave(Auto)
Small Blind [ME] : Folds
Small Blind [ME] : Leave(Auto)
Big Blind : Folds
UTG : Calls $0.85
*** FLOP *** [Ah Qd Ks]
UTG : Checks
UTG+1 : Bets $1.27
UTG : Folds
UTG : Leave(Auto)
UTG+1 : Return uncalled portion of bet $1.27
UTG+1 : Does not show [Ad Ts] (One pair)
UTG+1 : Hand result $2.43
UTG+1 : Leave(Auto)
UTG : Enter(Auto)
Big Blind : Enter(Auto)
Enter(Auto)
Enter(Auto)
Enter(Auto)
Enter(Auto)
*** SUMMARY ***

There would just be more hands. Each one will start with a similar first line and I stop copying the hand after the *** SUMMARY *** line when found. The program will turn the above String [] into a String [] for just the first hand, save that hand into its own .txt file, and then continue down the rawhandhistoryfile String [] repeating the process to the end of the array.

It is my intention that these individual .txt files will be created on the first usage, and then will exist for every usage after. No duplicate hands will be created in the database as that would effect the statistical analysis done on the database later.