readintarray

Table of Contents

Prototype: readintarray(array, filename, comment, split, maxentries, maxbytes)

Return type: int

Description: Populates array with up to maxentries values, parsed from the first maxbytes bytes in file filename.

Reads a two dimensional array from a file. One dimension is separated by the regex split, the other by the lines in the file. The first field of the lines names the first array argument.

The comment field is a multiline regular expression and will strip out unwanted patterns from the file being read, leaving unstripped characters to be split into fields. Using the empty string ("") indicates no comments.

Returns the number of keys in the array, i.e., the number of lines matched.

Arguments:

  • array : Array identifier to populate, in the range [a-zA-Z0-9_$(){}\[\].:]+
  • filename : File name to read, in the range "?(/.*)
  • comment : Unanchored regex matching comments, in the range .*
  • split : Unanchored regex to split lines into fields, in the range .*
  • maxentries : Maximum number of entries to read, in the range 0,99999999999
  • maxbytes : Maximum bytes to read, in the range 0,99999999999

Example:

Prepare:

echo 1: 5:7:21:13 >  /tmp/readintarray.txt
echo 2:19:8:14:14 >> /tmp/readintarray.txt
echo 3:45:1:78:22 >> /tmp/readintarray.txt
echo 4:64:2:98:99 >> /tmp/readintarray.txt

Run:

bundle agent main

{
  vars:

      "lines" int => readintarray("array_name",
                                  "/tmp/readintarray.txt",
                                  "#[^\n]*",
                                  ":",
                                  10,
                                  4000);

  reports:
      "array_name contains $(lines) keys$(const.n)$(with)"
        with => string_mustache("", "array_name");

}

Output:

R: array_name contains 4 keys
{
      "1": {
             "0": "1",
             "1": "5",
             "2": "7",
             "3": "21",
             "4": "13"
      },
      "2": {
             "0": "2",
             "1": "19",
             "2": "8",
             "3": "14",
             "4": "14"
      },
      "3": {
             "0": "3",
             "1": "45",
             "2": "1",
             "3": "78",
             "4": "22"
      },
      "4": {
             "0": "4",
             "1": "64",
             "2": "2",
             "3": "98",
             "4": "99"
      }
}

See also: readstringarray(), readrealarray(), parseintarray(), parserealarray(), parsestringarray()