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.

Ich bin auf der Suche nach einer Möglichkeit aus einer Datei folgender Struktur

1,a
2,a
2,a
2,a
3,a
3,a
4,a
4,a
4,a
4,a
5,a
5,a
6,a
6,a
7,a

folgendes zu machen

1 1
2 3
3 2
4 4
5 2
6 2
7 1

(Das A dient nur dazu um zu zeigen, dass es mehrere spalten geben kann...

ICh denke an das package datatool, aber ich kann das irgendwie nicht umsetzen, meine versuche basieren auf folgenden überlegungen....

datenbank laden.... in der datenbank nach dem minimum suchen counter erhöhen, zeile löschen bis die datenbank das erste mal durch ist.... dann neue zeile in neuer datenbank.... und den counter übertragen....

dann die prozedurer wieder von vorne, bis die anzahl der daten null sind....

ergebnisdatenbank sortieren...hm....

an der Umsetzung scheitert es; ich schaffe es nicht zwei ineinander geschachtelte for schleifen innerhalb von datatool zu realisieren...

LG Goldnas

share|improve this question

closed as too localized by doncherry, lockstep, Paulo Cereda, topskip, Marco Daniel Aug 9 '12 at 13:12

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
Willkommen! Welcome to TeX.sx!. Please have a look at Non-English questions –  percusse Aug 5 '12 at 17:08
    
Das heißt, du willst eine Häufigkeit der Zellen ermitteln? Ist datatool dazu das richtige Werkzeug? Ich würde eher R nehmen. –  Uwe Ziegenhagen Aug 5 '12 at 18:53

1 Answer 1

up vote 6 down vote accepted
\documentclass{article}
\usepackage{datatool}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.csv}
1,a
2,a
2,a
2,a
3,a
3,a
4,a
4,a
4,a
4,a
5,a
5,a
6,a
6,a
7,a
\end{filecontents*}

\DTLloaddb[noheader,keys={num}]{raw}{\jobname.csv}

\DTLnewdb{hist}
\newcounter{numcnt}

% fill table "hist"
\newcommand*{\SaveRow}{%
  \ifnum\value{numcnt}>0 %
    \DTLnewrow{hist}%
    \begingroup
      \dtlexpandnewvalue
      \DTLnewdbentry{hist}{num}{\oldnum}%
      \DTLnewdbentry{hist}{count}{\the\value{numcnt}}%
    \endgroup
  \fi
}%
\newcommand*{\oldnum}{}%
\setcounter{numcnt}{0}%
\DTLforeach*{raw}{\num=num}{%
  \ifx\oldnum\num
    \stepcounter{numcnt}%
  \else
    \SaveRow
    \setcounter{numcnt}{1}%
    \let\oldnum\num
  \fi
}%
\SaveRow

% write table "hist" with header
\DTLsetdelimiter{,}
\DTLsavedb{hist}{\jobname.hist}

\begin{document}   
Hello World% nothing would be written without output page
\end{document}

Result is database hist or the file \jobname.hist:

num,count
1,1
2,3
3,2
4,4
5,2
6,2
7,1

Remarks:

  • When writing a file, package datatool does not use \immediate, therefore a database file is writen at the next output of a page and is not written at all after the last page.

Variation, writing file \jobname.hist without headers:

\documentclass{article}
\usepackage{datatool}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.csv}
1,a
2,a
2,a
2,a
3,a
3,a
4,a
4,a
4,a
4,a
5,a
5,a
6,a
6,a
7,a
\end{filecontents*}

\DTLloaddb[noheader,keys={num}]{raw}{\jobname.csv}

\DTLnewdb{hist}
\newcounter{numcnt}

% fill table "hist" and manually write it
\makeatletter
\immediate\openout\@dtl@write=\jobname.hist\relax
% reusing datatool's write handle
\newcommand*{\SaveRow}{%  
  \ifnum\value{numcnt}>0 %
    \DTLnewrow{hist}%
    \begingroup
      \dtlexpandnewvalue
      \DTLnewdbentry{hist}{num}{\oldnum}%
      \DTLnewdbentry{hist}{count}{\the\value{numcnt}}%
      \immediate\write\@dtl@write{\oldnum,\the\value{numcnt}}%
    \endgroup
  \fi
}%
\newcommand*{\oldnum}{}%
\setcounter{numcnt}{0}%
\DTLforeach*{raw}{\num=num}{%
  \ifx\oldnum\num
    \stepcounter{numcnt}%
  \else
    \SaveRow
    \setcounter{numcnt}{1}%
    \let\oldnum\num
  \fi
}%
\SaveRow
\immediate\closeout\@dtl@write
\makeatother

\begin{document}
\end{document}

Again the result is database hist and file \jobname.hist, but this time the file is written without headers:

1,1
2,3
3,2
4,4
5,2
6,2
7,1

Remarks:

  • The (internal) write handle of package datatool is reused, because write handles are a very limited resource.
  • The data base is written immediately (\immediate\write) and not delayed to the next page output.
share|improve this answer

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