Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptp5.TableRow getNum() Method

p5.TableRow getNum() Method

The getNum() method of p5.TableRow in p5.js is used to retrieve a Float value from the given column of the table row. The column can be specified by its column ID or column name.

Syntax:

getNum( column )

Parameters: This method accepts a single parameter as mentioned above and described below:

  • column: It is a String or Number that denotes the column name or ID of the column.

Return Value: This method returns a Floating value retrieved from the given column of the table.

The example below illustrate the getNum() method in p5.js:

Example:

Javascript




function setup() {
  createCanvas(600, 300);
  textSize(18);
  
  text("Click on the button to get " +
       "all the values in the table",
       20, 20);
  
  setBtn =
    createButton("Get all table values");
  setBtn.position(30, 40);
  setBtn.mouseClicked(showTable);
  
  // Create the table
  table = new p5.Table();
  
  setTableData();
}
  
function setTableData() {
  
  // Add 5 columns and rows to the table
  for (let i = 0; i < 5; i++) {
    table.addColumn("Column " + i);
    table.addRow();
  }
  
  for (let r = 0; r < 5; r++) {
    for (let c = 0; c < 5; c++) {
  
      // Set the value at the given
      // column and row of the table
      table.set(r, c, r * 100 + c);
    }
  }
}
  
function showTable() {
  clear();
  text("The values of the table are " +
       "retrieved using the getNum() method",
       20, 20);
  
  // Show all the columns present
  for (let c = 0; c < 5; c++) {
    text(table.columns,
         20 + 100 * c, 80);
  }
  
  // Show all the rows currently
  // present in the table
  for (let r = 0; r < 5; r++) {
    
    // Get the row of the table
    let tableRow = table.rows[r];
  
    for (let c = 0; c < 5; c++) {
  
      // Get the numerical value at
      // the given column of the row
      text(tableRow.getNum(c),
           20 + 100 * c,
           120 + 20 * r);
    }
  }
}


Output:

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.TableRow/getNum

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