Menu

Preparing numbers to be sorted by TextFX sort

2008-04-18
2012-11-14
  • Fool4UAnyway

    Fool4UAnyway - 2008-04-18

    > What if I have a hundred links needed to be short docu.1, docu.2, ...docu.100,
    > can this be added through find and replace or any other function?

    > Thx in advance. I love this feature, too.

    There are several ways to achieve this.

    1. You can prefix numbers by a single 0 if (still) needed, one at a time.
    2. You could remove all numbers and replace them by the correct prefixed numbers all at once, _if_ they are number consecutive numbers (or with the same increment).

    1. Prefix numbers by a single 0 if (still) needed, one at a time.

    This uses regular expression mode in either (CTRL+H or CTRL+R) Find/Replace dialog.
    Check the regular expression checkbox and uncheck any other undesired option (Selection, wrap). Move the cursor before the first occurrence you want to replace.

    In the Find field, enter:
    docu.(\d),

    In the Replace field, enter:
    docu.0\1,

    (Find the first and) replace all occurrences.
    This will only add one 0 to numbers consisting of 1 digit, so numbers (0)0 through 0(9).
    If will search for docu.?, where ? is any single digit.
    Each occurrence will be replaced by docu. + a leading 0 + the found number (single digit) represented by \1.

    You can then do the same for numbers 0-100 by entering an additional \d:

    For the second pass, move the cursor back to the top and enter in the Find field:
    docu.(\d\d),

    The Replace field remains docu.0\1, because any previously added prefix 0 is contained in the (\d\d) expression.
    Again, replace all occurences.

    For numbers 0-1000 you can add another additional \d:
    docu.(\d\d\d),
    ... and so on.
    I can't think of a way to do this all in one pass by using the replace function.

    2. Removing and inserting consecutive numbers.

    You can use the regex mode of the replace dialog to remove the existing numbers.

    Enter in the find field:
    docu.\d+,

    Enter in the replace field:
    docu.,

    \d+ will search for any string of consecutive digits, so 0 as well as 10 as well as 100 etc.

    The replace field simply places the prefix and postfix identifiers back, which in effect removes the numbers.

    Now you can insert numbers with the Column Editor. First you have to select the position column where you want the numbers inserted.

    Move the cursor to the first docu., line.
    Press and hold the SHIFT key, while moving the cursor down to the last line.

    Now, while still holding the SHIFT key, also press the ALT key and move the cursor left and then right again (or any other forth-and-back movement), to activate column or block selection mode. You'll see the selection turn into a one character column, then "disappear" into what is actually a 0-character-width column.

    Press ALT+C to open the Column Editor dialog.
    Select "Number to insert".
    Enter any starting number (1) and any increment (1).
    Check the "Leading zeros" checkbox.
    Press OK.

    That should do it.

    You can now select the lines and sort them by using the TextFX sort options.
    You can move the cursor on the first or last line to the first position you want TextFX to start sorting from.

    Menu_____ : TextFX
    Submenu__ : TextFX Tools
    Options__ : Sort lines case (in)sensitive (at column)

    This message was also posted in the thread below, but moved here for faster access.
    "Plugin: NPPTextFX"
    http://sourceforge.net/forum/forum.php?thread_id=1352598&forum_id=482781

     
    • Fool4UAnyway

      Fool4UAnyway - 2008-04-18

      This post includes the messages preceding my answer:

      RE: Plugin: NPPTextFX (New)
      By: Nobody/Anonymous (nobody) - 2008-04-18 01:26 
      The TextFX short seems not to work correctly. 
      I got the wrong result from shorting ascending:

      document.1
      document.10
      document.19
      document.2

      This should be:

      document.1
      document.2
      document.10
      document.19

      So how can I achieve this result?

      Thx very much indeed.

      RE: Plugin: NPPTextFX (New)
      By: Nobody/Anonymous (nobody) - 2008-04-18 10:26 
      > The TextFX short seems not to work correctly.

      No, it is you who does not seem to understand the sorting.

      All lines are sorted by their characters, compared position by position.
      All characters up to the dot (.) are all the same.

      The next position contains the characters 1 or 2.
      The character 1 (ASCII code 49) is "less than" 2 (ASCII code 50).
      Therefor, all lines have a 1 at that position will be placed before all lines with a 2 at that position.

      There are three lines with a 1, one ending there, two with an additional character, 0 or 9. They are also sorted in the correct order.

      You may even add an extra digit to one of those lines (.109 and .19 for example) to get similar results: .109 will appear before .19.

      You see numbers, but Notepad++ doesn't.
      But you can fool Notepad++ if you like, by prefixing shorter numbers with zero's:

      document.01
      document.02
      document.10
      document.19

      or

      document.001
      document.002
      document.019
      document.109

      There lines will always sort in this order you "expect".

      RE: Plugin: NPPTextFX (New)
      By: Nobody/Anonymous (nobody) - 2008-04-18 22:04 
      Interesting part: "You see numbers, but Notepad++ doesn't. 
      But you can fool Notepad++ if you like, by prefixing shorter numbers with zero's"

      What if I have a hundred links needed to be short docu.1, docu.2, ...docu.100, can this be added through find and replace or any other function?

      Thx in advance. I love this feature, too.

       
    • Fool4UAnyway

      Fool4UAnyway - 2008-04-18

      You could probably also use the Simple Script Plug-In do the same.

      "Notepad Plug-Ins download page"
      http://sourceforge.net/project/showfiles.php?group_id=189927

      The Simple Script Plug-In has a function AppendNumberList which allows you to generate numbered lists, optionally prefixed. It also has a function Merge to merge listed data into specified places in a document. The descriptions of both functions are added below.

      You could do something like this:

      1. generate a list of numbers in a separate document
      2. applying the Merge function to a document containing a line with "...docu.<%1>,..."

      >>AppendNumberList(int LowNum, int HighNum, int LeadZero): Appends a list of numbers to the end of the document.

      This is especially useful for generating number lists for use with the Merge function, and you'll most often run it on a blank document.  It adds a list of numbers, from LowNum to HighNum to the end of the document.

      If LeadZero is 0, no leading zeroes are used.  Otherwise, they are.

      If LowNum is greater than HighNum, the list is added in reverse order.

      Examples:

      AppendNumberList(1,10,0) results in:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10

      AppendNumberList(10,1,0) results in:

      10
      9
      8
      7
      6
      5
      4
      3
      2
      1

      AppendNumberList(10,1,1) results in:

      10
      09
      08
      07
      06
      05
      04
      03
      02
      01

      >>Merge(string delimiter): Merges a set of data on the clipboard into the current document.

      To use this function, you will first need to copy your dataset to the clipboard.  You might, for instance, build your data set in a separate tab.

      The data set is a delimited list of elements using the delimiter as a separator for the fields (elements), and can contain multiple lines (records).

      The document should be a template that you want to merge the data into.  Use markers of the form "<%(fieldnum)>" to mark where the elements should be inserted.

      It will repeat the operation for each row in the data set, resulting in (# of template rows)*(# of data rows) lines of text.

      This is especially useful during programming when repeating the same series of lines over and over again with only minor modifications to each line.

      The following example illustrates how to use this function.

      Suppose you have the following copied to the clipboard:

      bird;tree;green
      dog;doghouse;red
      fish;pond;blue

      If you run merge(";") on the following document:

      A <%1> lives in a <%2>.
      The <%2> is <%3>.

      You will end up with the following merged text:

      A bird lives in a tree.
      The tree is green.
      A dog lives in a doghouse.
      The doghouse is red.
      A fish lives in a pond.
      The pond is blue.

      >>Merge(string delimiter): Merges a set of data on the clipboard into the current document.

      To use this function, you will first need to copy your dataset to the clipboard.  You might, for instance, build your data set in a separate tab.

      The data set is a delimited list of elements using the delimiter as a separator for the fields (elements), and can contain multiple lines (records).

      The document should be a template that you want to merge the data into.  Use markers of the form "<%(fieldnum)>" to mark where the elements should be inserted.

      It will repeat the operation for each row in the data set, resulting in (# of template rows)*(# of data rows) lines of text.

      This is especially useful during programming when repeating the same series of lines over and over again with only minor modifications to each line.

      The following example illustrates how to use this function.

      Suppose you have the following copied to the clipboard:

      bird;tree;green
      dog;doghouse;red
      fish;pond;blue

      If you run merge(";") on the following document:

      A <%1> lives in a <%2>.
      The <%2> is <%3>.

      You will end up with the following merged text:

      A bird lives in a tree.
      The tree is green.
      A dog lives in a doghouse.
      The doghouse is red.
      A fish lives in a pond.
      The pond is blue.

       
      • Fool4UAnyway

        Fool4UAnyway - 2008-04-18

        Perhaps fidvo could add a function to the Simple Script Plug-In prefixing numbers with a number of zeroes to get a certain length.

        ZeroPrefixNumbers( MinLength, MaxLength, DesiredLength, Prefix, Postfix )

        where

        MinLength______ = minimum length of numbers to work on
        MaxLength______ = maximum length of numbers to work on (could be optional or 0)
        DesiredLength__ = the destination length (which would already infer a MaxLength)
        Prefix_________ = work upon numbers with this prefix only (optional)
        Postfix________ = work upon numbers with this postfix only (optional)

        ZeroPrefixNumbers( 0, 0, 3, "docu.", "," ) could work for you, then.

        ZeroPrefixNumbers( 2, 3, 6, "docu.", "," ):

        This would only prefix the numbers 10 through 999 to become 000010 to 000999.

        A question could be how to detect the length of a number: by characters or by value?
        If there would already be values in the range (0)00 through (0)09, would they have to be changed as well? The easiest way to go would be to say yes, just count characters (number of consecutive digits).

        And advanced feature would be to exclude the decimal part of numbers (after the , or . as decimal separator) from the ZeroPrefixing.

        Perhaps also an UnzeroPrefixNumbers function could be created.

         
    • Fool4UAnyway

      Fool4UAnyway - 2008-04-18

      Here is yet another way to simply save and use your original numbers, whether they are consecutive or not.

      1. Prefix numbers with an excessive amount of zeroes by using the Column Editor.
      2. Remove unwanted zeroes usin the Regex mode of the replace dialog.

      1. Prefix numbers with an excessive amount of zeroes by using the Column Editor.

      Use the block selection mode to select a 0-character width column after the . of docu.
      Move the cursor after the . on the first or last docu. line, hold down the SHIFT key and move down or up to the line before the last or first. Now also hold the ALT key and move to the last or first line.

      Press ALT+C to open the Column Editor dialog.
      Select "Number to insert".
      Enter any number with an excessive amount of zeroes: 10000.
      Enter as increment the number 0.
      The "Leading zeros" checkbox now doesn't matter.
      Press OK.

      All numbers will now be prefixed by the desired number of zeroes, but also contain an unwanted prefix of 1 followed by a varying numbers of zeroes. We will remove this prefix in the next step.

      2. Remove unwanted zeroes using the Regex mode of the replace dialog.

      This uses regular expression mode in either (CTRL+H or CTRL+R) Find/Replace dialog.
      Check the regular expression checkbox and uncheck any other undesired option (Selection, wrap). Move the cursor before the first docu.100.... occurrence you want to replace.

      In the Find field, enter:
      docu.10*(\d\d\d\d),

      Use as many \d tokens as the desired or needed number of digits.

      In the Replace field, enter: 
      docu.\1,

      (Find the first and) replace all occurrences. 

      This will search and find all occurrences, but telling the replace engine to keep only the last "\d" number of digits. The prefix of 1 and any (remaining) number of zeroes won't be placed back, so in effect will be removed.

      0* means: any consecutive string of 0's with a minimum of zero (none)

       
    • Nobody/Anonymous

      Very informative indeed!!!
      Thx

       
    • Fool4UAnyway

      Fool4UAnyway - 2008-04-18

      Mmm, that's kind of funny... Didn't think of that before...

      > 2. Removing and inserting consecutive numbers.

      Of course, this method requires the numbers to be already sorted, which is just what you wanted to get done...

      But you know now you can prefix the numbers to prevent the sort function to mess with the "logical" order.