csv-into-map()

Reads CSV file a (delimiter specified in d), using the local system's encoding (or as specified in parameter e) and writes each line into a map b, by using c as index of the field to be used as key and the whole line as value. Parameter g ensures Excel compliance. Note: Lines beginning with character # are not read in.

If parameter f is set to true, the map will be cleared before the processing will start.

The key of the map is the CSV field given by the index c, the value is the complete line of the CSV file including the key column(s). If the same key occurs within the CSV file more than once, the last entry overwrites the previous ones.

Composite keys: If you want to build a key out of several columns of the CSV file, you can indicate the columns to be used by their indexes, separated by an arbitrary delimiter, within parameter c. Note that the delimiter has to remain the same between all the indexes and cannot be the delimiter of the CSV file.

The CSV file will be cached. A repeated call of the function will use the cached CSV file for improved performance unless the file has been modified. The cache expires after 30 minutes. In that case, the file will be read again.

After processing the CSV file, the function returns the number of key-value pairs contained in the map.

Parameters


Parameter

Description

a

Filename of the CSV file.

b

(optional) Name of the map. Default: default

c

(optional) Index of the key column. Default: 1

d

(optional) Delimiter between the values in the CSV file. Default: ,

e

(optional) The charset the CSV file is encoded. Default: The system encoding.

f

(optional) true if the map should be cleared first. Default: true

g

(optional) If true, Excel compliance is ensured. Default: false.

h

(optional) By default, the function works with an internal cache in which data parsed from the file is kept. If the file is changed in the meantime, this can lead to data inconsistencies for various reasons. To prevent this, you can disable the complete caching with value true. Default: false.

i

(optional) The 'delimiter' used internally in the map. Default: The delimiter from parameter d. Example: Let's assume the following CSV line: value1;"value2;anything";value3. If nothing is entered in parameter d and i, the 'delimiter' (;) is used internally in the map. The map will now contain the value value2;anything;value3 under key value1. But if you want to read what is stored in the map with function "get value from map()", for example, you get three values: value2, anything and value3 if you specify the delimiter (;). This is wrong, of course, because there were originally only two values in the CSV file (value2;anything and value3). But the delimiter within this value was invalidated there by the quotation marks (CSV quoting) that were set around the value and this information was lost when writing to the map. To work around this error, you can specify a different internal 'delimiter' for the map in parameter i, e.g. (|). As a result, the map then contains the value value2;anything|value3 under the key value1, and when you subsequently read it out, you still get the two correct values value2;anything and value3 if you also use the delimiter (|) there.

j

(optional) First line to be read in. Index starts at 1.

k

(optional) Last line to be read in. Index starts at 1.


Examples


The following CSV file is given.


values.csv
key1_1,key1_2,value1,value2,value3
key2_1,key2_2,value4,value5,value6

Different parameters and results:


Parameter a

b

c

d

e

f

g

h

i

j

k

Result

Values of the Map

./conf/example/values.csv

RESMAP


,








2

{key1_1=key1_1,key1_2,value1,value2,value3

key2_1=key2_1,key2_2,value4,value5,value6}

./conf/example/values.csv

RESMAP

2

,


true






2

{key1_2=key1_1,key1_2,value1,value2,value3

key2_2=key2_1,key2_2,value4,value5,value6}

./conf/example/values.csv

RESMAP

1#2

,


true






2

{key1_1#key1_2=key1_1,key1_2,value1,value2,value3

key2_1#key2_2=key2_1,key2_2,value4,value5,value6}