1

I'm using the arrayjobx to store data in arrays, and I want to search through those arrays.

For example, I might have a list of names and places and I want to search through those names to find the index of a given name in that array.

\newarray\Names
\newarray\Places
\readarray{\Names}{Alice,Bob,Charles,Steve,George}
\readarray{\Places}{Alberta,Bangkok,China,Saarland,Georgia}

I want something like \findindex so that I can call the place associated to the person, e.g.

\Places(\findindex{\Names}{Bob})

should output Bangkok.

How might I do this?

  • I'm not sure that arrayjobx is able to retrieve the index of an entry, without doing a loop over the whole array. Are you tied to arrayjobx or would you accept other solutions? – egreg May 21 '15 at 17:45
  • I'd accept other solutions. Is there a good alternative to arrayjobx here? I did eventually figure out how to do a loop through the whole array to find the index, which solved my problem, though. – luftbahnfahrer May 22 '15 at 19:45
3

You probably want a property list, rather than two independent arrays.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\addperson}{mm}
 {
  \prop_gput:Nnn \g_luftbahn_person_place_plist { #1 } { #2 }
 }

\DeclareExpandableDocumentCommand{\Place}{m}
 {
  \prop_item:Nn \g_luftbahn_person_place_plist { #1 }
 }

\prop_new:N \g_luftbahn_person_place_plist
\ExplSyntaxOff

\addperson{Alice}{Alberta}
\addperson{Bob}{Bangkok}
\addperson{Charles}{China}
\addperson{Steve}{Saarland}
\addperson{George}{Georgia}

\begin{document}

Bob lives in \Place{Bob}.

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.