summaryrefslogtreecommitdiff
path: root/macros/generic/randomlist/randomlist.dtx
blob: d62192fd45dc9f84e0ef9c528b99d1ca1e8e625e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
% \iffalse
%
%% randomlist.dtx
%% Copyleft 2013-2017 J.-C. Charpentier & C. Tellechea
%
%% Packages `randomlist' to use with (La)eTeX
%% Copyleft (L) 2013-2017 Jean-C\^ome Charpentier & Christian Tellechea.
% \fi
%
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
% \iffalse
%<*driver>
\documentclass[a4paper, 12pt]{ltxdoc}
\errorcontextlines=10

\makeatletter
\renewenvironment{theglossary}{%
  \glossary@prologue
  \GlossaryParms \let\item\@idxitem \ignorespaces}%
{}
\makeatother
\GlossaryPrologue{%
  \newpage
  \section*{Change history}
  \markboth{{Change history}}{{Change history}}%
}
\DisableCrossrefs
%\EnableCrossrefs
\CodelineIndex
\RecordChanges
%\OnlyDescription
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{kpfonts}
\usepackage[a4paper, left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage{array}
\usepackage{fvrb-ex}
\fvset{frame=lines,xrightmargin=0.5\linewidth}
\usepackage{makeidx}
\usepackage{randomlist}
\usepackage[USenglish]{babel}
\usepackage{hyperref}

\pagestyle{plain}
\makeindex
\title{%
  Package \package{randomlist}\\Tools for data base, table and random
  writting-reading}
\author{%
  Jean-Côme Charpentier\thanks{\ttfamily
    jean-come.charpentier@wanadoo.fr}
  \and
  Christian Tellechea\thanks{\ttfamily unbonpetit@openmailbox.org}
}
\newcommand*\pgm[1]{\texttt{#1}}
\newcommand*\file[1]{\texttt{#1}}
\newcommand*\package[1]{\texttt{#1}}
\newcommand*\class[1]{\texttt{#1}}
\newcommand*\option[1]{\texttt{#1}}
\newcommand*\key[1]{\texttt{#1}}
\newcommand*\Key[1]{\texttt{#1}\index{#1=\texttt{#1}}}
\newcommand*\environ[1]{\texttt{#1}}
\newcommand*\Environ[1]{\texttt{#1}\index{#1=\texttt{#1}}}
\makeatletter
\edef\quotechar{\string!}
\edef\actualchar{\string=}
\edef\verbatimchar{\string+}
\def\SpecialPageIndex#1{%
  \immediate\write\@indexfile{%
    \string\indexentry{\expandafter\@gobble\string#1\actualchar
    \string\verb\quotechar*\verbatimchar\string#1\verbatimchar|hyperpage}%
    {\number\c@page}%
  }%
}
\makeatother
\newcommand*\Cmd[1]{\texttt{\string#1}\SpecialPageIndex{#1}}

\begin{document}
\maketitle
\newpage
\setcounter{tocdepth}{3}
\tableofcontents
\newpage
\section{Overview}
The main aim of package \package{randomlist} is to work on list,
especially with random operation. The hidden aim is to build
personnal collection of exercices with different data for each
pupils. In order to build such exercices, some features about
databases are necessary.

In ``randomlist'', the word ``List'' must be understound with two
meanings:
\begin{itemize}
\item itemize and enumerate \LaTeX{} environments;
\item list as in computer science.
\end{itemize}
In fact, lists as in computer are not really lists: they are
arrays. Some commands allow to deal with these data structures as
queues, other commands as stacks and another commands as arrays.

\section{Array, queue, stack, or list?}
The package give the name ``list'' to the main data structure. First,
we have to declare a new list with command \Cmd{\NewList}. There is
nothing special about this command. It has a mandatory argument: the
name of the list.

Nearly any name is possible. However, don't use hyphen and number at
the end of the name. For instance \texttt{mylist-1} isn't a good idea
(\texttt{mylist*1} is a good one). Don't use fragile commands and
special characters. However you can use commands inside list names.

\subsection{Create, erase and show list}
You can't create an already existing list. For instance, the code:
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth]
  \NewList{MyList}
  \NewList{MyList}
\end{Verbatim}
give the error message:
\begin{Verbatim}[frame=none]
  ! Package randomlist Error: List MyList already exists.
\end{Verbatim}
If you want erase a list, use the command \Cmd{\ClearList}.

You can't create a list \key{namelist} if the macro \cmd{\namelist}
exists. For instance the code:
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth]
  \NewList{def}
\end{Verbatim}
give the error message:
\begin{Verbatim}[frame=none]
  ! Package randomlist Error: Command \def already exists.
\end{Verbatim}

\medskip
As you can see, the source is between horizontal rules and flush
right. The result is flush left. When the result isn't an error
message, source and result are side by side.

For the next commands we must be able to see the state of list. The
package \package{randomlist} offers the \Cmd{\ShowList} command which
allows to see the whole list. When a list is just created, it is empty,
so the \cmd{\ShowList} command shows it like that (source is typeseted
at the right side and result is showed at the left side):\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

\subsection{Writing and reading in a list}
Once a list is created, you can write values and, after that, read
values.

In fact, these lists can behaves like queues, stacks, or arrays
according to the used command. There are four kinds of command:
\begin{itemize}
\item Insert;
\item Extract;
\item Set;
\item Get.
\end{itemize}
\goodbreak
Each one has four variants to reach some position in the list:
\begin{itemize}
\item First;
\item Last;
\item Index (without prefix);
\item Random.
\end{itemize}
Thus we have the commands:
\begin{center}
  \begin{tabular}{*{4}{>{\ttfamily\textbackslash}l}}
    InsertFirstItem & ExtractFirstItem & SetFirstItem & GetFirstItem
    \\
    InsertLastItem & ExtractLastItem & SetLastItem & GetLastItem
    \\
    InsertItem & ExtractItem & SetItem & GetItem
    \\
    InsertRandomItem & ExtractRandomItem & SetRandomItem & GetRandomItem
  \end{tabular}
\end{center}

Each one has also a ``List'' variant which acts on several items. That
is, we have these commands: \cmd{\InsertList}, \cmd{\ExtractList},
\cmd{\SetList}, and \cmd{\GetList}. There is also a command
\Cmd{\CopyList} which is a shortcut for a special \cmd{\SetList}.

Finally, we have \cmd{\ShiftList} which is somewhere quiet special: it
creates empty items or destroy items by shifting inside a list. This
macro is rather for internal operation but you can use it. The syntax is:
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth]
  \ShiftList{<list>}{<start>}{<nb>}
\end{Verbatim}

When \texttt{<nb>} is positive, then items from index \texttt{<start>}
are right shifted. That is, index $n$ becomes index $n+\mathtt{nb}$
and index from \texttt{<start>} to
$\text{\texttt{<start>}}+\text{\texttt{<nb>}}-1$ are empty.

When \texttt{<nb>} is negative, then items starting from the end of
the list are left shifted and \texttt{<nb>} items (from index
\texttt{start}) disappear.

Usually, you don't need \Cmd{\ShiftList}: the other commands,
especially \cmd{\ExtractList} and \Cmd{\InsertList} are enough.

\subsubsection{Insert commands}
\paragraph[\string\InsertFirstItem]{\cmd{\InsertFirstItem}}
\Cmd{\InsertFirstItem} writes a value at the beginning of a list and
shift other values to the end of the list. This command has two
arguments: the list name and the value to insert. For
example:\par\medskip 

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertFirstItem{MyList}{First value}
  \ShowList{MyList}
  \InsertFirstItem{MyList}{Second value}
  \ShowList{MyList}
  \InsertFirstItem{MyList}{Third value}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

As you can see, the list is in the reverse order than the user
one. With \cmd{\InsertFirstItem}, list behaves like stack.

The value written in the list can be nearly anything. For
instance:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertFirstItem{MyList}{{two}{groups}}
  \InsertFirstItem{MyList}{\textbf{text}}
  \InsertFirstItem{MyList}{special^}
  \InsertFirstItem{MyList}{end}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

\paragraph[\string\InsertLastItem]{\cmd{\InsertLastItem}}
\Cmd{\InsertLastItem} is like \cmd{\InsertFirstItem} but the insertion
is made at the end of the list. As for the previous command, it takes
two arguments: the list name and the value to insert. The previous
example give:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertLastItem{MyList}{{two}{groups}}
  \InsertLastItem{MyList}{\textbf{text}}
  \InsertLastItem{MyList}{special^}
  \InsertLastItem{MyList}{end}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

With this command, the list behaves as queue.

\paragraph[\string\InsertItem]{\cmd{\InsertItem}}
\Cmd{\InsertItem} is like \cmd{\InsertFirstItem} but the insertion
is made to a specified position of the list. With this command, the list
behaves as an array.

The index starts from zero and if the list has $n$ elements then the
index can't be greater than $n$. When a value is inserted in position
$k$, then the previous values from $k$ to $n-1$ are shifted one
position to the right.

\cmd{\InsertItem} takes three arguments: the list name, the position
and the value. Here is an example:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertLastItem{MyList}{first}
  \InsertLastItem{MyList}{\textbf{second}}
  \InsertLastItem{MyList}{special^}
  \InsertLastItem{MyList}{end}
  \ShowList{MyList}
  \InsertItem{MyList}{2}{\textbf{insert!}}
  \InsertItem{MyList}{2}{\textbf{other!}}
  \InsertItem{MyList}{6}{real end}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

As you can see, when the two values is inserted at position~2, the
value \texttt{special\textasciicircum} is shifted from position~2 to
position~4. Moreover, it's possible to insert a value to the
nonexistent position $n$ when the length of the list is $n$: it's the
only one possibility to specify a nonexistent index.

\paragraph[\string\InsertRandfomItem]{\cmd{\InsertRandomItem}}
This is the first command which use random numbers. We will see later
how to manage random numbers themselves.

\Cmd{\InsertRandomItem} command works as the \cmd{\InsertItem} one
but the position is selected randomly by \TeX{}. Then there is only two
arguments: the list name and the value to insert. Here is an example:
\RLsetrandomseed 1\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertRandomItem{MyList}{first}
  \InsertRandomItem{MyList}{second}
  \InsertRandomItem{MyList}{third}
  \InsertRandomItem{MyList}{fourth}
  \InsertRandomItem{MyList}{fifth}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

\paragraph[\string\InsertList]{\cmd{\InsertList}}
\Cmd{\InsertList} allows to do in one shot what the \cmd{\InsertItem}
can do in several ones. The principle of this command is to insert all
the items of a list inside a second one. You have to give three
arguments: the list which receive, the index to start insertion, and
the list to insert. For instance:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \NewList{OtherList}
  \InsertLastItem{MyList}{first in M}
  \InsertLastItem{MyList}{fourth in M}
  \InsertLastItem{MyList}{fifth in M}
  \InsertLastItem{OtherList}{second in O}
  \InsertLastItem{OtherList}{third in O}
  \InsertList{MyList}{1}{OtherList}
  \ShowList{MyList}
  \ShowList{OtherList}
\end{SideBySideExample}
\par\medskip

As you can see, the second list is unchanged after operation.

Package \package{randomlist} checks that both lists exists and that
index is compatible with list. Otherwise an error message will be
raised.

It's possible to insert an empty list:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \NewList{OtherList}
  \InsertLastItem{MyList}{first}
  \InsertLastItem{MyList}{second}
  \InsertLastItem{MyList}{third}
  \InsertList{MyList}{1}{OtherList}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

\subsubsection{Extract commands}
\paragraph[\string\ExtractFirstItem]{\cmd{\ExtractFirstItem}}
The four commands \cmd{\Extract...Item} are the inverse one of the
four commands \cmd{\Insert...Item}.

\Cmd{\ExtractFirstItem} extract the first value of a list and store it
in a macro. The other elements of the list are shifted left (the list
length decreases by one). This command takes two argument: the list
name and the macro name where the value is stored. The last argument
is just the name of the macro, \emph{i.e.} the macro name without the
backslash. For example:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertLastItem{MyList}{\TeX}
  \InsertLastItem{MyList}{is}
  \InsertLastItem{MyList}{very}
  \InsertLastItem{MyList}{powerful}
  \ExtractFirstItem{MyList}{MyMacro}
  The first element was ``\MyMacro''.

  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

When you extract an element from a list, the list length decreases by
one. It explains why it's forbidden to extract an element from an
empty list. If you try  it,
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth]
  \NewList{MyList}
  \ExtractFirstItem{MyList}{MyMacro}
\end{Verbatim}
you have the error message:
\begin{Verbatim}[frame=none]
  ! Package randomlist Error: List MyList is empty.
\end{Verbatim}

\paragraph[\string\ExtractLastItem]{\cmd{\ExtractLastItem}}
\Cmd{\ExtractLastItem} behaves like \cmd{\ExtractFirstItem} but the
element extracted is the last one. Thus there is no shifting, there is
just a decrementation of the list length.

Here is an example:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertLastItem{MyList}{\TeX}
  \InsertLastItem{MyList}{is}
  \InsertLastItem{MyList}{very}
  \InsertLastItem{MyList}{powerful}
  \ExtractLastItem{MyList}{MyMacro}
  The last element was ``\MyMacro''.

  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

\paragraph[\string\ExtractItem]{\cmd{\ExtractItem}}
\Cmd{\ExtractItem} behaves like \cmd{\ExtractFirstItem} but the
element extracted is the one indicated by its index. The command takes
three argument: the list name, the index of element to extract, the
macro used to store the element extracted. Don't forget that indexes
start from zero. Here is an example:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertLastItem{MyList}{\TeX}
  \InsertLastItem{MyList}{is}
  \InsertLastItem{MyList}{very}
  \InsertLastItem{MyList}{powerful}
  \ExtractItem{MyList}{2}{MyMacro}
  The third element was ``\MyMacro''.

  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

There isn't anything special. The length of the list decreases by one
and elements are shifted accordingly to the extracted one.

\paragraph[\string\ExtractRandomItem]{\cmd{\ExtractRandomItem}}
\Cmd{\ExtractRandomItem} works like the previous
\cmd{\ExtractItem}. Here, the index is selected randomly by the
computer. Then there are only two arguments: the list name and the
macro to store the extracted element:\par\medskip

\RLsetrandomseed 3
\begin{SideBySideExample}
  \NewList{MyList}
  \InsertLastItem{MyList}{\TeX}
  \InsertLastItem{MyList}{is}
  \InsertLastItem{MyList}{very}
  \InsertLastItem{MyList}{powerful}
  \ExtractRandomItem{MyList}{MyMacro}
  ``\MyMacro'' was extracted.

  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

Even the extraction is made on a random index, it's forbidden to
extract something from an empty list. Then, the code:
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth]
  \NewList{MyList}
  \ExtractRandomItem{MyList}{MyMacro}
\end{Verbatim}
gives the usual error message:
\begin{Verbatim}[frame=none]
  ! Package randomlist Error: List MyList is empty.
\end{Verbatim}

\paragraph[\string\ExtractList]{\cmd{\ExtractList}}
The commands \cmd{\Extract...Item} extract one item and store it in a
macro. With the command \Cmd{\ExtractList} we can extract several
items and put them in a list. \cmd{\ExtractList} asks for four
arguments:
\begin{enumerate}
\item the main list;
\item the starting index;
\item the ending index;
\item the list which receive extracted values.
\end{enumerate}
Here is an example:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \NewList{OtherList}
  \InsertLastItem{MyList}{first}
  \InsertLastItem{MyList}{second}
  \InsertLastItem{MyList}{third}
  \InsertLastItem{MyList}{fourth}
  \InsertLastItem{MyList}{fifth}
  \InsertLastItem{MyList}{sixth}
  \ExtractList{MyList}{2}{4}{OtherList}

  \ShowList{MyList}
  \ShowList{OtherList}
\end{SideBySideExample}
\par\medskip

Obviously, \package{randomlist} checks list and indexes. You can have
the start index and the last index equals. In this case,
\cmd{\ExtractList} behaves like \cmd{\ExtractItem} but the extracted
value is put in a list rather than in a macro:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \NewList{OtherList}
  \InsertLastItem{MyList}{first}
  \InsertLastItem{MyList}{second}
  \InsertLastItem{MyList}{third}
  \InsertLastItem{MyList}{fourth}
  \InsertLastItem{MyList}{fifth}
  \InsertLastItem{MyList}{sixth}
  \ExtractList{MyList}{2}{2}{OtherList}

  \ShowList{MyList}
  \ShowList{OtherList}
\end{SideBySideExample}
\par\medskip

\subsubsection{Set commands}
\paragraph[\string\SetFirstItem]{\cmd{\SetFirstItem}}
The commands \cmd{\Set...Item} modify the existing values of
list. \Cmd{\SetFirstItem} modify the first value.\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertLastItem{MyList}{\TeX}
  \InsertLastItem{MyList}{is}
  \InsertLastItem{MyList}{very}
  \InsertLastItem{MyList}{powerful}
  \SetFirstItem{MyList}{\LaTeX}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

If a list is empty, there is the classic error message about empty
list.

\paragraph[\string\SetLastItem]{\cmd{\SetLastItem}}
\Cmd{\SetLastItem} acts like \cmd{\SetFirstItem} but at the end of the
list.

\paragraph[\string\SetItem]{\cmd{\SetItem}}
\Cmd{\SetItem} acts like the previous commands. It takes three
arguments: the list name, the index, the new value:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertLastItem{MyList}{\TeX}
  \InsertLastItem{MyList}{is}
  \InsertLastItem{MyList}{very}
  \InsertLastItem{MyList}{powerful}
  \SetItem{MyList}{2}{quiet}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

If the index doesn't exist, an error message is showed. Code:
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth]
  \NewList{MyList}
  \InsertLastItem{MyList}{\TeX}
  \InsertLastItem{MyList}{is}
  \InsertLastItem{MyList}{very}
  \InsertLastItem{MyList}{powerful}
  \SetItem{MyList}{4}{isn't it?}
\end{Verbatim}
gives the error message:
\begin{Verbatim}[frame=none]
  ! Package randomlist Error: Index 4 is greater than last index of list MyList.
\end{Verbatim}

\paragraph[\string\SetRandomItem]{\cmd{\SetRandomItem}}
\Cmd{\SetRandomItem} acts like the previous one but the index is
selected randomly. The list must be non empty. Here is an
example:\par\medskip 

\begin{SideBySideExample}
  \NewList{MyList}
  \InsertLastItem{MyList}{\TeX}
  \InsertLastItem{MyList}{is}
  \InsertLastItem{MyList}{really}
  \InsertLastItem{MyList}{very}
  \InsertLastItem{MyList}{powerful}
  \SetRandomItem{MyList}{snap!}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

\paragraph[\string\SetList]{\cmd{\SetList}}
Insert value one by one inside a list could be tiresome especially if
you have many values. Package \package{randomlist} allows to insert
many items in a row using the macro \Cmd{\SetList}. Items are
separated with comma. For instance:\par\medskip 

\begin{SideBySideExample}
  \NewList{MyList}
  \SetList{MyList}{\TeX, is, very,
    powerful}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

As you can see, spaces aren't discarded. A more satisfactory
presentation would be:\par\medskip 

\begin{SideBySideExample}
  \NewList{MyList}
  \SetList{MyList}{\TeX,is,very,%
    powerful}
  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

\paragraph[\string\CopyList]{\cmd{\CopyList}}
Copy a list in another one. Both lists must exists: this command don't
create list since only \cmd{\NewList} can do that. Here is an
example:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \NewList{OtherList}
  \SetList{MyList}{\TeX,is,very,%
                   powerful}
  \CopyList{MyList}{OtherList}
  \ShowList{OtherList}
\end{SideBySideExample}
\par\medskip

\subsubsection{Get commands}
\paragraph[\string\GetFirstItem]{\cmd{\GetFirstItem}}
The \cmd{\get...list} look for a value in a list. They don't change
the list. The index must exist elsewhere an error message will be
show. That is, for first, last, and random variant, list must be non
empty.

\Cmd{\GetFirstItem} put the first value of a list into a macro. The
arguments of the command are: the list name, the macro:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \SetList{MyList}{\TeX,is,so,cute}
  \GetFirstItem{MyList}{MyMacro}
  The first element is ``\MyMacro''

  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

\paragraph[\string\GetLastItem]{\cmd{\GetLastItem}}
\Cmd{\GetLastItem} acts like \cmd{\GetFirstItem} but give the last value.

\paragraph[\string\GetItem]{\cmd{\GetItem}}
\Cmd{\GetItem} acts like the previous one but give the value of the
element $k$ where $k$ is the second argument. Pay attention that
indexes start from zero. Then the index $k$ maps to the $k+1$st
element of the list.\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \SetList{MyList}{\TeX,is,so,cute}
  \GetItem{MyList}{2}{MyMacro}
  The third element is ``\MyMacro''

  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

Package \package{randomlist} offers an other syntax to access to an
item: \cmd{\<namelist>[<index>]}. Thus, we can write the previous
example like that:\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \SetList{MyList}{\TeX,is,so,cute}
  The third element is ``\MyList[2]''
\end{SideBySideExample}
\par\medskip

\paragraph[\string\GetRandomItem]{\cmd{\GetRandomItem}}
\Cmd{\GetRandomItem} give the value of a randomly selected element of a
list.\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \SetList{MyList}{\TeX,is,so,cute}
  \GetRandomItem{MyList}{MyMacro}
  The random element is ``\MyMacro''

  \ShowList{MyList}
\end{SideBySideExample}
\par\medskip

\paragraph[\string\GetList]{\cmd{\GetList}}
\Cmd{\GetList} builds a sub-list. Arguments are those of
\cmd{\ExtractList}, that is, the read list, the first index, the last
index, and the written list.\par\medskip

\begin{SideBySideExample}
  \NewList{MyList}
  \NewList{OtherList}
  \SetList{MyList}{X1,X2,X3,X4,X5,X6}
  \GetList{MyList}{2}{4}{OtherList}
  \ShowList{MyList}
  \ShowList{OtherList}
\end{SideBySideExample}
\par\medskip

Contrary to what \cmd{\ExtractItem} do, \cmd{\GetList} don't modify
the source list.

\section{Database}
\subsection{Simple database}
Package \package{randomlist} offers some features about databases. In
fact that was the first aim of this package: to be able to product one
assignment for one pupil (with all assignments different).

For \package{randomlist} a database is a list. For instance the next
example shows an usual list which is used as a database. We'll see
later real databases with records and fields. For now, our
database has records and each record has one single field: the name
and first name of our pupils. In order to parse all entries of
database \package{randomlist} offers the commands
\cmd{\ForEach...Item}. These command extract one by one all the
elements of a list and typeset, for each element, its third
argument. Second argument give the macro name where element is stored.
For these commands, the macro name is given without the
backslash. Depending how the extraction is made, we have the three
commands: \Cmd{\ForEachFirstItem}, \cmd{\ForEachLastItem}, and
\cmd{\ForEachRandomItem}. In fact, the readind is made with an
extraction but, as the work is made in a group, after the
\cmd{\ForEach...} command, the list is restored.

\subsubsection[\protect\texttt{\protect\textbackslash ForEachFirstItem}]{\cmd{\ForEachFirstItem}}

\begin{SideBySideExample}
  \NewList{Pupils}
  \SetList{Pupils}{Alfred Aho,%
    Charles Babbage,Gregory Chaintin,%
    Edsger Dijkstra}
  \ForEachFirstItem{Pupils}{Name}{%
    Test for \Name\par
    blah blah blah\dots\par\smallskip
  }
\end{SideBySideExample}
\par\medskip

\subsubsection[\protect\texttt{\protect\textbackslash ForEachLastItem}]{\cmd{\ForEachLastItem}}
\Cmd{\ForEachLastItem} acts like \cmd{\ForEachFirstItem} but the
reading is made in reverse order:\par\medskip

\begin{SideBySideExample}
  \NewList{Pupils}
  \SetList{Pupils}{Alfred Aho,%
    Charles Babbage,Gregory Chaintin,%
    Edsger Dijkstra}
  \ForEachLastItem{Pupils}{Name}{%
    Test for \Name\par
    blah blah blah\dots\par\smallskip
  }
\end{SideBySideExample}
\par\medskip

\subsubsection[\protect\texttt{\protect\textbackslash ForEachRandomItem}]{\cmd{\ForEachRandomItem}}
\Cmd{\ForEachRandomItem} acts like the previous commands but the
reading is made randomly. In the next example, we can see that the
list is restored after the command
\cmd{\ForEachRandomItem}:\par\medskip

\begin{SideBySideExample}
  \NewList{Pupils}
  \SetList{Pupils}{Alfred Aho,%
    Charles Babbage,Gregory Chaintin,%
    Edsger Dijkstra}
  \ForEachRandomItem{Pupils}{Name}{%
    Test for \Name\par
    blah blah blah\dots\par\smallskip
  }
  \ShowList{Pupils}
\end{SideBySideExample}
\par\medskip

You can put a command \cmd{\ForEach} inside another one. There is no
limits (but the stacks of \TeX{}):\par\medskip

\begin{SideBySideExample}
  \NewList{L-Man}
  \NewList{L-Woman}
  \SetList{L-Man}{Alfred,Charles,Gregory}
  \SetList{L-Woman}{Ada,Grace,Adele}
  \ForEachRandomItem{L-Man}{Man}{%
    \ForEachRandomItem{L-Woman}{Woman}{%
      \Man{} and \Woman{} are computer
      scientists\par
    }
  }
\end{SideBySideExample}
\par\medskip

Actually, there is a bug that don't allow fragile commands inside
lists when they are read with \cmd{\ForEach...Item} commands. I hope
that the next version of \package{randomlist} will fix this!

\subsection{Database with fields}
Each record of a database is read as a set of fields. In fact it's a
sequence of groups. \package{randomlist} allow to read each field with
the macro \Cmd{\ReadFieldItem}. In order to make life easy,
\package{randomlist} allow to read whole database from files with the
command \Cmd{\ReadFileList}.

This command read a field in a record and store it in a macro. It
takes three arguments: a whole record or a macro containing the whole
record, the rank of the field (starting zero), and a macro to store
the value of this field. For instance:\par\medskip

\begin{SideBySideExample}
  \def\record{{ein}{un}{one}}
  \ReadFieldItem{\record}{0}{Zahl}
  \ReadFieldItem{\record}{1}{Nombre}
  \ReadFieldItem{\record}{2}{Number}
  \Nombre\ French, \Zahl\ German,
  and \Number\ English.
\end{SideBySideExample}
\par\medskip

If there are less fields than the indicating rank then an
error message is raised:
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth]
  \def\record{{ein}{un}{one}}
  \ReadFieldItem{\record}{3}{Stuff}
\end{Verbatim}
give the error message:
\begin{Verbatim}[frame=none]
  ! Package randomlist Error: There aren't enough fields in the record.
\end{Verbatim}
Remember that fields are numbered starting from zero!

Obviously, the power of \Cmd{\ReadFieldItem} comes with list and real
database. For instance:\par\medskip

\begin{SideBySideExample}
  \NewList{Languages}
  \SetList{Languages}{{France}{un},%
    {Germany}{ein},{England}{one}}
  \ForEachFirstItem{Languages}{Unit}{%
    \ReadFieldItem{\Unit}{0}{Country}%
    \ReadFieldItem{\Unit}{1}{Number}%
    You say ``\Number'' in \Country.\par
  }
\end{SideBySideExample}
\par\medskip

It's not very handy to write a whole database inside a \LaTeX{}
source. \package{randomlist} allows to load a data base reading a
extern file. For that, there is the command \Cmd{\ReadFileList}. This
command takes two mandatory arguments: the name of the database and
the name of the file.

The file \texttt{pythagoras.dat} (page~\pageref{file-pythagoras})
shows 100~lines of three numbers separated by comma. When this file is
read, the data base contains 100~records with three fields. That is,
by default, \package{randomlist} read CSV files (Comma Separated
Values).\par\medskip

\begin{SideBySideExample}
  \NewList{Pyth}
  \ReadFileList{Pyth}{pythagoras.dat}
  \GetItem{Pyth}{10}{triple}
  \ReadFieldItem{\triple}{0}{triplea}
  \ReadFieldItem{\triple}{1}{tripleb}
  Do you know that
  $\sqrt{\triplea^2+\tripleb^2}$ is an
  integer?
\end{SideBySideExample}
\par\medskip

\NewList{Pyth}
\ReadFileList{Pyth}{pythagoras.dat}
\GetItem{Pyth}{10}{triple}
\ReadFieldItem{\triple}{0}{triplea}
\ReadFieldItem{\triple}{1}{tripleb}
\ReadFieldItem{\triple}{2}{triplec}
To those who check the triple page~\pageref{file-pythagoras}, don't
forget that the 10th rank maps with line number~11, that is,
``\texttt{\triplea,\tripleb,\triplec}''. Moreover, you have the result
to the operation : $\sqrt{\triplea^2+\tripleb^2}=\triplec$.

A file could have any structure. In particular, it could have one or
several title lines. It's the case for the file \texttt{pupils.dat}
(page~\pageref{file-pupils}) where the first line is obviously a title
line. You have just to extract this or these lines to obtain a
``classical'' database.\par\medskip

\begin{SideBySideExample}
  \NewList{Class}
  \ReadFileList{Class}{pupils.dat}
  \ExtractFirstItem{Class}{NULL}
  \GetRandomItem{Class}{pupil}
  \ReadFieldItem{\pupil}{0}{Name}
  \ReadFieldItem{\pupil}{1}{FName}
  \ReadFieldItem{\pupil}{2}{Note}
  Your child \Name{} \FName{} has
  \Note{} this term. This is
  \if A\Note very \fi
  \if C\Note not \fi
  good.
\end{SideBySideExample}
\par\medskip\noindent
Processing this way, you have got a database with real datas (no title
data).

The file could be in another format than CSV. In fact, you can define
a field separator (comma by default) and a string delimiter (double
quote by default) which allow to put a field separator inside a
field.
%The syntax isn't very rich. Unlike real CSV file, there isn't
%the syntax \texttt{""} to obtain a single \texttt{"} (or with an other
%field separator).
To indicate other symbols than comma and double
quote, the command \cmd{\ReadFileList} accept an optional argument
which declare the field separator and the string encloser by two
characters.

For instance, the file \texttt{comets.dat} (see page~
\pageref{file-comets}) has ``\texttt{\string|}'' as field
separator. Therefore, the calling syntax becomes:\par\medskip

\begingroup
\catcode`\|=12
\begin{SideBySideExample}
  \NewList{Comets}
  \ReadFileList[|"]{Comets}{comets.dat}
  \ExtractFirstItem{Comets}{NULL}
  \ExtractFirstItem{Comets}{NULL}
  \GetRandomItem{Comets}{comet}
  \ReadFieldItem{\comet}{1}{Name}
  \ReadFieldItem{\comet}{2}{Discover}
  \ReadFieldItem{\comet}{3}{Year}
  \ReadFieldItem{\comet}{4}{Period}
  The comet \Name{} was discovered by
  \Discover{} in \Year{}.
  \unless\ifx\Period\empty
    Its period is \Period{} years.
  \fi
\end{SideBySideExample}
\endgroup
\par\medskip

Observe that we have two ``title lines'' to discard. As each line
begins by a field separator, the first field of each record is
empty. Thus we extract field starting one (not zero). We test if a
period is empty because of 18D/Perrine-Mrkos comet.

\subsection{Tricks, things, and other matters}
\subsubsection{Random number}
In the package \package{randomlist}, the (pseudo) random numbers are
processed by the macro \Cmd{\RLuniformdeviate}\texttt{\{<n>\}\{<macro>\}}
(choose a random integer number between 0 and $\text{\key{n}}-1$ and
store it in |\<macro>|) and \Cmd{\RLsetrandomseed} (set the seed).

When you say nothing, the seed is calculated with the current date
(year, month, day, hour and minute). That is, if you run
\texttt{latex} twice with a delay greater than one minute, you will
have two different results. Sometime, it's what you want, sometime
it's annoying.

Under \LaTeX{}, you can set the seed with the package option
\texttt{seed} with the syntax:
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth]
  \usepackage[seed=<value>]{randomlist}
\end{Verbatim}
where \texttt{<value>} is an integer value. If \texttt{<value>} is
zero then the seed is calculated using actual time, year, month and
day.

Under \TeX{} you have to use the command \Cmd{\RLsetrandomseed} to
give the seed value. As for the option, with a zero value, the seed is
calculated using actual time, year, month and day. The syntax is
simple:
\begin{Verbatim}[xrightmargin=0pt,xleftmargin=0.5\linewidth]
  \RLsetrandomseed{<value>}
\end{Verbatim}
Of course, this command is available under \LaTeX{}.

\subsubsection{Loop}
For complex material with several databases, it could be useful to use
external loop such \cmd{\foreach} from \package{pgffor} package or
\cmd{\multido} from \package{multido} package. In fact, this is a good
idea but not the best! These commands (\cmd{\foreach} and
\cmd{\multido}) work inside a group at each loop. With
\package{randomlist} that doesn't work everytime since lists are
restored at each loop. For example, you can't extract element of a
list.

It is possible to read the list with \cmd{\Get...Item}. In this case,
you should probably use the command \Cmd{\CountList} to know the size
of a list. This command takes two arguments: the list name and a macro
to store the number of elements. As usual you give only the name of
the macro (without the backslash). The big difference between extract
and get is that with random reading, you can avoid to have twice (or
more) the same element.

A real example is too long to be inserted here. We give only the code
source. The file \texttt{.tex} and the result \texttt{.pdf} are part
of the package distribution. In this example, we use two databases:
one for the pupils and the other one for the pythagorean triples. As
we read randomly the pythagorean triples and as we won't the same test
for two pupils, then we don't use the external loop described in the
latter paragraph.

\VerbatimInput[gobble=0, frame=none, numbers=left, xleftmargin=0pt,
               xrightmargin=0pt]{test.tex}

Be careful! When we extract triples inside the loop, we must be sure
that there is more triples than pupils elsewhere an error about an
empty list is raised.

Lines 15, 16, 17 read the data bases and extract the title line from
\texttt{pupils.dat}. After that, we enter in the main loop (lines~18
to~60).

At the beginning of the loop, we read the fields for the pupil (name,
first name and note) and the three fields of the pythagorean triple
(lines~20 to~26). It's here that we extract randomly a triple. Since
it's an extraction, another pupil will have another triple.

Lines 27 to 39 typeset the test and lines 40 to 59 typeset the answer
to the test. We test the note of the pupil to decide the type of
exercise: Pythagorean theorem to find the hypotenuse (easy) or
Pythagorean theorem to find a side (less easy).

\subsubsection{Internal}
You can access directly to a list. It's not recommended but\dots

If the list name is \texttt{LName} (pay attention to the letter case),
then the length of the list is \cmd{\LName-len} and the $n$th element
of the list (starting from zero) is \cmd{\LName-$n$}. When an element
is a record with several fields, those fields are inside braces. For
example the first element of list \texttt{Triples} (see last example)
is: \cmd{\Triples-0}~$=$~\texttt{\{119\}\{120\}\{169\}}. As you can
see, inside a list, the characters for separator field and for string
delimiter don't exist.

The authors don't see any situation where knowing internal is
important. If some users have good idea about it then writing to the
authors will be an appreciate initiative!

\section{\LaTeX{} Lists}
Package \package{randomlist} offers two other special commands which
allow to build random lists.

The first one is \Cmd{\RandomItemizeList} which build an itemize list
with random placement of items. Each item is a group.\par\medskip

\RLsetrandomseed{1}
\begin{SideBySideExample}
  \LaTeX{} is:
  \RandomItemizeList
    {magical}
    {logical}
    {practical}
    {clinical}
    {cynical}
\end{SideBySideExample}
\par\medskip

The second command is for enumerate list. It is
\Cmd{\RandomEnumerateList} and it acts like the previous
one:\par\medskip

\RLsetrandomseed{2}
\begin{SideBySideExample}
  \LaTeX{} is:
  \RandomEnumerateList
    {magical}
    {logical}
    {practical}
    {clinical}
    {cynical}
\end{SideBySideExample}
\par\medskip


\newpage
\newgeometry{left=4cm, right=2cm, top=2cm, bottom=2cm}
\section{Package \package{randomlist} code}
\DocInput{randomlist.dtx}
\newpage
\newgeometry{left=2cm, right=2cm, top=2cm, bottom=2cm}
\PrintChanges
\PrintIndex
\newpage
\appendix
\fvset{gobble=0,frame=none,numbers=left,xleftmargin=0pt,xrightmargin=0pt}
\section{File \texttt{pythagoras.dat}}
\label{file-pythagoras}
This file contains Pythagorean triples which have three digits. There
isn't all these triple. In fact the triple are built with the famous
formula $(u^2-v^2,2uv,u^2+v^2)$ with $u$ and $v$ positive integers
such $u>v$. Here are only the hundred first triples with three
digits.

\VerbatimInput{pythagoras.dat}
\newpage
\section{File \texttt{pupils.dat}}
\label{file-pupils}
This file shows a first line which isn't a data line.

\VerbatimInput{pupils.dat}
\newpage
\section{File \texttt{comets.dat}}
\label{file-comets}
This file use lines which aren't data lines and weird separator.

\VerbatimInput{comets.dat}
\end{document}
%</driver>
% \fi
%
% \changes{v0.1}{2013/05/03}{%
%   First release
% }
% \changes{v0.1a}{2013/05/06}{%
%   Fix bug about braces.
% }
% \changes{v0.1b}{2013/05/10}{%
%   Add \cmd{\initrandomlist}.
% }
% \changes{v1.0}{2015/12/27}{%
%   randomlist completly rewritten.
% }
% \changes{v1.1}{2016/01/16}{%
%   Tons of improvements due to Christian Tellechea.
% }
% \changes{v1.2}{2016/07/13}{%
%   First public release.
% }
% \changes{v1.3}{2017/09/11}{%
%   Random operations are \global.
% }
% \CheckSum{0}
% \iffalse
%<*latex>
% \fi
% \subsection{\LaTeX's wrapper}
% \subsubsection{Introduction}
% We start with release number and date.
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}[1995/06/01]
\ProvidesPackage{randomlist}
         [2017/09/11 v1.3 Package for random list (JCC, CT)]
%    \end{macrocode}
% \LaTeX's wrapper has the possibility to use option. There is only
% one option: the seed one. It requires the \package{(x)keyval} package.
%    \begin{macrocode}
\RequirePackage{xkeyval}
\DeclareOptionX{seed}{\gdef\RL@seed{#1}}
\ExecuteOptions{seed=0}
\ProcessOptionsX
%    \end{macrocode}
% We can now call the real randomlist code!
%    \begin{macrocode}
\input{randomlist}
%    \end{macrocode}
% \subsubsection{\LaTeX{} lists}
% Obviously, \LaTeX{} lists are useful only with \LaTeX{}!
% \begin{macro}{\RandomItemizeList}
% Build an itemize list with random placement of items.
%    \begin{macrocode}
\NewList{*RandomList*}
\def\RandomItemizeList{%
  \def\RL@Type{itemize}%
  \ClearList{*RandomList*}%
  \@ifnextchar\bgroup{\@randomlist}{\@@randomlist}%
}
\long\def\@randomlist#1{%
  \InsertRandomItem{*RandomList*}{#1}%
  \@ifnextchar\bgroup{\@randomlist}{\@@randomlist}%
}
\def\@@randomlist{%
  \long\edef\RL@body{\noexpand\begin{\RL@Type}}%
  \RLfor \RL@var = 0 to \RL@lenof{*RandomList*}-1 \do{%
    \long\edef\RL@body{%
      \unexpanded\expandafter{\RL@body}%
      \unexpanded\expandafter{%
        \expandafter\item \csname *RandomList*-\RL@var\endcsname
      }%
    }%
  }%
  \long\edef\RL@body{\unexpanded\expandafter{\RL@body}\noexpand\end{\RL@Type}}%
  \RL@body
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RandomEnumerateList}
% Like |randomitemize| but for enumerate list.
%    \begin{macrocode}
\newcommand*\RandomEnumerateList{%
  \def\RL@Type{enumerate}
  \ClearList{*RandomList*}%
  \@ifnextchar\bgroup{\@randomlist}{\@@randomlist}
}
%    \end{macrocode}
% \end{macro}
% That's all for the \LaTeX's wrapper!
% \iffalse
%</latex>
%<*tex>
% \fi
% \subsection{\TeX{} code}
% At the beginning, we have to deal with multiple call and |@|'s
% catcode.
%    \begin{macrocode}
\csname RandomListLoaded\endcsname
\let\RandomListLoaded\endinput
\edef\RLAtCatcode{\the\catcode`\@}
\catcode`\@=11
%    \end{macrocode}
% If we aren't under \LaTeX{} then we need some \LaTeX{}
% commands. It's just a copy of \LaTeX{}$2\epsilon$ code.
%    \begin{macrocode}
\ifx\@ifnextchar\@undefined
%    \end{macrocode}
% Definition of |\@ifnextchar|.
%    \begin{macrocode}
  \long\def\@ifnextchar#1#2#3{%
    \let\reserved@d=#1%
    \def\reserved@a{#2}%
    \def\reserved@b{#3}%
    \futurelet\@let@token\@ifnch}
  \def\@ifnch{%
    \ifx\@let@token\@sptoken
      \let\reserved@c\@xifnch
    \else
      \ifx\@let@token\reserved@d
        \let\reserved@c\reserved@a
      \else
        \let\reserved@c\reserved@b
      \fi
    \fi
    \reserved@c}
  \def\:{\let\@sptoken= } \:  %
  \def\:{\@xifnch} \expandafter\def\: {\futurelet\@let@token\@ifnch}
\fi
%    \end{macrocode}
% Definition of |\PackageError| and some \LaTeX{} functions when
% running under \TeX{}.
%    \begin{macrocode}
\ifx\PackageError\@undefined
  \long\def\@firstoftwo#1#2{#1}
  \long\def\@secondoftwo#1#2{#2}
  \def\@nnil{\@nil}%
  \alloc@7\write\chardef\sixt@@n\@unused
  \def\typeout#1{\immediate\write\@unused{#1}}%
  \def\@spaces{\space\space\space\space}
  \def\PackageError#1#2#3{%
    \begingroup
    \newlinechar`\^^J
    \edef\RL@temp{#3}%
    \expandafter\errhelp\expandafter{\RL@temp}%
    \typeout{%
      #1 error. \space See User's Manual for further information.^^J
      \@spaces\@spaces\@spaces\@spaces
      Type \space H <return> \space for immediate help.}%
    \errmessage{#2}%
    \endgroup
  }
\fi
%    \end{macrocode}
% We check if we work with an engine which contain at least
% e\TeX{}.
%    \begin{macrocode}
\ifx\numexpr\@undefined
    \begingroup
    \newlinechar`\^^J
    \errhelp{Run under etex, pdftex, xetex, luatex, ... but not under
      tex}%
    \typeout{%
      randomlist error. \space See User's Manual for further information.^^J
      \@spaces\@spaces\@spaces\@spaces
      Type \space H <return> \space for immediate help.}%
    \errmessage{You can't use randomlist under tex without etex extension.}%
    \endgroup
\fi
%    \end{macrocode}
% \begin{macro}{\@gobble}
% Redefine |\@gobble| if needed.
%    \begin{macrocode}
\ifx\@gobble\@undefined
  \long\def\@gobble#1{}
\fi
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RL@addtomacro}
% We needs to add some code to some macros sometimes.
%    \begin{macrocode}
\def\RL@addtomacro#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RL@ifempty}
% Test if something is empty. Execute code according to the answer.
%    \begin{macrocode}
\def\RL@ifempty#1{%
  \ifcat\relax\detokenize{#1}\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
%    \end{macrocode}
% \end{macro}
% PDF\LaTeX, and X$_\mathrm{E}$\TeX know the primitives
% |\pdfsetrandomseed| and |\pdfuniformedeviate| but lua\TeX{} didn't
% know those primitives. Then we have to process ``by hand''!
% \begin{macro}{\RLsetrandomseed}
% We define the macro which give the initial seed. If the argument is
% zero, the value is a mixture of actual time, day, month and year. If
% the argument is nonzero, we process a new randomseed.
%
% The actual random number is stored in |\RL@random|.
%    \begin{macrocode}
\newcount\RL@random
\newcount\RL@random@a
\newcount\RL@random@b
\def\RLsetrandomseed#1{%
  \ifnum#1=0
    \global\RL@random \numexpr \time + \year * \month * \day \relax
  \else
    \global\RL@random \numexpr \ifnum#1<0 -\fi#1 \relax
  \fi
}
%    \end{macrocode}
% If |\RL@seed| exists -- that is, if we run under \LaTeX{} -- we process
% the seed (option of \LaTeX{} package). Otherwise, we use the zero
% value.
%    \begin{macrocode}
\ifx\RL@seed\@undefined
  \RLsetrandomseed{0}
\else
  \RLsetrandomseed{\RL@seed}
\fi
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RL@nextrand}
% Process the next random number using Linear Congruentiel Generator
% with Shrage's metthod.
%    \begin{macrocode}
\def\RL@nextrand{%
%    \end{macrocode}
% Use the LCG with :
% \[x_{n+1} = 7^5 \times x_n \pmod{2^{31}-1}.\]
% For that we take:
% \begin{itemize}
% \item $7^5=16807$;
% \item $2^{31}-1 = 2147483647$;
% \item $q = \mathrm{E}\left(\frac{2^{31}-1}{7^5}\right)=127773$;
% \item $r = 2^{31}-1 \pmod{7^5} = 2836$.
% \end{itemize}
% Then:
% \[x_{n+1} = 7^5(x_n \pmod{q}) - r\times\mathrm{E}\left(\frac{x_n}{q}\right).\]
% If $x_{n+1} < 0$ then $x_{n+1} = x_{n+1} + 2^{31}-1$
%    \begin{macrocode}
  \global\RL@random@a=\RL@random
  \global\divide\RL@random@a 127773
  \global\RL@random@b=\RL@random@a
  \global\multiply\RL@random@a -2836
  \global\multiply\RL@random@b -127773
  \global\advance\RL@random\RL@random@b
  \global\multiply\RL@random 16807
  \global\advance\RL@random\RL@random@a
%    \end{macrocode}
% If random number is negative add $2^{31}-1$.
%    \begin{macrocode}
  \ifnum\RL@random<0
    \global\advance\RL@random 2147483647
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RLuniformdeviate}
% Use |\RL@nextrand| to calculate a random integer between 0
% (inclusive) and |#1| (exclusive). Store the result in macro |#2|.
%    \begin{macrocode}
\def\RLuniformdeviate#1#2{%
%    \end{macrocode}
% Compute the next random number |\RL@random|.
%    \begin{macrocode}
  \RL@nextrand
%    \end{macrocode}
% Compute $|\RL@random| \pmod{\mathtt{\string#1}}$.
%    \begin{macrocode}
  \global\RL@random@a=\RL@random
  \global\RL@random@b=\RL@random
  \global\divide\RL@random@a \numexpr#1\relax
  \global\RL@random@b \numexpr\RL@random@b - \RL@random@a * (#1)\relax
  \expandafter\edef\csname #2\endcsname{\number\RL@random@b}%
}%
%    \end{macrocode}
% \end{macro}
% \subsubsection{Introduction and first commands}
% \begin{macro}{\@ifIsList}
% Test if a list exists and then executes true code or false code. For
% that the list of list names is stored inside the token register
% |\@ListOfList|. Each name is separed to the next one by a ``|\sep|''
% markup.
%    \begin{macrocode}
\newtoks\@ListOfList
%    \end{macrocode}
% |\@ifIsList| test if the list |#1| exists. If yes then it executes the
% next argument else it executes the third argument. Test must be
% executed on an expanded argument.
%    \begin{macrocode}
\def\@ifIsList#1{%
  \expandafter\@ifIsList@\expandafter{#1}%
}
\def\@ifIsList@#1{%
  \def\@@ifIsList##1#1\sep##2\@@ifIsList{%
    \csname @\ifx\empty##2\empty second\else first\fi oftwo\endcsname
  }%
  \expandafter\@@ifIsList\the\@ListOfList#1\sep\@@ifIsList
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RL@lenof}
% Shortcut allowing to get the len of a list
%    \begin{macrocode}
\def\RL@lenof#1{\csname #1-len\endcsname}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@ifIsListNotEmpty}
% Test if a list exist and isn't empty. If double yes then it executes
% the second argument else it executes the third one.
%    \begin{macrocode}
\newif\if@EmptyListFound
\def\@ifIsListNotEmpty#1{%
  \global\@EmptyListFoundfalse
  \@ifIsList{#1}{%
    \ifnum\RL@lenof{#1}=0
      \global\@EmptyListFoundtrue
      \expandafter\@secondoftwo
    \else
      \expandafter\@firstoftwo
    \fi
  }%
  \@secondoftwo
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@NoListError}
% Error for an unexisting list or an empty list.
%    \begin{macrocode}
\def\@NoListError#1{%
  \if@EmptyListFound
    \@EmptyListError{#1}%
    \global\@EmptyListFoundfalse
  \else
    \PackageError{randomlist}%
                 {List #1 doesn't exist}%
                 {Maybe you mistyped the list name?}%
  \fi
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@EmptyListError}
% Error for an empty list.
%    \begin{macrocode}
\def\@EmptyListError#1{%
  \if@EmptyListFound
  \PackageError{randomlist}%
               {List #1 is empty}%
               {Ask yourself why this list is empty.}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\@OutOfRangeError}
% Error for index out of range.
%    \begin{macrocode}
\def\@OutOfRangeError#1#2{%
  \PackageError{randomlist}%
               {Index #2 is greater than last index of list #1}%
               {There aren't enough elements in the list.}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RL@nameldef}
% |\long| version of |\@namedef|.
%    \begin{macrocode}
\long\def\RL@nameldef#1{%
  \long\expandafter\def\csname #1\endcsname
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RL@nameledef}
% All the macros in \package{randomlist} are long ones. It's
% useless for now since there isn't argument but it's a precaution
% for the future.
%
% |\long| version of |\@nameedef|.
%    \begin{macrocode}
\long\def\RL@nameledef#1{%
  \long\expandafter\edef\csname #1\endcsname
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RL@namelgdef}
% |\long| version of |\@namegdef|.
%    \begin{macrocode}
\long\def\RL@namelgdef#1{%
  \long\expandafter\gdef\csname #1\endcsname
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RL@namelxdef}
% |\long| version of |\@namexdef|.
%    \begin{macrocode}
\long\def\RL@namelxdef#1{%
  \long\expandafter\xdef\csname #1\endcsname
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RL@let}
% |\let| between two macros (with just the names)
%    \begin{macrocode}
\def\RL@let#1#2{%
  \expandafter\let\csname#1\expandafter\endcsname\csname#2\endcsname
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RLfor}
% Loop without group. Syntax is |\RLfor<var>=<begin>to<end>\do|
%    \begin{macrocode}
\long\def\RL@doafterfi#1\fi{\fi#1}
\def\RLfor#1=#2to#3\do{%
%    \end{macrocode}
% Set the variable.
%    \begin{macrocode}
  \edef#1{\number\numexpr#2}%
%    \end{macrocode}
% Set |\RL@sgncomp| to |<+| or |>-| if variable is greater or less
% than end
%    \begin{macrocode}
  \edef\RL@sgncomp{\ifnum#1<\numexpr#3\relax>+\else<-\fi}%
%    \end{macrocode}
% Call auxiliary macro with five parameters:
%    \begin{macrocode}
  \expandafter\RLfor@i
%    \end{macrocode}
% First argument (sub-recursive macro name build with the <variable>
% name).
%    \begin{macrocode}
  \csname RLfor@ii@\string#1\expandafter\endcsname\expandafter
%    \end{macrocode}
% Second argument (max).
%    \begin{macrocode}
  {\number\numexpr#3\expandafter}%
%    \end{macrocode}
% Third and fourth arguments since |\RL@sgncomp| is ``|<+|'' or ``|>-|''.
%    \begin{macrocode}
  \RL@sgncomp
%    \end{macrocode}
% fifth argument (variable name).
%    \begin{macrocode}
  #1%
}
%    \end{macrocode}
% Auxiliary macro:
% \begin{itemize}
% \item |#1| recursive macro name (like |\RLfor@ii@<var>|;
% \item |#2| max integer;
% \item |#3| ``|<|'' or ``|>|'';
% \item |#4| ``|+|'' or ``|-|'' (incrementation or decrementation);
% \item |#5| variable name;
% \item |#6| code to execute.
% \end{itemize}
%    \begin{macrocode}
\long\def\RLfor@i#1#2#3#4#5#6{%
%    \end{macrocode}
% Define the recursive submacro.
%    \begin{macrocode}
  \def#1{%
%    \end{macrocode}
% While <var> isn't greater than max
%    \begin{macrocode}
    \unless\ifnum#5#3#2\relax
%    \end{macrocode}
% In order to have a tail recursion.
%    \begin{macrocode}
    \RL@doafterfi{%
%    \end{macrocode}
% Execute the loop code.
%    \begin{macrocode}
      #6%
%    \end{macrocode}
% Increment <variable> by one.
%    \begin{macrocode}
      \edef#5{\number\numexpr#5#41\relax}%
%    \end{macrocode}
% And repeat.
%    \begin{macrocode}
      #1%
    }%
    \fi
  }%
%    \end{macrocode}
% submacro recursive call.
%    \begin{macrocode}
  #1%
}
%    \end{macrocode}
% \end{macro}
% \subsubsection{General list commands}
% \begin{macro}{\NewList}
% The main structure is the list. A list |L| is a collection of macros
% |L-<n>| where |<n>| is an index (starting from zero) and a macro
% |L-len| which store the len of the list, i.e. the last index plus
% one.
%
% When a new list is created, its name is stored in |@ListOfList|. A
% macro is also created for accessing data.
%    \begin{macrocode}
\def\NewList#1{%
  \@ifIsList{#1}{%
%    \end{macrocode}
% If a list with the same name exists then raise an error.
%    \begin{macrocode}
    \PackageError{randomlist}%
                 {List #1 already exists}%
                 {Use \string\ClearList.}%
  }%
  {%
%    \end{macrocode}
% When a list |MyName| is created, the macros |\MyName| and
% |\MyName-len| are created and there will be macros |\MyName-<n>| to
% store data. Then \package{randomlist} prohibit the name |MyName| for
% a list if the macro |\MyName| already exists.
%    \begin{macrocode}
    \ifcsname #1\endcsname
      \PackageError{randomlist}%
                   {Command \csname#1\endcsname already exists}%
                   {Creating list #1 defines a \csname#1\endcsname command.}%
    \else
%    \end{macrocode}
% If everything is fine, create the len macro which store the len of
% the list (starting with 0);
%    \begin{macrocode}
      \RL@nameldef{#1-len}{0}%
%    \end{macrocode}
% append the list name to the list of names |\@ListOfList|;
%    \begin{macrocode}
      \@ListOfList\expandafter{\the\@ListOfList#1\sep}%
%    \end{macrocode}
% and create the |\Mylist[<index>]| macro.
%    \begin{macrocode}
      \expandafter\def\csname #1\endcsname[##1]{%
%    \end{macrocode}
% If index is to big, the macro is |\relax| (a sort of undefined
% without error).
%    \begin{macrocode}
        \ifnum##1>\csname#1-len\endcsname
          \relax
        \else
          \csname #1-##1\endcsname
        \fi
      }%
    \fi
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ClearList}
% |\ClearList| erases a list. It sets the length to zero. There is no
% need to erase all the |\Mylist-<index>| macros.
%    \begin{macrocode}
\def\ClearList#1{%
  \@ifIsList{#1}{%
%    \end{macrocode}
% Clear the list if it exists.
%    \begin{macrocode}
     \RL@nameldef{#1-len}{0}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\CopyList}
% Copy list |#1| in list |#2|.
%    \begin{macrocode}
\def\CopyList#1#2{%
  \@ifIsList{#1}{%
    \@ifIsList{#2}{%
      \RL@let{#2-len}{#1-len}%
      \ifnum\RL@lenof{#1}>0
        \RLfor\RL@iter=0 to \RL@lenof{#1}-1 \do{%
          \RL@let{#2-\RL@iter}{#1-\RL@iter}%
        }%
      \fi
    }%
    {\@NoListError{#2}}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\InsertList}
% Insert List |#3| to the list |#1| starting at index |#2|. 
%    \begin{macrocode}
\def\InsertList#1#2#3{%
  \@ifIsList{#1}{%
    \@ifIsList{#3}{%
      \ifnum #2>\RL@lenof{#1}
        \@OutOfRangeError{#1}{#2}%
      \else
        \ShiftList{#1}{#2}{\RL@lenof{#3}}%
        \ifnum\RL@lenof{#3}>0
          \RLfor\RL@iter=0 to \RL@lenof{#3}-1 \do{%
            \RL@let{#1-\number\numexpr\RL@iter+#2}{#3-\RL@iter}%
          }%
        \fi
      \fi
    }%
    {\@NoListError{#3}}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ShowList}
% Macro for debugging purpose. First we declare some scratch count
% registers.
%    \begin{macrocode}
\newcount\RL@counti
\newcount\RL@countii
\newcount\RL@countiii
\def\ShowList#1{%
%    \end{macrocode}
% We show a list only if this list exists!
%    \begin{macrocode}
  \@ifIsList{#1}{%
    \ifhmode\par\noindent\fi
%    \end{macrocode}
% Typeset |BEGIN{MyList}|. As we typeset braces, we have to use
% ttfamily, then put the material inside a group.
%    \begin{macrocode}
    \begingroup
      \ifdefined\ttfamily\ttfamily\else\tt\fi
      BEGIN\detokenize{{#1}}
%    \end{macrocode}
% Typeset the number of elements.
%    \begin{macrocode}
      (\ifcase\RL@lenof{#1}
        empty list%
      \or
        1 element%
      \else
        \RL@lenof{#1} elements%
      \fi)\par
%    \end{macrocode}
% Loop to typeset element one after one.
%    \begin{macrocode}
      \ifnum\RL@lenof{#1}>0
        \parindent=1em
        \RLfor\RL@iter=0 to \RL@lenof{#1}-1 \do {%
          #1[\RL@iter] = \expandafter\RL@meaning\csname
          #1-\RL@iter\endcsname
          \par
        }%
      \fi
%    \end{macrocode}
% Typeset |END{MyList}|.
%    \begin{macrocode}
      \noindent
      END\detokenize{{#1}}\par
    \endgroup
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\RL@meaning}
% Like \TeX{} primitive |\meaning| without prefix |(\long) macro:->|:
%    \begin{macrocode}
\def\RL@meaning#1{\expandafter\RL@meaningi\meaning#1}
\expandafter\def\expandafter\RL@meaningi\expandafter#\expandafter1\string>{}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\CountList}
% Count the number of elements in the list |#1|. Store it in |#2|.
%    \begin{macrocode}
\def\CountList#1#2{%
  \@ifIsList{#1}%
  {\RL@nameledef{#2}{\RL@lenof{#1}}}%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \subsubsection{Writing and reading list commands}
% \begin{macro}{\ShiftList}
% Shift the elements of a list left or right. The syntax is:
%
% |\ShiftList{list name}{start}{shift}|
%
% where |start| is the first index to shift and |shift| the number of
% shifting. If |shift| is positive, it is a right shift. If |shitf| is
% negative, it is a left shift.
%    \begin{macrocode}
\def\ShiftList#1#2#3{%
  \@ifIsList{#1}%
  {%
%    \end{macrocode}
% No action if |shift| is zero!
%    \begin{macrocode}
    \unless\ifnum#3=0
%    \end{macrocode}
% If |<start>| is negative, raise an error.
%    \begin{macrocode}
      \ifnum\numexpr#2<0
        \PackageError{randomlist}%
                     {Negative index number}%
                     {Index must be equal or greater than 0}%
      \else
%    \end{macrocode}
% If |<start>| is greater than the lists length, raise an error.
%    \begin{macrocode}
      \ifnum\numexpr#2>\RL@lenof{#1}\relax
        \PackageError{randomlist}%
                     {Index \number\numexpr #2\relax\space too big
                       (<=\RL@lenof{#1})}%
                     {Index must be equal or smaller than length of
                       the list}%
      \else
%    \end{macrocode}
% Here we have $0 \leq |<start>| \leq |len|(|<list>|)$.
%
% If |<shift>| is positive, we process a right shifting: it's alway
% possible.
%    \begin{macrocode}
      \ifnum\numexpr#3>0
        \RLfor\RL@iter = \RL@lenof{#1} to #2 \do{%
          \RL@let{#1-\number\numexpr\RL@iter+#3}{#1-\RL@iter}%
        }%
%    \end{macrocode}
% Empty the items out of shift part.
%    \begin{macrocode}
        \RLfor\RL@iter = #2 to #2 + #3 - 1 \do{%
          \RL@nameldef{#1-\RL@iter}{}%
        }%
      \else
        \ifnum-#3>\numexpr#2\relax
%    \end{macrocode}
% If the negative shifting is to big for index |#2| then raise an error.
%    \begin{macrocode}
          \PackageError{randomlist}%
                       {Negative shift to big}%
                       {When negative, shift must not be greater than index}%
        \else
%    \end{macrocode}
% Elsewhere, process the left shifting.
%    \begin{macrocode}
          \RLfor\RL@iter=#2 to \RL@lenof{#1} \do{%
            \RL@let{#1-\number\numexpr\RL@iter+#3}{#1-\RL@iter}%
          }%
        \fi
      \fi
%    \end{macrocode}
% Set the list length for both positive and negative shifting.
%    \begin{macrocode}
      \RL@nameledef{#1-len}{\number\numexpr\RL@lenof{#1} + #3}%
    \fi\fi\fi
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\InsertLastItem}
% Add an element |#2| at the end of the list |#1|.
%    \begin{macrocode}
\long\def\InsertLastItem#1#2{%
  \@ifIsList{#1}
  {%
    \RL@nameldef{#1-\RL@lenof{#1}}{#2}%
    \RL@nameledef{#1-len}{\number\numexpr\RL@lenof{#1}+1}%
  }
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\InsertFirstItem}
% Add an element |#2| at the beginning of the list |#1|. For that,
% shift right all the element and then put |#3| at |L[0]|.
%    \begin{macrocode}
\long\def\InsertFirstItem#1#2{%
  \InsertItem{#1}{0}{#2}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\InsertItem}
% Add an element |#3| at the position |#2| of the list |#1|. For that,
% pass from |L[0]| to |L[#2-1]| then shift right from |L[#2]| to
% |L[len]| and finally put |#3| at |L[#2]|. To do this, we must have
% |#2| $\geq$ |L-len|.
%    \begin{macrocode}
\long\def\InsertItem#1#2#3{%
  \@ifIsList{#1}%
  {%
    \ShiftList{#1}{#2}{1}%
    \RL@nameldef{#1-#2}{#3}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\InsertRandomItem}
% Insert element |#2| in a random position of list |#1|.
%    \begin{macrocode}
\long\def\InsertRandomItem#1#2{%
  \@ifIsList{#1}%
  {%
    \RLuniformdeviate{\RL@lenof{#1}+1}{RL@temp}%
    \InsertItem{#1}{\RL@temp}{#2}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ExtractFirstItem}
% Extract the first element of list |#1| and store it in |#2|.
%    \begin{macrocode}
\def\ExtractFirstItem#1#2{%
  \@ifIsList{#1}%
  {%
    \ExtractItem{#1}{0}{#2}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ExtractLastItem}
% Extract the last element of list |#1| and store it in |#2|.
%    \begin{macrocode}
\def\ExtractLastItem#1#2{%
  \@ifIsListNotEmpty{#1}%
  {%
    \RL@let{#2}{#1-\number\numexpr\RL@lenof{#1}-1}%
    \RL@nameledef{#1-len}{\number\numexpr\RL@lenof{#1}-1}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ExtractItem}
% Extract the element at the position |#2| of the list |#1| and store
% it in |#3|.
%    \begin{macrocode}
\def\ExtractItem#1#2#3{%
  \@ifIsListNotEmpty{#1}%
  {%
    \RL@let{#3}{#1-#2}%
    \ShiftList{#1}{#2+1}{-1}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ExtractRandomItem}
% Extract element in a random position of list |#1| and store it in |#2|.
%    \begin{macrocode}
\def\ExtractRandomItem#1#2{%
  \@ifIsListNotEmpty{#1}%
  {%
    \RLuniformdeviate{\RL@lenof{#1}}{RL@temp}%
    \ExtractItem{#1}{\RL@temp}{#2}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ExtractList}
% |ExtractList| extract a list from a list. There are four arguments:
% \begin{itemize}
% \item |#1| is the list from which the extraction is made;
% \item |#2| is the starting index of extraction;
% \item |#3| is the ending index of extraction;
% \item |#4| is the list which receive the extracted list.
% \end{itemize}
%    \begin{macrocode}
\def\ExtractList#1#2#3#4{%
%    \end{macrocode}
% In order to do something, |#1| and |#2| must be lists, and indexes
% |#2| and |#3| must be inside list |#1|.
%    \begin{macrocode}
  \@ifIsList{#1}{%
    \@ifIsList{#4}{%
      \ifnum#2<\RL@lenof{#1}%
        \ifnum#3<\RL@lenof{#1}%
          \ifnum#2>#3\relax
%    \end{macrocode}
% If $\text{start} > \text{end}$ we build an empty list.
%    \begin{macrocode}
            \RL@nameldef{#4-len}{0}%
          \else
%    \end{macrocode}
% If $\text{start} \leq \text{end}$ we build a real extracted list. We
% have to be careful because |\ExtractItem| uses the loop variable
% |\RL@iter|. Then we use another loop variable. 
%    \begin{macrocode}
            \RLfor\RL@iterextract=0 to #3 - #2 \do{%
              \RL@let{#4-\RL@iterextract}{#1-#2}%
              \ExtractItem{#1}{#2}{RL@temp}%
            }%
            \RL@nameledef{#4-len}{\number\numexpr #3 - #2 + 1}%
          \fi
        \else
          \@OutOfRangeError{#1}{#3}%
        \fi
      \else
        \@OutOfRangeError{#1}{#2}%
      \fi
    }%
    {\@NoListError{#4}}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\GetFirstItem}
% Get the first element of list |#1| and store it in |#2|.
%    \begin{macrocode}
\def\GetFirstItem#1#2{%
  \GetItem{#1}{0}{#2}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\GetLastItem}
% Get the last element of list |#1| and store it in |#2|.
%    \begin{macrocode}
\def\GetLastItem#1#2{%
  \GetItem{#1}{\number\numexpr\RL@lenof{#1}-1}{#2}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\GetItem}
% Get the element of rank |#2| of list |#1| and store it in |#3|.
%    \begin{macrocode}
\def\GetItem#1#2#3{%
  \@ifIsListNotEmpty{#1}
  {%
    \ifnum\numexpr\RL@lenof{#1}-1-#2<0
      \@OutOfRangeError{#1}{#2}%
    \else
      \RL@let{#3}{#1-#2}%
    \fi
  }
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\GetRandomItem}
% Get element in a random position of list |#1| and store it in |#2|.
%    \begin{macrocode}
\def\GetRandomItem#1#2{%
  \@ifIsListNotEmpty{#1}%
  {%
    \RLuniformdeviate{\RL@lenof{#1}}{RL@temp}%
    \GetItem{#1}{\RL@temp}{#2}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\GetList}
% |\GetList| copy a sub-list from a list. There are four arguments:
% \begin{itemize}
% \item |#1| is the list from which the reading is made;
% \item |#2| is the starting index of extraction;
% \item |#3| is the ending index of extraction;
% \item |#4| is the list which receive the readen items.
% \end{itemize}
%    \begin{macrocode}
\def\GetList#1#2#3#4{%
%    \end{macrocode}
% In order to do something, |#1| and |#2| must be lists, and indexes
% |#2| and |#3| must be inside list |#1|.
%    \begin{macrocode}
  \@ifIsList{#1}{%
    \@ifIsList{#4}{%
      \ifnum#2<\RL@lenof{#1}%
        \ifnum#3<\RL@lenof{#1}%
          \ifnum#2>#3\relax
%    \end{macrocode}
% If $\text{start} > \text{end}$ we build an empty list.
%    \begin{macrocode}
            \RL@nameldef{#4-len}{0}%
          \else
%    \end{macrocode}
% If $\text{start} \leq \text{end}$ we build a real extracted list.
%    \begin{macrocode}
            \RLfor\RL@iter=#2 to #3 \do{%
              \RL@let{#4-\number\numexpr \RL@iter - #2}{#1-\RL@iter}%
            }%
            \RL@nameledef{#4-len}{\number\numexpr #3 - #2 + 1}%
          \fi
        \else
          \@OutOfRangeError{#1}{#3}%
        \fi
      \else
        \@OutOfRangeError{#1}{#2}%
      \fi
    }%
    {\@NoListError{#4}}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SetFirstItem}
% Set the first element of list |#1| with value |#2|.
%    \begin{macrocode}
\long\def\SetFirstItem#1#2{%
  \SetItem{#1}{0}{#2}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SetLastItem}
% Set the last element of list |#1| with value |#2|.
%    \begin{macrocode}
\long\def\SetLastItem#1#2{%
  \SetItem{#1}{\number\numexpr\RL@lenof{#1}-1}{#2}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SetItem}
% Set the |#2| element of list |#1| with value |#3|.
%    \begin{macrocode}
\long\def\SetItem#1#2#3{%
  \@ifIsListNotEmpty{#1}%
  {%
    \ifnum\numexpr\RL@lenof{#1}-1-#2<0
      \@OutOfRangeError{#1}{#2}%
    \else
      \RL@nameldef{#1-#2}{#3}%
    \fi
  }%
  {\@NoListError{#1}}%%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SetRandomItem}
% Set element in a random position of list |#1| with value |#2|.
%    \begin{macrocode}
\long\def\SetRandomItem#1#2{%
  \@ifIsListNotEmpty{#1}%
  {%
    \RLuniformdeviate{\RL@lenof{#1}}{RL@temp}%
    \SetItem{#1}{\RL@temp}{#2}%
  }%
  {\@NoListError{#1}}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\SetList}
% |\SetList| allow to give multiple values to a list. This function
% acts like a repetition of |\InsertLastItem|.
%    \begin{macrocode}
\def\SetList#1#2{%
  \@ifIsList{#1}%
  {%
    \ClearList{#1}%
    \def\RL@name{#1}%
    \RL@setlist#2,\@nil,%
  }%
  {\@NoListError{#1}}%
}
\long\def\RL@setlist#1,{%
  \def\RL@arg{#1}%
  \unless\ifx\RL@arg\@nnil
    \InsertLastItem{\RL@name}{#1}%
    \expandafter\RL@setlist
  \fi
}
%    \end{macrocode}
% \end{macro}
% \subsubsection{Loop on list}
% \begin{macro}{\ForEachFirstItem}
% |\ForEachFirstItem| typesets |#3| for each element of the list |#1|
% extracting the actual first element (stored in |#2|).
%    \begin{macrocode}
\long\def\ForEachFirstItem#1#2#3{%
  \begingroup
    \RLfor \RL@var = 0 to \RL@lenof{#1}-1 \do{%
      \ExtractFirstItem{#1}{#2}%
      #3%
    }%
  \endgroup
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ForEachLastItem}
% |\ForEachLastItem| typesets |#3| for each element of the list |#1|
% extracting the actual last element  (stored in |#2|).
%    \begin{macrocode}
\long\def\ForEachLastItem#1#2#3{%
  \begingroup
    \RLfor \RL@var = 0 to \RL@lenof{#1}-1 \do{%
      \ExtractLastItem{#1}{#2}%
      #3%
    }%
  \endgroup
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ForEachRandomItem}
% |\ForEachRandomItem| typesets |#3| for each element of the list |#1|
% extracting randomly an element (stored in |#2|).
%    \begin{macrocode}
\long\def\ForEachRandomItem#1#2#3{%
  \begingroup
    \RLfor \RL@var = 0 to \RL@lenof{#1}-1 \do{%
      \ExtractRandomItem{#1}{#2}%
      #3%
    }%
  \endgroup
}
%    \end{macrocode}
% \end{macro}
% \subsubsection{Database}
% \begin{macro}{\ReadFieldItem}
% Macro |\ReadFieldItem| read a field in a record.
%
% A record is a sequence of groups, each group is a field.
% \begin{itemize}
% \item |#1| is the record (sequence of groups;
% \item |#2| is the index of item (starting at zero);
% \item |#3| is the macro name which store the field.
% \end{itemize}
%    \begin{macrocode}
\long\def\ReadFieldItem#1#2#3{%
%    \end{macrocode}
% Store the field's index.
%    \begin{macrocode}
  \RL@counti #2\relax
%    \end{macrocode}
% Call the recursive macro
%    \begin{macrocode}
  \expandafter\RL@ReadFieldItem#1\@nil
%    \end{macrocode}
% Store the result in macro |\#3|.
%    \begin{macrocode}
  \expandafter\let\csname#3\endcsname\RL@temp
}
%    \end{macrocode}
% In fact the first recusive call check for a left brace. A record
% must contain at least one field otherwise an error message is
% raised.
%    \begin{macrocode}
\long\def\RL@ReadFieldItem{%
  \@ifnextchar\bgroup{\RL@@ReadFieldItem}{\RL@@ReadFieldItemError}%
}
%    \end{macrocode}
% 
%    \begin{macrocode}
\long\def\RL@@ReadFieldItem#1{%
  \ifnum\RL@counti=\z@
    \def\RL@temp{#1}%
    \expandafter\RL@@ReadFieldItemEnd
  \else
    \advance\RL@counti \m@ne
    \expandafter\RL@ReadFieldItem
  \fi
}
\long\def\RL@@ReadFieldItemEnd#1\@nil{}
\long\def\RL@@ReadFieldItemError#1\@nil{%
  \PackageError{randomlist}%
               {There aren't enough fields in the record}%
               {Pay attention that field number starts from zero.}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ReadFileList}
% First, we look for special delimiters for fields and strings. By
% default, the delimiter for fields is the comma and the delimiter for
% string is the double quote.
%    \begin{macrocode}
\def\RL@SetDelimiters#1#2#3\@nil{%
%    \end{macrocode}
% \begin{itemize}
% \item argument |#1| is the field separator;
% \item argument |#2| is the string delimiter;
% \item argument |#3| is the remainder to ignore.
% \end{itemize}
%    \begin{macrocode}
  \def\RL@markstrings##1{%
    \let\RL@accu\empty
    \expandafter\RL@markstrings@i##1#2\@nil#2%
    \let##1=\RL@accu
  }%
  \def\RL@markstrings@i##1#2##2#2{%
    \RL@addtomacro\RL@accu{##1}%
    \def\RL@current{##2}%
    \unless\ifx\@nnil\RL@current
      \RL@addtomacro\RL@accu{\RL@string{##2}}%
      \expandafter\RL@markstrings@i
    \fi
  }%
  \def\RL@unmarkstrings##1{%
    \let\RL@accuA\empty
    \expandafter\RL@unmarkstrings@i##1\RL@string\@nil
    \let##1=\RL@accuA
  }%
  \def\RL@unmarkstrings@i##1\RL@string##2{%
    \RL@addtomacro\RL@accuA{##1}%
    \def\RL@current{##2}%
    \unless\ifx\@nnil\RL@current
      \RL@ifempty{##2}%
        {\RL@addtomacro\RL@accuA{#2}}%
        {\RL@addtomacro\RL@accuA{##2}}%
      \expandafter\RL@unmarkstrings@i
    \fi
  }%
  \def\RL@parsefields##1{%
    \let\RL@accu\empty
    \expandafter\RL@parsefields@i##1#1\@nil#1%
    \let##1=\RL@accu
  }%
  \def\RL@parsefields@i##1#1{%
    \def\RL@current{##1}%
    \unless\ifx\@nnil\RL@current
      \RL@unmarkstrings\RL@current
      \RL@removefirstspaces\RL@current
      \RL@removelastspaces \RL@current
      \expandafter\RL@addtomacro\expandafter\RL@accu\expandafter
        {\expandafter{\RL@current}}%
      \expandafter\RL@parsefields@i
    \fi
  }%
}
%    \end{macrocode}
% The macro |\ReadFileList| uses a handle for the reading file. it
% needs also a macro to detect |\par|
%    \begin{macrocode}
\newread\RL@hdle
\def\@ppar{\par}
%    \end{macrocode}
% At first, |\ReadFileList| check for an optionnal argument giving
% delimiters. By default, delimiters are comma for field separator and
% double quote for string delimiter.
%    \begin{macrocode}
\def\ReadFileList{\@ifnextchar[{\@ReadFileList}{\@ReadFileList[,"]}}
%    \end{macrocode}
% \begin{itemize}
% \item |#1| contains the delimiters;
% \item |#2| is the data base name;
% \item |#3| is the file name.
% \end{itemize}
%    \begin{macrocode}
\def\@ReadFileList[#1]#2#3{%
  \openin \RL@hdle = #3
  \ifeof\RL@hdle
    \PackageError{randomlist}%
      {File #3 doesn't exist}%
      {Verify its name, its extension, its location, its permissions.}%
  \else
%    \end{macrocode}
% If the optionnal argument is empty then raise an error and take the
% comma and the double quote instead.
%    \begin{macrocode}
    \RL@ifempty{#1}%
      {%
        \PackageError{randomlist}
          {Optional argument empty: [,"] inserted}
          {Do not leave an optional argument empty}%
          \RL@SetDelimiters,"\@nil
      }
%    \end{macrocode}
% Else add double quote to for security.
%    \begin{macrocode}
      {\RL@SetDelimiters#1"\@nil}%
%    \end{macrocode}
% The main loop read each line of the file. Don't process anything if
% the line is empty (it could be the very end of the file).
%    \begin{macrocode}
    \loop
      \read\RL@hdle to \RL@buffer
      \unless\ifx\RL@buffer\@ppar
%    \end{macrocode}
% Mark the string
%    \begin{macrocode}
        \RL@markstrings\RL@buffer
%    \end{macrocode}
% and process the fields.
%    \begin{macrocode}
        \RL@parsefields\RL@buffer
%    \end{macrocode}
% Save current record with fields, that is, with sequence of groups.
%    \begin{macrocode}
        \def\RL@accuA{\InsertLastItem{#2}}%
        \expandafter\RL@accuA\expandafter{\RL@buffer}%
      \fi
      \ifeof\RL@hdle\else
    \repeat
  \fi
}
%    \end{macrocode}
% Check for a heading space.
%    \begin{macrocode}
\def\RL@ifspacefirst#1{%
  \RL@ifspacefirst@i#1A \@nil
}
\expandafter\def\expandafter\RL@ifspacefirst@i
  \expandafter#\expandafter1\space#2\@nil{%
    \RL@ifempty{#1}%
}
%    \end{macrocode}
%    \begin{macrocode}
% Remove all spaces at the start of argument (macro).
\def\RL@removefirstspaces#1{%
  \expandafter\RL@ifspacefirst\expandafter{#1}
    {\expandafter\removefistspace@i#1\@nil#1}
    {}%
}
\expandafter\def\expandafter\removefistspace@i\space#1\@nil#2{%
  \def#2{#1}%
  \RL@removefirstspaces#2%
}
%    \end{macrocode}
%    \begin{macrocode}
% Store |^^00|'s catcode
\edef\RL@restorecatcodezero{\catcode0=\number\catcode0\relax}
%    \end{macrocode}
% then set this catcode to other catcode.
%    \begin{macrocode}
% puis le modifie à 12.
\catcode0=12
%    \end{macrocode}
% Remove all heading and trailing spaces of argument (macro).
%    \begin{macrocode}
\def\RL@removelastspaces#1{%
  \expandafter\def\expandafter#1\expandafter{%
    \romannumeral\expandafter
    \RL@removelastspaces@i\expandafter\relax#1^^00 ^^00\@nil
  }%
}
\def\RL@removelastspaces@i#1 ^^00{\RL@removelastspaces@ii#1^^00}
\def\RL@removelastspaces@ii#1^^00#2\@nil{%
  \RL@ifspacefirst{#2}
    {\RL@removelastspaces@i#1^^00 ^^00\@nil}
    {\expandafter\z@\@gobble#1}%
}
%    \end{macrocode}
%    \begin{macrocode}
% Restore |^^00|'s catcode.
\RL@restorecatcodezero
%    \end{macrocode}
% \end{macro}
% At the very end of the package, we restore the |@|'s catcode.
%    \begin{macrocode}
\catcode`\@=\RLAtCatcode\relax
%    \end{macrocode}
% \iffalse
%</tex>
% \fi