Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort Table of Contents numbering from MS Word 1

Status
Not open for further replies.

jmodo

Programmer
Aug 2, 2006
43
US
I'm dynamically creating a html table of contents out of multiple html files that I have. Their filenames follow the numbering scheme used by microsoft word.
(i.e.:
1 Title
1.1 Title
1.2 Title
1.2.1 Title)

Currently I'm sorting the numbers by $a cmp $b, but when the numbers get above 10, 10 comes before 2 and X.10 comes before X.2 for example, the sorted list ends up looking like:
1
10
10.1
10.1.1
2 # this section should be able the 10 section
2.1

and also
10.1
10.10
10.11
10.2 # this section should be able the 10.10 section

Any ideas for algorithms?!?
Thanks, (I've exhausted google)
J
 
use <=> instead of cmp

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Well, travs69, [tt]<=>[/tt] won't work for [tt]10.1.1[/tt]...[wink]
The only way that I can see is using fixed length numbers, zero padded to the left.
Assuming you have an array with your lines and that your paragraph numbers won't go above 999 (easy to change), this would take you more or less to this:
Code:
for(@lines){
  my($num,undef)=split;
  $paranum{join('',map{sprintf"%03u",$_}split/\./,$num)}=$_;
}
for(sort keys%paranum){
  print$paranum{$_},"\n";
}

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Thanks! Worked great. I need to look into how the sorting algorithms work a little more later to fully understand this.

Cheers!
J
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top