how to use macro to make my code
-
@paul-wormer
As first line you needfrom Npp import *
. If some of the country names contain UTF-8 codes you need# -*- coding: utf-8 -*-
on top of your script as well. -
@igor-lerinc The suggestion by the other guys to use scripting is probably the best approach especially if you have to rebuild the file (or others like it) at least somewhat often.
But if you’re not inclined to learn a scripting language in the short run, and especially if you already have some spreadsheet skill, this task can be achieved using something like OpenOffice Calc or Excel. The process would be along these lines:
- make a column of ascending numbers (Fill… Down…)
- optionally, make columns with your static text (
case
,country=
and the like) - import your 4 x 230 item lists as columns
- move the columns around, so you have one row per record with most or all the text that goes into a record
- optionally, use functions like CONCATENATE to do some massaging, that is, merge columns, so there’s less work to do in Npp.
- save as csv (unless your CONCAT-massaging is so complete you’re down to a single column, in which case, save as txt)
- open in Npp
- use a Find & Replace to break each row into a multiline record (by substituting in newlines for commas or any other record separator chars you used such as whitespace), including the adding of leading whitespace, empty lines, etc
And after typing the above, it occurs to me that you could probably do it all in Npp using Npp’s native column copy/paste operations, which you already are good at using. That is, do what you already did, except build it as one line per record, adding static text columns, then, variable data columns, then the next static text column, etc. You may wish to put in anchoring text columns to help manage the final formatting:
case 1 BREAK1 country=af BREAK2 coutry_word=Afghanistan BREAK3 country_flag=https://imgur.com/yRbLG3B BREAK4 country_code_number=+324 BREAK5 break;
Finally, as before, transform each line into multi-line records with the desired whitespace using Find & Replace.
You can also automate some of this with macros, since column pastes can be performed within a macro.
-
@neil-schipper said in how to use macro to make my code:
you could probably do it all in Npp
But the advantage of using a spreadsheet is that it also provides a convenient way of maintaining (adding/removing countries, modifying specific fields, and so on), and version tracking the source data that is used to create the records.
-
@paul-wormer said in how to use macro to make my code:
you could execute a script that roughly looks like this
I 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.
Of course, I found it difficult to understand what the OP does want, contributing to why I didn’t offer any ideas beyond the possibility of using a scripting plugin…
-
country="ad" country="ae" country="af" country="ag" country="ai" country="al" country="am" country="ao" country="aq" country="ar" country="as" country_code_number="+376" country_code_number="+971" country_code_number="+93" country_code_number="+1" country_code_number="+1" country_code_number="+355" country_code_number="+374" country_code_number="+244" country_code_number="+672" country_code_number="+54" 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; country_flag=R.drawable.flag_anguilla; coutry_word="Andorra" coutry_word="United Arab Emirates (UAE)" coutry_word="Afghanistan" coutry_word="Antigua and Barbuda" coutry_word="Anguilla" coutry_word="Albania" coutry_word="Armenia" coutry_word="Angola" coutry_word="Antarctica"
Here is what i managed to get using some regex (so assigning variable is done this way), but i still need to put them in syntax of switch case
case n country="ad" coutry_word="Andorra" country_flag=R.drawable.flag_andorra; country_code_number="+376" break; case n+1
So because i already assigned variable names, all i’m left to do is to put those variables into switch case syntax
how these can be done?
as it looks i’ve done those 920 times, i only need to wrap it into corresponding…
yes, and they are sorted this way accrodingly. (if it’s first for country name, it means first line in country number would mean for that first line country name, same is for abbrv, and it repeats to end) -
@igor-lerinc said in how to use macro to make my code:
i still need to put them in syntax of switch case
It would be good if you showed 2 or 3 record sample “before” and “after” text boxes so we can see the exact input - output relationship you want.
I have trouble understanding your text. Here’s an easy experiment: type up your text in your language (try using the simplest and most common words and grammar and sentence structure that you can while still getting your intentions across). Then run that through a translator and provide us with the output.
-
@neil-schipper said in how to use macro to make my code:
I have trouble understanding your text.
Yep, me too.
-
@alan-kilborn I appreciate the validation (even though sometimes I’m genuinely thick).
-
@neil-schipper
this is syntax i need. there’s nothing to directly compile, as this is supposed to just insert appropriate variables depending on the input n (user inputs number 2 and variables those defined would be loaded and processed in code further)BEFORE
country="ad" country="ae" country="af" country="ag" country="ai" country="al" country="am" country="ao" country="aq" country="ar" country="as" country_code_number="+376" country_code_number="+971" country_code_number="+93" country_code_number="+1" country_code_number="+1" country_code_number="+355" country_code_number="+374" country_code_number="+244" country_code_number="+672" country_code_number="+54" 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; country_flag=R.drawable.flag_anguilla; coutry_word="Andorra" coutry_word="United Arab Emirates (UAE)" coutry_word="Afghanistan" coutry_word="Antigua and Barbuda" coutry_word="Anguilla" coutry_word="Albania" coutry_word="Armenia" coutry_word="Angola" coutry_word="Antarctica"
AFTER
switch (n) { case 1: country="ad" country_code_number="+376" country_flag=R.drawable.flag_andorra; coutry_word="Andorra" break; case 2: country="ae" country_code_number="+971" country_flag=R.drawable.flag_uae; coutry_word="United Arab Emirates (UAE)" break; case 3: country="af" country_code_number="+93" country_flag=R.drawable.flag_afghanistan; coutry_word="Afghanistan" break; default: console.log(`Nothing here`); }
and to do that for all these 230 possible cases
(in fact it doesnt receive number directly from user, but just to explain it more vividly) -
@igor-lerinc said in how to use macro to make my code:
there’s nothing to directly compile, as this is supposed to just insert appropriate variables depending on the input n (user inputs number 2 and variables those defined would be loaded and processed in code further)
I know you’re not trying to be cruel, but I am experiencing this as pain. Ouch! Can we try the russian-english translation method?
And if you are talking about what the switch statement is being used for in your application, that is not relevant to the text manipulation problem you want help with.
Your sample before and after text is helpful.
Is it a requirement that the process handle sublists of different sizes as in your sample (10 country but 5 country_flag)?
What determines that the process result in 3 fully formed output records even though none of the sublists consist of 3 items?
-
First, further to my prior post:
- your sample AFTER text looks like C code, but some lines have a semicolon and others do not
- 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.
-
-
-
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.
-
-
@alan-kilborn
You should have used an existing file with 920 lines.editor.lineDown()
does not add a new line. -
@paul-wormer
My solution was useless anyway now that I saw his BEFORE and AFTER. I, too, misunderstood his problem. -
I don’t think you have a handle on how to reply to postings here, evidence:
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.
-
@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. -
@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).
-
@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).
-
@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. -
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…