• Tidak ada hasil yang ditemukan

Table 16: Temperature = 30, NaCl = 5.12, pH = 4.5 Time Log Count

0 3.73 23 3.67 60 4.9

Example 11 (Dynamically Allocating Field Name)

(This example was suggested by Bill Hobbs.) Suppose you have a directory containing members of multiple clubs. The CSV file (say, clubs.csv) may look something like:

First Name,Surname,Rockin,Single John,"Smith, Jr",member,

Jane,Brown,,friend Andy,Brown,friend,member Z\"oe,Adams,member,member Roger,Brady,friend,friend Clare,Verdon,member,

(Blank entries indicate that the person is not a member of that club.) The data can be loaded as follows:

\DTLloaddb{clubs}{clubs.csv}

Suppose at the beginning of your document you have specified which club you are interested in (RockinorSingle) and store it in\DIdent:

\newcommand{\DIdent}{Rockin}

You can now display the members for this particular club as follows:

\begin{table}[htbp]

\caption{Club Membership}

\centering

\begin{tabular}{lll}

\bfseries First Name & \bfseries Surname & \bfseries Status

\DTLforeach*[\not\DTLiseq{\status}{}]{clubs}

{\firstname=First Name,\surname=Surname,\status=\DIdent}{%

\\\firstname & \surname & \status }%

\end{tabular}

\end{table}

The result is shown inTable 17.

Table 17: Club Membership First Name Surname Status

John Smith, Jr member

Andy Brown friend

Zöe Adams member

Roger Brady friend

Clare Verdon member

When you iterate through the database using\DTLforeach(described insub- section 5.4), if an entry is missing for a given row, the associated command given in the ⟨values⟩ argument will be set to a null value. This value depends on the data type associated with the given key.

\DTLstringnull

\DTLstringnull

This is the null value for a string.

\DTLnumbernull

\DTLnumbernull

This is the null value for a number.

\DTLifnull{⟨cmd⟩}{⟨true part⟩}{⟨false part⟩}

\DTLifnull

This checks if⟨cmd⟩is null where ⟨cmd⟩is a command name, if it is, then ⟨true part⟩is done, otherwise⟨false part⟩is done. This macro is illustrated inexample 12 below.

Example 12 (Null Values)

Consider the following (which creates a database calledemailDB):

\DTLnewdb{emailDB}

\DTLnewrow{emailDB}

\DTLnewdbentry{emailDB}{Surname}{Jones}

\DTLnewdbentry{emailDB}{FirstName}{Mary}

\DTLnewdbentry{emailDB}{Email1}{[email protected]}

\DTLnewdbentry{emailDB}{Email2}{[email protected]}

\DTLnewrow{emailDB}

\DTLnewdbentry{emailDB}{Surname}{Smith}

\DTLnewdbentry{emailDB}{FirstName}{Adam}

\DTLnewdbentry{emailDB}{Email1}{[email protected]}

\DTLnewdbentry{emailDB}{RegNum}{12345}

In the above example, the first row of the database contains an entry with the key Email2, but the second row doesn’t. Whereas the second row contains an entry with the keyRegNum, but the first row doesn’t.

The following code puts the information in atabularenvironment:

\begin{tabular}{lllll}

\bfseries First Name &

\bfseries Surname &

\bfseries Email 1 &

\bfseries Email 2 &

\bfseries Reg Num%

\DTLforeach{emailDB}{\firstname=FirstName,\surname=Surname,%

\emailI=Email1,\emailII=Email2,\regnum=RegNum}{%

\\\firstname & \surname & \emailI & \emailII & \regnum}%

\end{tabular}

This produces the following:

First Name Surname Email 1 Email 2 Reg Num

Mary Jones [email protected] [email protected] 0

Adam Smith [email protected] NULL 12345

Note that on the first row of data, the registration number appears as 0, while on the next row, the second email address appears as NULL. The datatoolpack- age has identified the key RegNum for this database as a numerical key, since all elements in the database with that key are numerical, whereas it has identified the key Email2as a string, since there is at least one element in this database with that key that is a string. Null numerical values are set to \DTLnumbernull(0), and null strings are set to\DTLstringnull(NULL).

The following code checks each value to determine whether it is null using

\DTLifnull. If it is, the text Missing is inserted, otherwise the value itself is used:

\begin{tabular}{lllll}

\bfseries First Name &

\bfseries Surname &

\bfseries Email 1 &

\bfseries Email 2 &

\bfseries Reg Num%

\DTLforeach{emailDB}{\firstname=FirstName,\surname=Surname,%

\emailI=Email1,\emailII=Email2,\regnum=RegNum}{%

\\\DTLifnull{\firstname}{\emph{Missing}}{\firstname} &

\DTLifnull{\surname}{\emph{Missing}}{\surname} &

\DTLifnull{\emailI}{\emph{Missing}}{\emailI} &

\DTLifnull{\emailII}{\emph{Missing}}{\emailII} &

\DTLifnull{\regnum}{\emph{Missing}}{\regnum}}%

\end{tabular}

This produces the following:

First Name Surname Email 1 Email 2 Reg Num Mary Jones [email protected] [email protected] Missing

Adam Smith [email protected] Missing 12345

If you want to do this, you may find it easier to define a convenience command that will display some appropriate text if an entry is missing, for example:

\newcommand*{\checkmissing}[1]{\DTLifnull{#1}{---}{#1}}

Then instead of typing, say,

\DTLifnull{\regnum}{---}{\regnum}

you can instead type:

\checkmissing{\regnum}

Now suppose that instead of defining the database using\DTLnewdb,\DTLnewrow and\DTLnewdbentry, you have a file with the contents:

Surname,FirstName,RegNum,Email1,Email2 Jones,Mary,,[email protected],[email protected] Smith,Adam,12345,[email protected],

and you load the data from this file using\DTLloaddb(defined insubsection 5.2).

Now the database has no null values, but has an empty value for the key RegNum on the first row of the database, and an empty value for the key Email2on the second row of the database. Now, the following code

\begin{tabular}{lllll}

\bfseries First Name &

\bfseries Surname &

\bfseries Email 1 &

\bfseries Email 2 &

\bfseries Reg Number%

\DTLforeach{emailDB}{\firstname=FirstName,\surname=Surname,%

\emailI=Email1,\emailII=Email2,\regnum=RegNum}{%

\\\DTLifnull{\firstname}{\emph{Missing}}{\firstname} &

\DTLifnull{\surname}{\emph{Missing}}{\surname} &

\DTLifnull{\emailI}{\emph{Missing}}{\emailI} &

\DTLifnull{\emailII}{\emph{Missing}}{\emailII} &

\DTLifnull{\regnum}{\emph{Missing}}{\regnum}}%

\end{tabular}

produces:

First Name Surname Email 1 Email 2 Reg Number

Mary Jones [email protected] [email protected]

Adam Smith [email protected] 12345