Community
    • Login

    how to use macro to make my code

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    25 Posts 6 Posters 6.8k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Neil SchipperN
      Neil Schipper @Neil Schipper
      last edited by

      @neil-schipper

      First, further to my prior post:

      1. your sample AFTER text looks like C code, but some lines have a semicolon and others do not
      2. coutry_word is probably a typo.

      Now, I composed a macro that, using “Run a macro multiple times…” (and specifying 4 times), turns this:

      1
      2
      3
      4
      
      country="ad"
      country="ae"
      country="af"
      country="ag"
      
      country_code_number="+376"
      country_code_number="+971"
      country_code_number="+93"
      country_code_number="+1"
      
      country_flag=R.drawable.flag_andorra;
      country_flag=R.drawable.flag_uae;
      country_flag=R.drawable.flag_afghanistan;
      country_flag=R.drawable.flag_antigua_and_barbuda;
      
      coutry_word="Andorra"
      coutry_word="United Arab Emirates (UAE)"
      coutry_word="Afghanistan"
      coutry_word="Antigua and Barbuda"
      
      
      

      into this:

      1
      2
      3
      4
      
      country="ad"
      country="ae"
      country="af"
      country="ag"
      
      country_code_number="+376"
      country_code_number="+971"
      country_code_number="+93"
      country_code_number="+1"
      
      country_flag=R.drawable.flag_andorra;
      country_flag=R.drawable.flag_uae;
      country_flag=R.drawable.flag_afghanistan;
      country_flag=R.drawable.flag_antigua_and_barbuda;
      
      coutry_word="Andorra"
      coutry_word="United Arab Emirates (UAE)"
      coutry_word="Afghanistan"
      coutry_word="Antigua and Barbuda"
      
      1
      country="ad"
      country_code_number="+376"
      country_flag=R.drawable.flag_andorra;
      coutry_word="Andorra"
      
      2
      country="ae"
      country_code_number="+971"
      country_flag=R.drawable.flag_uae;
      coutry_word="United Arab Emirates (UAE)"
      
      3
      country="af"
      country_code_number="+93"
      country_flag=R.drawable.flag_afghanistan;
      coutry_word="Afghanistan"
      
      4
      country="ag"
      country_code_number="+1"
      country_flag=R.drawable.flag_antigua_and_barbuda;
      coutry_word="Antigua and Barbuda"
      
      
      

      Note that the macro:

      • relies on there being exactly 5 groups of exactly 4 lines with exactly one line between groups
      • appends the new text to the end, and leaves the original text untouched

      It would be trivial to enhance the macro so that output groups also have whatever leading whitespace, case, break, semicolons, etc. are desired.

      I don’t know if it would meet your needs because I still don’t fully understand your needs.

      I had trouble finding the actions that got it to work. The Ctl-Shift-down operation did not work as I had hoped (in macro playback). However, using bookmark actions Ctl-F2 and F2, along with other conventional select, copy, paste actions, I was able to get the result I wanted.

      1 Reply Last reply Reply Quote 1
      • Igor LerincI Igor Lerinc deleted this topic on
      • PeterJonesP PeterJones restored this topic on
      • PeterJonesP
        PeterJones @Igor Lerinc
        last edited by

        @igor-lerinc ,

        Please do not delete your topic after there have been replies. We might not have a way to mark a topic as “answered”, but deleting the topic is not the right way to “end” the question. Just say, “thanks, that worked” or something to that effect, and the conversation will end naturally.

        1 Reply Last reply Reply Quote 0
        • Alan KilbornA Alan Kilborn referenced this topic on
        • Paul WormerP
          Paul Wormer @Alan Kilborn
          last edited by

          @alan-kilborn
          You should have used an existing file with 920 lines. editor.lineDown() does not add a new line.

          Paul WormerP 1 Reply Last reply Reply Quote 0
          • Paul WormerP
            Paul Wormer @Paul Wormer
            last edited by

            @paul-wormer
            My solution was useless anyway now that I saw his BEFORE and AFTER. I, too, misunderstood his problem.

            Alan KilbornA 1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @Paul Wormer
              last edited by

              @paul-wormer

              I don’t think you have a handle on how to reply to postings here, evidence:

              d5f376c2-6ee0-4e13-8618-348afb0ee433-image.png

              So you are telling me that I “should have used an existing file with 920 lines”. Well, I don’t know what that means, because I haven’t contributed in that way to this thread.

              Also, you’re replying to yourself; this can be done, but try to do it only if it makes sense.

              1 Reply Last reply Reply Quote 0
              • Paul WormerP
                Paul Wormer @Alan Kilborn
                last edited by

                @alan-kilborn said in how to use macro to make my code:

                II gave it a whirl and upon execution I got one really long line generated (12420 characters), that pretty much looks like this, over and over on that one line:

                country= coutry_word=country_flag=country_code_number=

                I’m not sure how that gets close to what the OP wants.

                I am sorry that I don’t understand the mechanics of this particular forum. But my answer about editor.lineDown() was about your comment above.

                Alan KilbornA 1 Reply Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @Paul Wormer
                  last edited by

                  @paul-wormer said in how to use macro to make my code:

                  I am sorry that I don’t understand the mechanics of this particular forum. But my answer about editor.lineDown() was about your comment above.

                  Ah, apologies.
                  And you did say earlier:

                  Here I assume that you have an existing file of 920 lines

                  So I missed that.
                  Again, sorry.


                  Other thoughts:

                  editor.lineDown() isn’t really a great way to script something like this. It’s too “macro”-ish, and scripts give you the power to work with more of a data set rather than “careting” around a document as in a macro approach.

                  But…it is hard to say more, because I really don’t have a great grasp on the problem statement, as IMO the OP hasn’t made it very clear (and the longer that state persists, the less interested I am).

                  PeterJonesP 1 Reply Last reply Reply Quote 0
                  • PeterJonesP
                    PeterJones @Alan Kilborn
                    last edited by

                    @alan-kilborn said in how to use macro to make my code:

                    as IMO the OP hasn’t made it very clear

                    Since the OP deleted their question and it had to be resurrected, I am pretty sure either the OP got the answer they needed, or they left in a huff because they couldn’t figure out how to make us understand what they wanted. Either way, I doubt the OP is going to come back and clarify, and I doubt there’s much point in our spending any more time on it (unless the OP does come back and clarify).

                    Neil SchipperN 1 Reply Last reply Reply Quote 0
                    • Neil SchipperN
                      Neil Schipper @PeterJones
                      last edited by

                      @peterjones said in how to use macro to make my code:

                      OP deleted their question and it had to be resurrected

                      @igor-lerinc A rather anti-social thing to do.

                      @peterjones said in how to use macro to make my code:

                      I doubt there’s much point in our spending any more time on it

                      I realized this morning an enhancement to the spreadsheet approach I suggested earlier I’d like to briefly describe.

                      Without too much difficulty a column could be added consisting of formulas to calculate the line number in the target file to be assembled.

                      Assuming 6 lines per case (1 for “case” stmnt, 4 vars, and 1 for static “break” stmnt),

                      the first bank, the “case” numbers, would have target line numbers 1,7,13… (1+(92-1)*6)

                      the second bank, the “country” fields would have target line numbers 2,8,14… (2+(92-1)*6)

                      etc.

                      Now the lines could be sorted, either in the spreadsheet or after xfer to text editor.

                      To make it amenable to upkeep and modification, one should define things like NUM_COUNTRIES (92) and LINES_PER_CASE (6 in my example), and use those in the formulas.

                      As before, there are many variations on implementation details, such as, doing C-ification early, in spreadsheet:
                      CONCAT spaces/tabs + "case " + case num " “:”
                      CONCAT spaces/tabs + “country=” + country text + “;”
                      etc
                      or late, in Npp using fancy regex in a F&R.

                      1 Reply Last reply Reply Quote 0
                      • Igor LerincI
                        Igor Lerinc
                        last edited by

                        this was the answer i was looking for:

                        https://stackoverflow.com/a/70632134/17848947

                        i just didn’t wanted to continue building on more misunderstandings trying to explain further and waste anyone’s time…

                        1 Reply Last reply Reply Quote 0
                        • guy038G
                          guy038
                          last edited by guy038

                          Hello, @igor-lerinc, @peterjones, @alan-kilborn, @neil-schipper, @paul-wormer and All;

                          I found out a nice solution with a minimum of actions to proceed ! This further method is rather difficult to express but is finally easy to execute :-))

                          I will expose my method with a short list of 6 countries ( Andorre, United Arab Emirates, Afganistan, Albania, Armenia and Angola ) which, of course, can be extend to your 230 items and even more !

                          Basically, you have three informative zones and two pseudo-zones case n; and R.drawable.flag_ + the current country code, located on the next line

                          Let’s place each of these 5 zones in a different file, from file A.txt to file E.txt. Presently, the files B, C and E will contain 6 informative lines and files A and D are still empty !


                          • In the B.txt file :

                            • Move to the very beginning of file

                            • Create a zero-length rectangular selection onto the 6 lines

                            • Open the Column Editor ( Alt + C )

                            • Choose the Number to Insert radio button

                            • Initial number 2

                            • Increase by 5

                            • Repeat 1

                            • Tick the Leading zeros option

                            • Click on the OK button

                          => You should get the numbering : 02, 07, 12, 17, 22, 27

                          • In the C.txt file :

                            • Move to the very beginning of file

                            • Create a zero-length rectangular selection onto the 6 lines

                            • Open the Column Editor ( Alt + C )

                            • Choose the Number to Insert radio button

                            • Initial number 3

                            • Increase by 5

                            • Repeat 1

                            • Tick the Leading zeros option

                            • Click on the OK button

                          => You should get the numbering : 03, 08, 13, 18, 23, 28

                          • In the E.txt file :

                            • Move to the very beginning of file

                            • Create a zero-length rectangular selection onto the 6 lines

                            • Open the Column Editor ( Alt + C )

                            • Choose the Number to Insert radio button

                            • Initial number 5

                            • Increase by 5

                            • Repeat 1

                            • Tick the Leading zeros option

                            • Click on the OK button

                          => You should get the numbering : 05, 10, 15, 20, 25, 30

                          • In the D.txt empty file :

                            • Hit 6 times on the Enter key

                            • Insert a semicolon character on each line

                            • Move to the very beginning of file

                            • Create a zero-length rectangular selection onto these 6 lines

                            • Open the Column Editor ( Alt + C )

                            • Choose the Number to Insert radio button

                            • Initial number 4

                            • Increase by 5

                            • Repeat 1

                            • Tick the Leading zeros option

                            • Click on the OK button

                          => You should get the numbering : 04, 09, 14, 19, 24, 29, followed with a semicolon

                          • Finally, in the A.txt empty file :

                            • Hit 6 times on the Enter key

                            • Move to the very beginning of file

                            • Create a zero-length rectangular selection onto these 6 lines

                            • Open the Column Editor ( Alt + C )

                            • Choose the Number to Insert radio button

                            • Initial number 1

                            • Increase by 5

                            • Repeat 1

                            • Tick the Leading zeros option

                            • Click on the OK button

                          => You should get the numbering : 01, 06, 11, 16, 21, 26

                          • Secondly :

                            • Move the caret at end of the first line

                            • Create, again, a zero-lengh rectangular selection onto these 6 lines

                            • Open the Column Editor ( Alt + C )

                            • Choose the Number to Insert radio button

                            • Initial number 1

                            • Increase by 1, this time

                            • Repeat 1

                            • Untick the Leading zeros option, this time

                            • Click on the OK button

                          You should get the final numbering : 011, 062, 113, 164, 215, 266


                          Then, let’s merge the files, from A.txt to E.txt, in any order, in a new tab, renamed as Resul.txt, which possible blank lines in between. You obtain this text :

                          011
                          062
                          113
                          164
                          215
                          266
                          
                          
                          02"ad"
                          07"ae"
                          12"af"
                          17"al"
                          22"am"
                          27"an"
                          
                          03"+376"
                          08"+971"
                          13"+93"
                          18"+355"
                          23"+374"
                          28"+244"
                          
                          
                          04;
                          09;
                          14;
                          19;
                          24;
                          29;
                          
                          05"Andorra"
                          10"United Arab Emirates (UAE)"
                          15"Afghanistan"
                          20"Albania"
                          25"Armenia"
                          30"Angola"
                          
                          • Perform a classical sort with the Edit > Line Operations > Sort Lines Lexicographically Ascending option. After deleting the first possible blank lines, you’re left with these Result.txt contents :
                          011
                          02"ad"
                          03"+376"
                          04;
                          05"Andorra"
                          062
                          07"ae"
                          08"+971"
                          09;
                          10"United Arab Emirates (UAE)"
                          113
                          12"af"
                          13"+93"
                          14;
                          15"Afghanistan"
                          164
                          17"al"
                          18"+355"
                          19;
                          20"Albania"
                          215
                          22"am"
                          23"+374"
                          24;
                          25"Armenia"
                          266
                          27"an"
                          28"+244"
                          29;
                          30"Angola"
                          
                          • Move to the very beginning of file

                          • For the last time, do a zero-length rectangular selection onto the 30 lines

                          • Delete the first two digits, hitting twice the Delete key

                          The Resul.txt file becomes :

                          1
                          "ad"
                          "+376"
                          ;
                          "Andorra"
                          2
                          "ae"
                          "+971"
                          ;
                          "United Arab Emirates (UAE)"
                          3
                          "af"
                          "+93"
                          ;
                          "Afghanistan"
                          4
                          "al"
                          "+355"
                          ;
                          "Albania"
                          5
                          "am"
                          "+374"
                          ;
                          "Armenia"
                          6
                          "an"
                          "+244"
                          ;
                          "Angola"
                          

                          Almost finished ! Open the Replace dialog and performs this regex S/R :

                          SEARCH (?-s)^(\d+)\R(.+)\R(.+)\R(.+)\R"(.+)"(\R)

                          REPLACE \tcase \1:\6\t\tcountry=\2\6\t\tcountry_code_number=\3\6\t\tcountry_flag=R\.drawable\.flag_\5;\6\t\tcountry_word="\5"\6\t\tbreak;\6

                          Magically, the text is changed into :

                          	case 1:
                          		country="ad"
                          		country_code_number="+376"
                          		country_flag=R.drawable.flag_Andorra;
                          		country_word="Andorra"
                          		break;
                          	case 2:
                          		country="ae"
                          		country_code_number="+971"
                          		country_flag=R.drawable.flag_United Arab Emirates (UAE);
                          		country_word="United Arab Emirates (UAE)"
                          		break;
                          	case 3:
                          		country="af"
                          		country_code_number="+93"
                          		country_flag=R.drawable.flag_Afghanistan;
                          		country_word="Afghanistan"
                          		break;
                          	case 4:
                          		country="al"
                          		country_code_number="+355"
                          		country_flag=R.drawable.flag_Albania;
                          		country_word="Albania"
                          		break;
                          	case 5:
                          		country="am"
                          		country_code_number="+374"
                          		country_flag=R.drawable.flag_Armenia;
                          		country_word="Armenia"
                          		break;
                          	case 6:
                          		country="an"
                          		country_code_number="+244"
                          		country_flag=R.drawable.flag_Angola;
                          		country_word="Angola"
                          		break;
                          

                          To end, run this second regex S/R :

                          SEARCH (?s)(?!\r|\n).+

                          REPLACE switch (5) {\r\n$0\tdefault:\r\n\tconsole.log\(\x96Nothing here\x96\);\r\n\t}\r\n

                          And here is, Igor, your expected result :

                          switch 5 {
                          	case 1:
                          		country="ad"
                          		country_code_number="+376"
                          		country_flag=R.drawable.flag_Andorra;
                          		country_word="Andorra"
                          		break;
                          	case 2:
                          		country="ae"
                          		country_code_number="+971"
                          		country_flag=R.drawable.flag_United Arab Emirates (UAE);
                          		country_word="United Arab Emirates (UAE)"
                          		break;
                          	case 3:
                          		country="af"
                          		country_code_number="+93"
                          		country_flag=R.drawable.flag_Afghanistan;
                          		country_word="Afghanistan"
                          		break;
                          	case 4:
                          		country="al"
                          		country_code_number="+355"
                          		country_flag=R.drawable.flag_Albania;
                          		country_word="Albania"
                          		break;
                          	case 5:
                          		country="am"
                          		country_code_number="+374"
                          		country_flag=R.drawable.flag_Armenia;
                          		country_word="Armenia"
                          		break;
                          	case 6:
                          		country="an"
                          		country_code_number="+244"
                          		country_flag=R.drawable.flag_Angola;
                          		country_word="Angola"
                          		break;
                          	default:
                          	console.log(`Nothing here`);
                          	}
                          

                          Best Regards

                          guy038

                          P.S. :

                          • Your Two-letters country, in the B.txt file, represents the ISO 3166-1 alpha-2 standard for each country

                          • Your Country_code_number, in the C.txt file, represents the E_164 codes standard for each country

                          Refer to this main link :

                          https://en.wikipedia.org/wiki/Country_code

                          Paul WormerP 1 Reply Last reply Reply Quote 2
                          • Paul WormerP
                            Paul Wormer @guy038
                            last edited by Paul Wormer

                            @guy038
                            Congratulations with your ingenious solution. Although it seems to me that an editor - any editor - is not quite the right tool for this job. It is as if you are explaining how to build a wooden chicken coop with as only tool the heel of a shoe.

                            shoeheel.png

                            1 Reply Last reply Reply Quote 1
                            • First post
                              Last post
                            The Community of users of the Notepad++ text editor.
                            Powered by NodeBB | Contributors