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: Beginner:
page layout

 



Jurafsky
Novice

Dec 3, 2012, 3:18 AM

Post #1 of 8 (1006 views)
page layout Can't Post

I've a text like this

Section
Subsection
Subsection
Subsubsection
Section
Subsection
subsubsection
Section
Section


How can I create a perl script to have an output like this:

1. Section
1.1 Subsection
1.2 Subsection
1.2.1 Subsubsection
2. Section
2.1 Subsection
2.1.1 subsubsection
3. Section
3. Section



rovf
Veteran

Dec 3, 2012, 3:28 AM

Post #2 of 8 (1004 views)
Re: [Jurafsky] page layout [In reply to] Can't Post

I would keep a separate counter for the occurances of the word sub. You could use an array, where the element with index $i contains the actual counter value for a section where the word sub occurs $i times.


Jurafsky
Novice

Dec 3, 2012, 3:34 AM

Post #3 of 8 (1003 views)
Re: [rovf] page layout [In reply to] Can't Post

does it work for "subsubsection" too?Crazy


rovf
Veteran

Dec 3, 2012, 5:42 AM

Post #4 of 8 (998 views)
Re: [Jurafsky] page layout [In reply to] Can't Post

Sure, because "subsubsection" has 2 occurances of "sub", and 2 is a valid index for an array.


Jurafsky
Novice

Dec 3, 2012, 5:48 AM

Post #5 of 8 (997 views)
Re: [rovf] page layout [In reply to] Can't Post

Could you please post your solution? Crazy


rovf
Veteran

Dec 3, 2012, 6:40 AM

Post #6 of 8 (997 views)
Re: [Jurafsky] page layout [In reply to] Can't Post

I suggest you post your attempt, and we can then discuss how to improve it.

Am I correct that you are not only a beginner in Perl programming, but don't have programmed in other languages either? In this case, I suggest that you first post your proposed solution in plain English, before translating it into code.


Jurafsky
Novice

Dec 4, 2012, 2:15 AM

Post #7 of 8 (993 views)
Re: [rovf] page layout [In reply to] Can't Post

I did it. I give to you my perl script (modified) for other users.


Code
 
my ($file,$line,$scount,$sscount,$ssscount);

open (F, "<",$file);
while(defined($line = <F> )){
chomp $line;
if ($line =~ /(\\section{(.+)})/){
$scount++;
print "$scount.$2\n";
}
elsif ($line =~ /(\\subsection{(.+)})/){
$sscount++;
print "$scount.$sscount.$2\n";
}
elsif ($line =~ /(\\subsubsection{(.+)})/){
$ssscount++;
print "$scount.$sscount.$ssscount.$2\n";
}
}
close F;



rovf
Veteran

Dec 4, 2012, 3:58 AM

Post #8 of 8 (987 views)
Re: [Jurafsky] page layout [In reply to] Can't Post

Aside from other problems, which we can discuss later, your script has the disadvantage, that it does not work for subsubsubsections. You should allow it for an arbitrary number of 'sub'.

You should do this by counting the number of occurances of the string 'sub' at the beginning of the string being read.

Here a general idea:

(1) Declare an array @current_section_number, which has the property that $current_section_number[$n] holds the current number of the section which has $n times the word 'sub' in its name.

(2) Open your input file

(3) For each line in the input file, update @current_section_number accordingly and print the section number for the file

You need to keep track of all section numbers simultaneously, because depending on the input, you will have to reset section numbers to 1 occasionally.

One more note: When developing your program, especially as a beginner, ALWAYS start your program with the following lines:

use strict;
use warnings FATAL => qw(all);
use diagnostics;

Once you are experienced enough (i.e. you know what you are doing), you can adjust these lines according to your needs.

 
 


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

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