Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Parsing Exercise set

by jimbojones (Friar)
on Apr 13, 2005 at 22:25 UTC ( #447603=note: print w/ replies, xml ) Need Help??


in reply to Parsing Exercise set

Here's a go without using a big regex. It looks for begin{} and end{} keys and stores the text between the two in an anon array in a hash indexed by that key.

use strict; use warnings; #-- hash that will be keyed by Latex element my %in = (); while ( <DATA> ) { if ( /\\begin\{(\S+?)\}/ ) { #-- tell that we're in a block $in{$1}->{Status} = "in"; #-- add a new element to the anon array containing this info push @{$in{$1}->{Text}}, ""; } if ( /\\end\{(\S+?)\}/ ) { $in{$1}->{Status} = "pending"; } #-- now loop on all keys, see we are in that element. foreach my $key ( keys %in ) { my $status = $in{$key}->{Status}; if ( $status eq "in" || $status eq "pending" ) { #-- add text to last element of the array $in{$key}->{Text}->[$#{$in{$key}->{Text}}] .= $_; } $in{$key}->{Status} = "out" if $status eq "pending"; } } #-- write it out. Here loop over all possible keys. Could be restricte +d to # 'exertext', 'answers', 'soln' foreach my $key ( keys %in ) { my $file = $key . ".tex"; print "Creating file '$file'\n"; open( FILE, '>', $file) or die "Cannot open file '$file' for writing: + $!\n"; #-- write each element of array my $i = 1; foreach my $text ( @{$in{$key}->{Text}} ) { print FILE "%% Starting Element $i\n"; print FILE $text; $i++; } close FILE; }
The output is
  • instructions.tex
  • soln.tex
  • answer.tex
  • exer.tex
  • exertext.tex
With nested tags, the sub tags are included in the given tag (so that exer.tex includes exertext.tex, soln.tex, answer.tex). If you ran it on a full doc, presumably "document.tex" would be the same as your input doc.

- j


Comment on Re: Parsing Exercise set
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://447603]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2015-07-02 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls