Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Tips and tricks ➜ How to use Global Replace to number each line

How to use Global Replace to number each line

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Thu 29 Dec 2005 07:40 PM (UTC)
Message
Another use for the Notepad's Global Replace functionality is to do something like number each line sequentially. Here is how you could do that:


Find Pattern: ^.*$
Replacement: f
Line by Line: checked
Script:

count = 0

function f (str)
  count = count + 1
  return string.format ("%04i", count) .. ": " .. str
end -- function f


Applying this to a batch of code we can see the difference:


Before replacement:

sub ResetTmr (name, output, wildcards)
  dim Icnm ' Names.
  dim Tms ' Current times.
  dim Ada
  Ada = split(GetVariable("Ada"),",")
  Icnm = split(GetVariable("Icnm"),",")
  Tms = split(GetVariable("Tms"),",")
  dim count,test
  test = 0
  for count = 0 to ubound(Icnm)
    if Icnm(count) = name then
      Tms(count) = 0
      if ada(count) > 0 then
        test = 1
      end if
    end if
  next
  SetVariable "Tms", join(Tms,",")
  if test then
    EnableTrigger "CatchStat", 1
    Send "stats"
  end if
end sub

After replacement:

0001: sub ResetTmr (name, output, wildcards)
0002:   dim Icnm ' Names.
0003:   dim Tms ' Current times.
0004:   dim Ada
0005:   Ada = split(GetVariable("Ada"),",")
0006:   Icnm = split(GetVariable("Icnm"),",")
0007:   Tms = split(GetVariable("Tms"),",")
0008:   dim count,test
0009:   test = 0
0010:   for count = 0 to ubound(Icnm)
0011:     if Icnm(count) = name then
0012:       Tms(count) = 0
0013:       if ada(count) > 0 then
0014:         test = 1
0015:       end if
0016:     end if
0017:   next
0018:   SetVariable "Tms", join(Tms,",")
0019:   if test then
0020:     EnableTrigger "CatchStat", 1
0021:     Send "stats"
0022:   end if
0023: end sub


Of course, you can omit the leading zeroes by changing the string.format line, changing "%04i" to "%4i".

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,140 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 29 Dec 2005 07:42 PM (UTC)
Message
You can reverse the process by searching for numbers at the start of the line and omitting them, like this:


Find Pattern: ^[%d]+: (.*)$
Replacement: %1
Line by Line: checked

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


5,944 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.