Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptp5.Table setString() Method

p5.Table setString() Method

The setString() method of p5.Table in p5.js is used to store the given String value to the given row and column of the table. The row can be specified by the row ID and the column can be specified by its column ID or column name.

Syntax:

setString( row, column, value )

Parameters: This function accepts three parameters as mentioned above and described below:

  • row: It is a Number that denotes the row ID of the row.
  • column: It is a String or Number that denotes the column name or ID of the column.
  • value: It is a String that specifies the value that has to be stored.

Below examples illustrate the setString() method in p5.js:

Example 1:




function setup() {
  createCanvas(500, 300);
  textSize(18);
  
  text("Click the button to modify" +
       " the table", 20, 20);
  
  setBtn =
    createButton("Change Status");
  setBtn.position(30, 60);
  setBtn.mouseClicked(modifyTableData);
  
  // Create the table
  table = new p5.Table();
  
  // Add the columns and rows
  table.addColumn("flight");
  table.addColumn("status");
  
  let row = table.addRow();
  row.setString("flight", "American Air");
  row.setString("status", "On Time");
  
  row = table.addRow();
  row.setString("flight", "Lufthansa");
  row.setString("status", "On Time");
  
  row = table.addRow();
  row.setString("flight", "United Air");
  row.setString("status", "Delayed");
  
  showTable();
}
  
function modifyTableData() {
  
  // Loop through all the rows
  // in the table
  for (let r = 0; r < table.getRowCount(); r++) {
  
    // Change the string of the status column
    if (table.getString(r, "status") == "On Time"
      table.setString(r, "status", "Delayed");
    else table.setString(r, "status", "On Time");
  }
  
  showTable();
}
  
function showTable() {
  clear();
  text("The value is modified using the" +
       " setString() method", 20, 20);
  
  // Show all the columns present
  for (let c = 0; c < 2; c++) {
    text(table.columns,
         20 + 160 * c, 110);
  }
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < 3; r++) {
    for (let c = 0; c < 2; c++) {
      text(table.getString(r, c),
           20 + 160 * c,
           140 + 20 * r);
    }
  }
}


Output:
setString-ex1

Example 2:




function setup() {
  createCanvas(500, 300);
  textSize(18);
  
  setBtn = createButton("Toggle Strings");
  setBtn.position(30, 40);
  setBtn.mouseClicked(modifyTableData);
  
  // Create the table
  table = new p5.Table();
  
  for (let i = 0; i < 10; i++) {
    table.addColumn();
    table.addRow();
  }
  
  createTable();
}
  
function createTable() {
  
  // Loop through all the rows and columns
  // in the table and set their initial
  // string values
  for (let r = 0; r < table.getRowCount(); r++) {
    for (let c = 0; c < table.getColumnCount(); c++) {
      if ((r + c) % 2 == 0) 
        table.setString(r, c, "X");
      else table.setString(r, c, "O");
    }
  }
  
  showTable();
}
  
function modifyTableData() {
  
  // Loop through all the rows and columns
  // in the table and change their values
  // depending on the previous string
  for (let r = 0; r < table.getRowCount(); r++) {
    for (let c = 0; c < table.getColumnCount(); c++) {
      if (table.getString(r, c) == "X"
        table.setString(r, c, "O");
      else table.setString(r, c, "X");
    }
  }
  
  showTable();
}
  
function showTable() {
  clear();
  text("The value is modified using the"
       " setString() method", 20, 20);
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < table.getRowCount(); r++) {
    for (let c = 0; c < table.getColumnCount(); c++) {
      text(table.getString(r, c), 
           20 + 40 * c, 
           80 + 20 * r);
    }
  }
}


Output:
setString-ex2

Online editor: https://editor.p5js.org/

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

Reference: https://p5js.org/reference/#/p5.Table/setString

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments