Reading data from a file
| Chapter 13. Getting hold of the data to be displayed |

## Reading data from a file

The second method in order of complexity is to read the data from plain text files. An example on how to use this was shown in Preparing the data. The library contains utility methods to ease reading of plain textual data in one of the following formats:

1. One column

2. Two columns

3. Comma separated values, CSV

The utility class to handle this is called `Class ReadFileData` and contains three utility methods corresponding to the list above, they are `ReadFileData::From1Col()`, `ReadFileData::From2Col()`, `ReadFileData::FromCSV()` and `ReadFileData::FromCSV2()`.

These methods are described shortly below

`ReadFileData::From1Col($aFileName, $aCol1)`

  Reads data from a text file with one column of data and stores in the supplied `$aCol1` vector.

  Typical data looks like

      1
      2
      3

      123
      14.5
      19.2

  which would result in

      1

      $aCol == array(123, 14.5, 19,2)

`ReadFileData::From2Col($aFile, $aCol1, $aCol2, $aSepChar=' ')`

  Reads data from a text file with two columns separated by the specified character.

  Typical data looks like

      1
      2
      3

      12,15
      13,34
      14,27

  which would result in

      1
      2

      $aCol1 == array(12,13,14);
      $aCol2 == array(15,34,27);

ReadFileData::FromCSV($aFile,&$aData,$aSepChar=',',$aMaxLineLength=1024)

  This method reads comma separated values from a specified file. The values are all separeted by the specified character. This method can be seen as a generalization of From1Col() method.

  Typical data looks like

      1

      12,34,56,18,19.7,55

  which would result in

      1

      $aData == array(12,34,56,18,19,7,55)

ReadFileData::FromCSV2($aFile, &$aData, $aOptions = array())

  This method also reads comma separated values from a file but with more advanced options to control how the data is read. This can be seen as a generalization of From2Col() method.

  The possible options and there default values for this method are

  - 'separator' => ','

  - 'enclosure' => '"'

  - 'readlength' => 1024

  - 'ignore_first' => false

  - 'first_as_key' => false

  Typical data (using the default values)

      1
      2
      3

      10,55
      12,78
      15,98

  would result in

      1
      2
      3
      4

      $aData = array(
        0 => array(10,12,15),
        1 => array(55,78,98)
      );

  If 'first_as_key'=>true and the data looks looks like

      1
      2
      3
      4

      "key","value"
      10,55
      12,78
      15,98

  the data would instead be read as

      1
      2
      3
      4

      $aData = array(
        "key"   => array(10,12,15),
        "value" => array(55,78,98)
      );

ReadFileData::FromMatrix($aFile,$aSepChar=' ')

  This method is especially suited o read matrix data from a file for use with the Matrix visualization (described in Chapter 22. Matrix graphs). Each line in the file corresponds to one row in the matrix.

  Typical data can look like

      1
      2
      3

      13,87,12
      15,99,33
      19,86,61

  Which wold return a matrix looking like

      1
      2
      3
      4
      5

      array(
        array(13,87,12),
        array(15,99,33),
        array(19,86,61)
      );

| Up |
| Home |