A simple script to show a table of contents like view of a latex file. I could make a couple of assumptions about the input based on my setup. My first attempt which is probably somewhere on my blog looked like this:
Perl:
if($#ARGV > 0) { $in_file_name = shift; }
else { $in_file_name = "thesis.tex"; }
sub read_file
{
while(<IN>) {
if(/^\\chapter.?
{(.+?
)}/
) {print "——————–\n$1\n";
}
elsif(/^\\section.?
{(.+?
)}/
) {print " . . $1\n";
}
elsif(/^\\subsection.?
{(.+?
)}/
) {print " . . . . $1\n";
}
elsif(/^\\subsubsection.?
{(.+?
)}/
) {print " . . . . . . $1\n";
}
elsif(/^\\paragraph.?
{(.+?
)}/
) {print " . . . . . . . . $1\n";
}
elsif(/^\\input{(.+?)}/) {read_file($1);}
}
}
read_file($in_file_name);
A classic recursive solution using a function. I had a quick play today and produced this:
Perl:
#!perl -n
/^\\chapter.?
{(.+?
)}/
and print "-" x20 .
"\n$1\n" or
/^\\section.?
{(.+?
)}/
and print " ." x2 .
" $1\n" or
/^\\subsection.?
{(.+?
)}/
and print " ." x4 .
" $1\n" or
/^\\subsubsection.?
{(.+?
)}/
and print " ." x6 .
" $1\n" or
/^\\paragraph.?
{(.+?
)}/
and print " ." x8 .
" $1\n" or
/^\\input
{(.+?
)}/
and system "perl list_sections2.pl $1";
Which is a lot more compact. It’s almost inefficient, but this doesn’t really matter for this problem (or at least the input I have).
Which one feels more Perl-y?
Or should I just give up on Perl and go back to my Pythonic ways?
No comments yet.