Riley Hun
Riley Hun

Reputation: 2785

KDB: apply function to all values in a dictionary

I have a dictionary with different prefixes corresponding to different keys. I want to append and pre-prend strings to each value in the dictionary.

For example:

filePrefixes: `QCentral`QWest`QEast!("GLO_CCEUML_CSFBSL_";"GLO_CCALML_CSFBSL_";"GLO_CCAPML_CSFBSL_")

And I want to pre-pend the folderPath and append the dateFormat and file extension ".csv".

So the dictionary should look like this:

filePrefixes: `QCentral`QWest`QEast!("..\..\code\products\Q\ShortLocator2\Request Files\1\GLO_CCEUML_CSFBSL_20181203_1948.csv";"..\..\code\products\Q\ShortLocator2\Request Files\1\GLO_CCALML_CSFBSL_20181203_1948.csv";"..\..\code\products\Q\ShortLocator2\Request Files\1\GLO_CCAPML_CSFBSL_20181203_1948.csv")

I did something like this. Not sure if there is a better way to do this:

  filename: (value filePrefixes),\:(dateFormat,".csv");
  filePaths: string[folderPath],/:(filename);
  filePrefixes:(key filePrefixes)!(filePaths);

Upvotes: 1

Views: 496

Answers (1)

Thomas Smyth
Thomas Smyth

Reputation: 5644

The values can be modified directly in the dictionary if that helps. Making some assumptions for dateFormat and folderPath based on your sample code:

dateFormat:"20181203_1948";
folderPath:"..\\..\\code\\products\\Q\\ShortLocator2\\Request Files\\1\\"

This can be combined with the steps you provided above without extracting the values:

q)show filePrefixes:folderPath,/:filePrefixes,\:dateFormat,".csv"
QCentral| "..\\..\\code\\products\\Q\\ShortLocator2\\Request Files\\1\\GLO_CCEUML_CSFBSL_20181203_1948.csv"
QWest   | "..\\..\\code\\products\\Q\\ShortLocator2\\Request Files\\1\\GLO_CCALML_CSFBSL_20181203_1948.csv"
QEast   | "..\\..\\code\\products\\Q\\ShortLocator2\\Request Files\\1\\GLO_CCAPML_CSFBSL_20181203_1948.csv"

Essentially the values act like a list, with some restrictions that are briefly discussed in the dictionaries section in Q for mortals.

Upvotes: 3

Related Questions