CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Outputting to file information between <tag> and </tag>

 



alalleyn
Novice

Apr 2, 2002, 12:44 AM

Post #1 of 3 (420 views)
Outputting to file information between <tag> and </tag> Can't Post

Hi,

Don't know if I am writing to the correct place but might someone be able to help me? If I have the following input file.

<section>
<title>
Main Coding Section
</title>
<para>
The system works by allowing the programmer to split a program up into a number of small scraps that can appear in any order within the file. Then one of the pocesses reorders them and outputs them to a specified file while the other process just copies the enitre file with all the document but with the markup removed.
</para>
<para>
Scraps always contain certain information file the file that it is to be written to and a name. In the following section I will be starting a new Java file.
</para>
<programlisting id="scrap1" role="Welcome.java">
-- This is sample code for Java using the basic program Hello World
-- Any other comments on might feel important like authour and date of last modification.

public class Welcome {
</programlisting>
<para>
The next section simply follows on from the first, but in the real program this does not always have to be so.
</para>
<programlisting id="scrap2" role="Welcome.java">
public static void main ( String args[] ) {
<xref endterm="scrap4" />
</programlisting>
<para>
Now the program must be closed.
</para>
<programlisting id="scrap3" role="Welcome.java">
} // closes main arguement
} // closes entire class
</prgoramlisitng>
<para>
This section of code should appear where the xref element tells it too. Its th main body of code and simply outputs a single like of text.
</para>
<programlisting id="scrap4" role="Welcome.java">
System.out.println("Welcome to my new system. Reading this file means that all works correctly");
</programlisting>
</section>

I am trying to get the sections enclosed by the programlisting tags to output to a file called Welcome.java, I would also like to if possible be able to reorder the file at the same time. For instance it would normally output the programlistings in the order that it gets to them. However if it reaches an xref it them outputs that scrap there, before continuing in order again.

I have tried to write the following Perl code for outputting in order, but even this seems not to work for me.

open (TXT,
"<litprog.xml");

while (<TXT>) {
@alllines = $_;
}

$codestarts =
0;
$codeends =
0;
@currentlist;
@biglist;

foreach $test (@allines) {
if ($test =~ /<\/progamlisting>/) {
$codestarts =
0;
$codesends =
1;
}
elsif ($codeends ==
1) {
foreach $test (@currentlist){
$currentlist = ($currentlist. $test);
}
push @biglist, $currentlist;
$currentlist =();
@currentlist = @_;
$endcode =
0;
}

if ($test =~ /<progamlisting.*>/) {
$codestarts =
1;
}
elsif ($codestarts ==
1) {
push @currentlist, $test;
}

close (TXT);


open(OUT,
">Welcome.java");
foreach $line (@currentlist){
print
"$line \n"
}

close(OUT)

I know it does not work because I have been using the DzSoft Perl editor and it tells tell that execution was aborted due to compilation errors, but I can not see where I am going wrong. Please help if it is at all possible.

Thanking you in advance for any help that is offered.


Coderifous
journeyman

Apr 2, 2002, 6:32 AM

Post #2 of 3 (416 views)
Re: [alalleyn] Outputting to file information between <tag> and </tag> [In reply to] Can't Post

Without getting too deep into it:


Code
  

# replace this chunk of code near the top:



while (<TXT>) {
@alllines = $_;
}


# with this chunk:



push @alllines, $_ while(<TXT>);



alalleyn
Novice

Apr 2, 2002, 10:13 AM

Post #3 of 3 (411 views)
Re: [Coderifous] Outputting to file information between <tag> and </tag> [In reply to] Can't Post

Thanks for the help so far, but I am back. I have tried the change that you recommedned but still get the same problem in that the code refuses to compile.

I even tried rewriting the file from scrath as I assumed that maybe I might have introduced an unseen error but this did not work either. What else could be wrong with it? I am hoping that I am missing something simple like a line end. The code now looks as follows:

#!C/usr/bin/perl

open (TXT,
"<litprog.xml");

push @alllines, $_ while(<TXT>);

$codestarts =
0;
$codeends =
0;
@currentlist;
@biglist;

foreach $test (@allines) {
if ($test =~ /<\/progamlisting>/) {
$codestarts =
0;
$codesends =
1;
}
elsif ($codeends ==
1) {
foreach $test (@currentlist){
$currentlist = ($currentlist. $test);
}
push @biglist, $currentlist;
$currentlist =();
@currentlist = @_;
$endcode =
0;
}

if ($test =~ /<progamlisting.*>/) {
$codestarts =
1;
}
elsif ($codestarts ==
1) {
push @currentlist, $test;
}

close (TXT);

open(OUT,
">Welcome.java");
foreach $line (@currentlist){
print
"$line \n";
}

close(OUT);

Hopefully you will be able to help again.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives