Copy link to clipboard
Copied
I have a task to complete the below requirement for Index part in a book. Please help me.
I have sequence of numbers like this,
Index1, 26, 35, 36, 37, 47
Index2, 65, 78, 79, 89, 90
I need to change like this
i.e., the number which are in sequence order (chronological order) needs to be changed as ndash.
Copy link to clipboard
Copied
Uwe,
That's right, unlike Marc's function, my version requires a sorted array without duplicate numbers. It's easy to fix that, though.
Peter
Copy link to clipboard
Copied
@Peter,
a pre-sorted array, yes. But it's so close to perfectly working with double entries.
Just post-processing the resuling temp array.
function page_ranges (array, obj)
{
var temp = [];
var range = false;//ADD-ON:
//Sort input array:
array.sort(function(x,y){return x-y;});for (var i = 0; i < array.length; i++)
{
temp.push (array);
while (array[i+1] - array <= obj.tolerance)
{i++; range = true}
if (range)
temp[temp.length-1] += obj.dash + array;
range = false;
}
//ADD-ON:
for(var n=0;n<temp.length;n++){
var f = temp.toString().split(obj.dash);
if(f[0]===f[1]){
temp=f[0];
};
};
return temp;
} // page_ranges
Uwe
Copy link to clipboard
Copied
>It doesn't make sense (to me) to have sequences of 2 digits have a hyphen.
There's a difference between "89-90" and "89, 90": "89-90" indicates is a single reference whose discussion begins on p. 89 and ends on p. 90; "89, 90" indicates two separate references. You could therefore have page references like this: "86-89, 90, 91-95". So when you see page ranges like these you can be pretty sure the index was hand made.
This is one of the reasons why automatic page rangers aren't really a good idea. Most people don't mind, though, so I don't mind using them when necessary.
Peter
Copy link to clipboard
Copied
Hi all,
Here is the routine I use in IndexMatic 2 (script-in-progress). That's certainly equivalent to Bob's and/or Peter's approach, but why not sharing:
function makeSequences(/*Number[]*/numbers, /*String*/separator, /*String*/linker)
{
separator = separator || ", ";
linker = linker || "-";
if( numbers.length < 2 ) return numbers.join('');
var a = numbers.concat().sort(function(x,y){return x-y;}),
sz = a.length-1, i = 0, i0 = 0, n = a[0], r = [];
var format = function()
{
r.push(a[i0] + ((i-i0)>1 ? linker+a[i-1] : ''));
return i0=i;
};
while( i<sz && ( n+1>=(n=a[++i]) || format()));
format();
return r.join(separator);
}
// Sample code:
var myTest1 = [ 1, 2, 4, 6, 9, 10, 11, 12, 13, 14, 15, 18, 19, 30 ];
alert( makeSequences(myTest1, ", ", "\u2013") );
// => 1–2, 4, 6, 9–15, 18–19, 30
var myTest2 = [ 5, 3, 4, 4, 6, 6, 9, 12, 1, 18, 21, 22, 22, 0, 8 ];
alert( makeSequences(myTest2, " | " ) );
// => 0-1 | 3-6 | 8-9 | 12 | 18 | 21-22
@+
Marc
Copy link to clipboard
Copied
Since we're all sharing, here's the function I wrote about two and a half years ago. (If I were writing it now, it would probably look a lot nicer...)
function FixIndexRanges(){
var story = app.selection[0].parentStory;
var storyWords = story.words;
for(var i=storyWords.length-1;i>0;i--){
var curNumber = parseFloat(storyWords.contents);
var prevNumber = parseFloat(storyWords[i-1].contents);
if(curNumber!=NaN && prevNumber!=NaN && prevNumber==curNumber-1){
var insertNumber = curNumber+0;
var theIndex = storyWords.characters[0].index-1;
while(prevNumber==insertNumber-1){
i--;
insertNumber--;
try{
var prevNumber = parseFloat(storyWords[i-1].contents);
}catch(e){break}
}
var theIPIndex = storyWords.insertionPoints[0].index;
story.texts.itemByRange(storyWords.characters[0],story.characters.item(theIndex)).remove();
story.insertionPoints[theIPIndex].contents=insertNumber+'-';
}
}
}
Harbs
Copy link to clipboard
Copied
cool script Harbs
really nice one
Sajeev
Copy link to clipboard
Copied
It's not the most efficient or nicely designed, but it does the job...
Harbs
http://www.in-tools.com
Innovations in Automation
Copy link to clipboard
Copied
Isn't it generally considered stylistically better to make an exception of "teens" -- in other words to leave the digit '1' after the en dash in '12–16' etc. rather than '12–6', but to take out the '2', '3', etc. after the en dash in '22–6', '32–6', etc.?
Copy link to clipboard
Copied
Jeremy bowmangraphics wrote:
Isn't it generally considered stylistically better to make an exception of "teens" -- in other words to leave the digit '1' after the en dash in '12–16' etc. rather than '12–6', but to take out the '2', '3', etc. after the en dash in '22–6', '32–6', etc.?
What says "The Chicago Manual of Style" on it?
@+
Marc
Copy link to clipboard
Copied
I just checked, and see that the _Chicago Manual of Style_ has a
rather complicated set of rules for number elision, and hence differsfrom the _Oxford Guide to Style_ [OGS] which recommends the "mostsuccinct" possible, although I don't understand what it means when itsays "do not elide digits in the group 10 to 19, as these representsinfgle rather than compound numbers".I mean, huh? How is '14' (say) "A" digit? And what the heck is a"single" as opposed to a "compound" number?I would follow _Chicago Manual of Style_ myself, as it is more widelyconsulted, and makes more sense to this little guy's little brain.Judith Butcher goes with the OGS recommendation, and so does theBritish and Irish Society of Indexers, which I took as my guide in myown (amateurish but working) script a year or two ago. I would notassume it is a simple American versus British English difference,however, as Nancy Mulvany (the well-known American indexer) mentionsthe OGS recommendation without disdain.Copy link to clipboard
Copied
Jeremy,
The OP wanted to create page ranges (23, 23, 23, 24, 25 > 23-25). What you describe (23-25 > 23 > 5) is abbreviating an existing page range (aka as "elision" and "page dropping"). And when dropping pages, it's indeed the case that you don't drop teens. The reason is that you can't pronounce dropped teens: you can say "twenty-four to eight" meaning 24-28, but you can't say "eleven to fif" or "twelve to eight". There are indeed different styles for this. In Britain, maximin elision is usually preferred (1634-7) while in other countries it is generally avoided or banned from all doublets (1634-37).
Peter
Copy link to clipboard
Copied
Peter wrote:
The reason is that you can't pronounce dropped teens: you can say "twenty-four to eight" meaning 24-28, but you can't say "eleven to fif" or "twelve to eight".
Aha! So that's it! I was thinking about the numerals instead of the words for the numbers, saying to myself that the "1" in "15" function the same way as the "2" in "25".
BTW, I've often thought that the rule for dropping the S after the possessive case for "names of antiquity" (we are supposed to talk about Hobbes's Leviathan but Socrates' trial) is simply a matter of pronunciation too: "sokra-teases" sounds silly, and lots of ancient Greek names end with "eases". I guess the test is whether it is better to write "Jesus's disciples" or "Jesus' disciples".
Copy link to clipboard
Copied
Marc,
Your function is an elegant brainteaser which I haven't figured out (yet) and the most attractive of the lot posted here (I think -- not the only thing I don't understand that attracts me), but it does have a fault: it ignores the last number of a range at the end of the string:
20, 25, 26 > 20, 25
20, 25, 26, 27, 28 > 20, 25-27
If you replace "sz = a.length-1" with "sz = a.length" it seems to work fine.
Peter
Copy link to clipboard
Copied
Thanks a lot, Peter!
I have a bug indeed (!), and my function isn't as efficient as I need it to be, so I'm working on another approach using Array.splice().
Adding the tolerance argument in your function is a brillant improvement, my project needs this option too...
Find more inspiration, events, and resources on the new Adobe Community
Explore Now