Home: Perl Programming Help: Beginner:
page layout



Jurafsky
Novice

Dec 3, 2012, 3:18 AM


Views: 2865
page layout

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


Views: 2863
Re: [Jurafsky] page layout

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


Views: 2862
Re: [rovf] page layout

does it work for "subsubsection" too?Crazy


rovf
Veteran

Dec 3, 2012, 5:42 AM


Views: 2857
Re: [Jurafsky] page layout

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


Views: 2856
Re: [rovf] page layout

Could you please post your solution? Crazy


rovf
Veteran

Dec 3, 2012, 6:40 AM


Views: 2856
Re: [Jurafsky] page layout

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


Views: 2852
Re: [rovf] page layout

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


Views: 2846
Re: [Jurafsky] page layout

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.