I have a latex document which in the main part lists a number of "tasks" (for students to solve); to each task belongs a "script", however, all scripts are supposed to be printed in an appendix at the end of the document. For this purpose, the environment "skript" is built to copy its content to a file and re-insert it at the end of the LaTeX run. Tasks and scripts are numbered, and the numbers should correspond to each other.
And here is the problem: TeX seems to expand the script number only after re-inserting the scripts at the end of the document. However, at that time, the task counter is obviously at its maximum value leading to all scripts getting the same number.
Is there a way to get TeX to expand the task counter before storing it to the external file?
A minimal example is the following (where count is the task number):
\documentclass{article}
\usepackage{skripts}
\newcounter{count}
\begin{document}
\refstepcounter{count}
Here comes task \thecount:
\begin{skript}
\thecount
\end{skript}
\refstepcounter{count}
Here comes task \thecount:
\begin{skript}
\thecount
\end{skript}
\refstepcounter{count}
Here comes task \thecount:
\begin{skript}
\thecount
\end{skript}
\refstepcounter{count}
Here comes task \thecount:
\begin{skript}
\thecount
\end{skript}
Now insert the scripts corresponding to the tasks at the document's end:
\includeSkripts
\end{document}
Which outputs:

Obviously, \thecount, when used inside the "skript" environment has been evaluated only after the counter has been set to its highest value.
The package "skripts" (also in a minimal version) looks as follows:
\ProvidesPackage{skripts}[2009/11/03 v0.1 Styledefinitionen]
\usepackage{verbatim}
\newenvironment{ex@skripts}[1]{#1}{}
\newwrite\verbatim@outSkr % Define file.
\immediate\openout\verbatim@outSkr=\jobname.skr % Open file for writing.
\def\skript{
\@bsphack
\let\do\@makeother\dospecials
\catcode`\^^M\active
\def\verbatim@processline{%
\immediate\write\verbatim@outSkr{\the\verbatim@line}}%
\immediate\write\verbatim@outSkr{\string\begin{ex@skripts}{Script}}
\verbatim@start}
\def\endskript{%
\immediate\write\verbatim@outSkr{\string\end{ex@skripts}}
\@esphack}
\newcommand*{\includeSkripts}{%
\immediate\closeout\verbatim@outSkr % Close file.
\InputIfFileExists{\jobname.skr}{}{}
\newwrite\verbatim@outSkr % Datei wird definiert
\immediate\openout\verbatim@outSkr=\jobname.skr % Open file for writing.
}
Now, I am aware that according to this minimal example, there are several solutions to my problem (such as giving "count" as a parameter to the "skript" environment). However, in my actual, more complex context, it is important to solve the problem in the described setting without any structural changes to the environment.
EDIT: I already tried this quite crazy hack:
\romannumeral-`X\foo
which I got from here.

skriptspackage, but you may want to look intoexpl3(texdoc interface3). It makes this kind of stuff a lot simpler. – Sean Allred Mar 16 '15 at 14:57pdflatex,biber,arara, etc.). It's primarily used to pull up package documentation (texdoc <package>), but there is other documentation, too. If you entertexdoc interface3into your terminal (or command line, same thing), it will open that PDF. – Sean Allred Mar 16 '15 at 15:07\newwritein\includeSkripts, otherwise you would run out of file handles – Mar 16 '15 at 15:15exsheetspackage – cgnieder Sep 06 '15 at 11:28