# Splitting file into lines and lines into parts

I'm working on a song book using LaTeX. It's early in the project, and the songs aren't all picked out yet, but I thought I'd fix the inner workings of it in the meantime.
Since many people will (hopefully) be involved, and many songs included, I want to save all songs separately and \input them.
The dream would be to have a text file where the songs are listed like

Thrift shop-macLemore
Two worlds, one family-from Tarzan
Gagnam Style-Psy

and for me to be able to loop through the list and say (Sorry for mixing languages):

for(line l : index.txt) {
String[] k = l.split('-');
\includesong{k[0]}{k[1]}
}


Is there any good way to do this?

EDIT: I found a package called parselines that does the job fairly well. Especially combined with a little regex-fu (Why is there so little information on the internet about \def\foo#1, #2?).

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format.
– user11232
Oct 9, 2013 at 23:24
• perhaps processing it like a csv database? If so, check the docsvlist and forcsvlist commands from the etoolbox package (see also this answer) Oct 9, 2013 at 23:25

\documentclass{article}
\def\splitdash#1-#2\relax{%
\def\thissong{#1}%
\def\thissource{#2}%
}
\begin{document}
\def\zpar{\par}
\openin\sngs=songs.txt
\loop
\ifx\tmp\zpar\else
\expandafter\splitdash\tmp\relax
\input{\thissong}
\fi
\ifeof\sngs
\else
\repeat

\end{document}


songs.txt:

Thrift shop-macLemore
Two worlds, one family-from Tarzan
Gagnam Style-Psy


This tries to input the three titles (you might want to normalise the filenames, removing spaces or whatever)

! LaTeX Error: File Thrift shop.tex' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: tex)

Enter file name:

! LaTeX Error: File Gagnam Style.tex' not found.
`