類似記錄的資料結構?

類似記錄的資料結構?

我需要這樣的東西:

\setfield{record1}{field1}{alpha}
\setfield{record1}{field2}{beta}
\setfield{record2}{field1}{gamma}
\setfield{record2}{field2}{delta}
\assign{record3}{record2}
\getfield{record1}{field1} % should expand to alpha
\getfield{record1}{field2} % should expand to beta
\getfield{record2}{field1} % should expand to gamma
\getfield{record2}{field2} % should expand to delta
\getfield{record3}{field1} % should expand to gamma
\getfield{record3}{field2} % should expand to delta

有這個包嗎?沒有assign命令我也能活下去。

答案1

本質上\setfield是使用前兩個參數來建構控制序列\def的地方。僅設定請求的控制序列(不執行錯誤檢查;...如果記錄/欄位不存在則傳回)。csname\getfield\csname\endcsname\relax

在此輸入影像描述

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\setfield}[3]{%
  \expandafter\def\csname #1@#2\endcsname{#3}%
  \listcsgadd{#1}{#2}%
}
\newcommand{\getfield}[2]{%
  \ifcsname #1@alias\endcsname
    \expandafter\expandafter\expandafter\csname \csname #1@alias\endcsname @#2\endcsname
  \else
    \csname #1@#2\endcsname
  \fi}
\newcommand{\recordalias}[2]{\expandafter\def\csname #2@alias\endcsname{#1}}
\newcommand{\recordcopy}[2]{%
  \renewcommand*{\do}[1]{%
    \expandafter\edef\csname #2@##1\endcsname{\csname #1@##1\endcsname}}%
  \dolistcsloop{#1}%
}

\begin{document}

\setfield{record1}{field1}{alpha}
\setfield{record1}{field2}{$\beta$}
\setfield{record2}{field1}{gamma}
\setfield{record2}{field2}{$\delta$}

\recordalias{record2}{record3}
\recordcopy{record2}{record4}

\getfield{record1}{field1} % should expand to alpha
\getfield{record1}{field2} % should expand to $\beta$
\getfield{record2}{field1} % should expand to gamma
\getfield{record2}{field2} % should expand to $\delta$
\getfield{record3}{field1} % should expand to gamma
\getfield{record3}{field2} % should expand to $\delta$

\setfield{record2}{field1}{$\gamma$}

\getfield{record2}{field1} % should expand to $\gamma$
\getfield{record3}{field1} % should expand to $\gamma$
\getfield{record4}{field1} % should expand to gamma

\end{document}

新增了兩種類型的作業:

  • 別名via \recordalias{<first>}{<second>}- 這使得記錄成為;<second>的別名。<first>
  • 複製via -使用以下提供的清單處理\recordcopy{<first>}{<second>}順序將所有欄位複製到<first>其中<second>etoolbox

答案2

\documentclass[10pt]{article}
\usepackage{readarray}
\begin{document}
\readarraysepchar{ }
\def\mydata{alpha beta gamma delta}
\readarray\mydata\myarray[-,2]% Read \mydata as 2 fields into 2-D \myarray
Cell (2,2) has \myarray[2,2]
whereas cell (1,2) has \myarray[1,2]
\end{document}

在此輸入影像描述

如果資料在文件中,也沒有問題。請注意\readdef,在讀取檔案時,設定\ncols為在第一行中偵測到的字段數,並且可以使用 來代替顯2\readarray.我還示範了不同分隔符號(逗號而不是空格)的使用,並展示了選項如何*刪除\readarray前導和尾隨空格。

\documentclass[10pt]{article}
\usepackage{readarray,filecontents}
\begin{filecontents*}{mydatafile}
alpha, beta
gamma, delta force
\end{filecontents*}
\begin{document}
\readarraysepchar{,}
\readdef{mydatafile}{\mydata}
\readarray*\mydata\myarray[-,\ncols]% Read \mydata as 2 fields into 2-D \myarray
Cell (2,2) has \myarray[2,2]
whereas cell (1,2) has \myarray[1,2]
\end{document}

在此輸入影像描述

最後,如果需要動態更改單元格數據,我在這裡實現巨集\setfield\<array name>[<row>,<column>]{<data>}

\documentclass[10pt]{article}
\usepackage{readarray}
\makeatletter
\gdef\setfield#1[#2,#3]#4{%
  \expandafter\gdef\csname\expandafter\@gobble\string#1[#2,#3]\endcsname{#4}}%
\makeatother
\begin{document}
\readarraysepchar{ }
\def\mydata{alpha beta gamma delta}
\readarray\mydata\myarray[-,2]% Read \mydata as 2 fields into 2-D \myarray
Cell (2,2) has \myarray[2,2]
whereas cell (1,2) has \myarray[1,2]

\setfield\myarray[1,2]{$\beta$}
Cell (1,2) now has \myarray[1,2].
\end{document}

在此輸入影像描述

相關內容