Open In App

p5.Table matchRow() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The matchRow() method of p5.Table in p5.js is used to find the first row that matches the given regular expression and returns a reference to that row. The column that the method uses to search the row can be specified as a parameter. The method only returns the first row of the possible matches even if multiple matches of the regular expression exist.

Syntax:

matchRow( regexp, column )

Parameters: This method accepts two parameters as mentioned above and described below:

  • regexp: It is a String or RegExp object that specifies the regular expression to match.
  • column: It is a String or number that denotes the column name or ID of the column.

Return Value: This method returns a p5.TableRow object which matches the given regular expression.

The example below illustrates the match Row() method in p5.js:

Example:




function setup() {
  createCanvas(500, 300);
  textSize(16);
  
  matchQueryInput = createInput();
  matchQueryInput.position(30, 40);
  
  getColBtn =
    createButton("Get the matching row");
  getColBtn.position(30, 70);
  getColBtn.mouseClicked(getMatchResults);
  
  // Create the table
  table = new p5.Table();
  
  // Add two columns
  table.addColumn("name");
  table.addColumn("id");
  
  // Add some rows to the table
  let newRow = table.addRow();
  newRow.setString("name", "mary");
  newRow.setString("id", 21);
  
  newRow = table.addRow();
  newRow.setString("name", "marco6");
  newRow.setString("id", 27);
  
  newRow = table.addRow();
  newRow.setString("name", "tunisia 4");
  newRow.setString("id", 32);
  
  newRow = table.addRow();
  newRow.setString("name", "user 23");
  newRow.setString("id", 32);
  
  newRow = table.addRow();
  newRow.setString("name", "admin");
  newRow.setString("id", 45);
  
  newRow = table.addRow();
  newRow.setString("name", "mikasa");
  newRow.setString("id", 23);
  
  showTable();
}
  
function getMatchResults() {
  clear();
  
  let matchQuery =
      matchQueryInput.value();
  
  // Get the row values using matchRow()
  if (matchQuery != "") {
    
    // Match the query in the column of 'name'
    matchResults =
      table.matchRow(new RegExp(matchQuery),
                     "name");
  
    if (matchResults) {
      text("The row that matches the "
           "query is", 20, 120);
  
      // Display the matched value
      text(matchResults.arr[0], 20, 140);
      text(matchResults.arr[1], 120, 140);
    } else text("No Results Found", 20, 120);
  } else {
    text("The query string is empty", 20, 120);
  }
  text("Enter a string to find it in " +
       "the 'name' column table", 20, 20);
}
  
function showTable() {
  clear();
  
  // Display the total rows present in the table
  text("There are " + table.getRowCount() +
       " rows in the table", 20, 120);
  
  for (let r = 0; r < table.getRowCount(); r++)
    for (let c = 0; c < table.getColumnCount(); c++)
      text(table.getString(r, c),
           20 + c * 100, 140 + r * 20);
  
  text("Enter a string to find it in "
       "the 'name' column table", 20, 20);
}


Output:

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

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



Last Updated : 19 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads