Numbering lines

hh hh at hyperhh.de
Sun Oct 28 15:51:37 EDT 2018


>> David G. wrote:
>> Thanks Geoff, I did play with Split, but one of the reasons for numbering
>> is to make any identical lines unique.  With split, for any that are not,
>> all but one is deleted.  So definitely not the result I wanted.

From that previous answer I concluded you intend to index a text file, that is
to find different lines of a text and save them together with their line number
offsets in the original.

But now (after your last post) I think you want still a simple line numbering.
But this is what my handlers do:
Even if you have 10000 identical lines then they are numbered, as they appear, from
1 up to 10000. Your answer shows that you didn't even try ...
(Following the proposal of Geoff C. here once again the methods as functions:

-- D is the separator for numbers and text lines, usually space or ": "
-- T is the input text, delimited with return
-- prepends the number and separator to each line:
function addLineNumbers D,T 
  split T by return
  put the keys of T into K
  sort K numeric
  repeat for each line L in K
    put cr & L & D & T[L] after S
  end repeat
  return char 2 to -1 of S
end addLineNumbers

-- D is the separator for numbers and text lines, usually space or ": "
-- T is the input text, delimited with return
-- removes the number and separator from each line:
function removeLineNumbers D,T
  split T by return and D
  put the keys of T into K
  sort K numeric
  repeat for each line L in K
    put cr & T[L] after S
  end repeat
  return char 2 to -1 of S
end removeLineNumbers
 
> David G. wrote:
> Thanks for this, although I’m not sure I understand.  In fact I am sure I don’t.    I know how amazingly fast the array method is, I use it elsewhere in the same stack, and it is great. 
> 
> I also don’t understand the distinction between line numbering and indexing.  If I was guessing, I would go for line numbering prepends to each line in a text field a number, and indexing is adding a numerical key to a database or array.  The former is what I described in my original post.
> 
> The reason the issue arrises is because some of the text/chat message records I referred to have the date and time stamps stripped out.  So in the following exchange…
> 
> 1757	Shadowknave: U gotta b there
> 1758	What_goes_Moo: kk
> 1759	Shadowknave: no let down?
> 1760	What_goes_Moo: kk
> 1761 Shadowknave: U b there 8?
> 1762 What_goes_Moo: I wil
> 
> … lines 2 and 4 would be identical but for the line number I added via the script. And of course, the integrity of the dialogue must be maintained.
> 
>  I had believed that collapsing duplicates and alphabetising are unavoidable with split.  If there is an array method which doesn’t mess with the text message order or content, then I would be delighted.  Is that what this is below, but I haven’t appreciated it?
> 




More information about the use-livecode mailing list