Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

pgffor's foreach can take an "array" of elements. I need to map those elements using another array(a lookup table) because I need both the elements and there mapped value.

e.g., suppose I an doing some cryptography and have letters mapped to numbers in some non-standard way

\foreach \i in {a,b,c,d,e}
{
 \i = \lookup{\i}{\data}
}

where data is, I guess some type of associative array that says stuff like

a = 12, b = 16, c = 3, d = 9, e = 19

which the foreach will then print

a = 12
b = 16
c = 3
d = 9
e = 19

I also can simply another array and I want to step through them simultanously, for of like

\foreach \i,\j in {a,b,c,d,e} , {12,16,3,9,19}
{
 \i = \j
}

(I do not want some type of nested loops, that is not what I'm trying to do)

The easiest solution the better. I'm not trying to do anything extremely complex

share|improve this question
1  
Don't forget to indent your code by 4 spaces –  Loop Space Aug 22 '12 at 12:01
add comment

1 Answer 1

up vote 3 down vote accepted

In the question, you have some options in mind and things you might not do but the question is not clear at all. I'm guessing something of the following.

\documentclass{article}
\usepackage{pgffor,pgfmath}

% Got some array from Excel via =randbetween(1-26)
\def\Sometwentysixnumarray{{11,4,8,12,17,20,13,9,12,16,4,19,6,11,4,26,10,13,14,15,15,1,5,1,26,14}}

\begin{document}
\foreach \x[count=\xi from 0] in {a,...,z}{
\pgfmathparse{\Sometwentysixnumarray[\xi]}
\x = \pgfmathresult, 
}
\end{document} 

enter image description here

share|improve this answer
    
This looks like it will work. It's very simple, I want to map the index value into another value. It would be nice if I could simply define what you have done in my last example as it is cleaner but yours achieves the same thing as far as I can tell. –  AbstractDissonance Aug 22 '12 at 13:03
    
@AbstractDissonance I'm sorry but I don't understand your comment. Why don't you state the problem clearly with actual data with a minimal example giving the exact output you wish to obtain so that we don't guess each others intention? –  percusse Aug 22 '12 at 13:28
    
lol, you seem to be the only one confused ;/ –  AbstractDissonance Aug 22 '12 at 13:32
    
@AbstractDissonance That might be true. –  percusse Aug 22 '12 at 13:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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