Page 1 of 1

Replacing random strings of numbers

Posted: Sun Jun 23, 2019 1:54 am
by dontcareiii
Hello

I hope you gyus will help me :oops:

I need to replace a number of entries in a document. Their are identical in structure.
Sample: stat_cost 1, 150, 350, 250, 250, 1150, 4, 340

I'll be always stat_cost same spacing then number, number, number, 250, 250, 1150, 4, 340

I figured out how to find all to entries: regular expressions and "stat_cost 1, ..., ..."

Now I need to know if I can make a formule to replace all like this

stat_cost 1, same number (different in every entry)multiplied by three, same number multiplied by three

so if first entry is
stat_cost 1, 100, 300, 350, 250, 250, 1150, 4, 340
i want it to be
stat_cost 1, 300, 900, 350, 250, 250, 1150, 4, 340

and if the second is
stat_cost 1, 400, 400, 350, 250, 250, 1150, 4, 340
i want it to be
stat_cost 1, 1200, 1200, 350, 250, 250, 1150, 4, 340

On first place there is always 1
I need to modify only second and third number, the rest dont matter

I guess it is simple but I am dumb. Hope you are smarter :oops:
Help will be appreciated wit virtual hug

Re: Replacing random strings of numbers

Posted: Sun Jun 23, 2019 2:38 am
by RusselB
Welcome to the Forums.
I see that you posted in the Writer portion of the forum, but what you appear to be wanting to do looks, to me, more like a Calc application.
Can you please confirm if you are wanting this for Writer or Calc?

Re: Replacing random strings of numbers

Posted: Sun Jun 23, 2019 8:18 am
by Lupp
(My answer assumes, it's Writer.)

Unfortunately I can only help you with a user routine. IIn Basic it reads:

Code: Select all

Sub doIt()
REM Much to specialised, of course!
doc = ThisComponent
sd1 = doc.createSearchDescriptor()
sd1.SearchRegularExpression = True
sd1.SearchString = "(?<=idStr idNr)(, [0-9]+)+(?= )"
found = doc.findAll(sd1)
For j = 0 To found.Count - 1
  jRg = found(j)
  jSfound = jRg.String
  jSrepl  = ""
  jSplit  = Split(jSfound, ", ")
  u = Ubound(jSplit)
  For k = 1 To u
    kNew = 3*jSplit(k)
    jSrepl = jSrepl & ", " & kNew
  Next k
  jRg.String = jSrepl
Next j
End Sub
See attached demo.
You need to either permit macro execution for the document macro, or to move the macro to your local 'My Macros' standard library.
The demo was made with LibreOffice V 6.3.0.0.beta1, but should also work in (not too old?) AOO. It was tested with AOO V 4.1.5.

In Calc the situation would be similar. A solution based on standard functions would be very complicated if possible at all.

Re: Replacing random strings of numbers

Posted: Sun Jun 23, 2019 1:39 pm
by dontcareiii
Hey guys

Yes, unfortunately this is Writer, document is unreadable otherwise, as it is basic .txt

I also found that command "stat_cost 1, [digit:][digit:][digit:]" finds all those entries perfectly

I could settle on just multiplying just first digit of first [digit:] to get less variables but I fear that changes nothing as writer don't have command of "leave what you have found" or "alter it" in replace all section.

It is around 1400 entries in each of two files so doing it manually is rather tedious perspective

About those macros thank you immensely. Ido not know how it works but will try it.

Re: Replacing random strings of numbers

Posted: Sun Jun 23, 2019 2:22 pm
by dontcareiii
Lupp wrote:(My answer assumes, it's Writer.)

Unfortunately I can only help you with a user routine. IIn Basic it reads:

Code: Select all

Sub doIt()
REM Much to specialised, of course!
doc = ThisComponent
sd1 = doc.createSearchDescriptor()
sd1.SearchRegularExpression = True
sd1.SearchString = "(?<=idStr idNr)(, [0-9]+)+(?= )"
found = doc.findAll(sd1)
For j = 0 To found.Count - 1
  jRg = found(j)
  jSfound = jRg.String
  jSrepl  = ""
  jSplit  = Split(jSfound, ", ")
  u = Ubound(jSplit)
  For k = 1 To u
    kNew = 3*jSplit(k)
    jSrepl = jSrepl & ", " & kNew
  Next k
  jRg.String = jSrepl
Next j
End Sub
See attached demo.
You need to either permit macro execution for the document macro, or to move the macro to your local 'My Macros' standard library.
The demo was made with LibreOffice V 6.3.0.0.beta1, but should also work in (not too old?) AOO. It was tested with AOO V 4.1.5.

In Calc the situation would be similar. A solution based on standard functions would be very complicated if possible at all.
I tried to run this macro it does nothing. No number changed. I simply run this and didn't do anything else. How it is supposed to do to make it work? Should I do something after running it? I have AOO 4.1.6

Re: Replacing random strings of numbers

Posted: Sun Jun 23, 2019 2:29 pm
by robleyd
Have you considered importing the data into Calc and manipulating it? Import each element into a separate cell, then do things [TM].

You might end up with something like

Code: Select all

A1        |B1   |C1      |D1    |E1    |F1    |G1    |H1     |I1    |J1
stat_cost |1    |1E100   |300   |350   |250   |250   |1150   |4     |340
You could then manipulate the data as needed, and save it in a suitable format.

Re: Replacing random strings of numbers

Posted: Sun Jun 23, 2019 5:05 pm
by Lupp
dontcareiii wrote:I tried to run this macro it does nothing. No number changed. I simply run this and didn't do anything else. How it is supposed to do to make it work? Should I do something after running it? I have AOO 4.1.6
To be sure: Before I upload an example I test it. I just again took the time to download the attachment to my original answer again, and to run it under AOO 4.1.5 and under LibO 6.2.3.2 again. It worked as claimed and expected in both cases.

To be completely sure I also ran the openly posted Sub in a newly created document. Again: Worked correctly.
Lupp wrote:You need to either permit macro execution for the document macro, or to move the macro to your local 'My Macros' standard library.
The demo was made with LibreOffice V 6.3.0.0.beta1, but should also work in (not too old?) AOO. It was tested with AOO V 4.1.5.
Do you know how to follow this advice? (I manwhile also tested with LibO 3.3 which is virtually the same as OOo 3.x. Worked ok!)
Lupp wrote:... Something idStr idNr, 12, 22, 32, 1042 something ...
"(?<=idStr idNr)(, [0-9]+)+(?= )"
dontcareiii wrote:
... stat_cost 1, 150, 350, 250, 250, 1150, 4, 340 ...
Did you notice that my eaxmple pointed to generalisations using "idStr" in place of your "stat_cost" and " idNr" in place of your first " 1".
To run your own examples you would need to adapt the used RegEx to "(?<=stst_cost 1)(, [0-9]+)+(?= )" in place of "(?<=idStr idNr)(, [0-9]+)+(?= )". But, of course your next use case may require that you adapt the regular expression yourself - as you also will have to find the way of replacing your factor 3 from the first example to a probably later needed 2.73 .

Please attach a .odt file containg realistic examples of your style exactly.

Re: Replacing random strings of numbers

Posted: Sun Jun 23, 2019 5:23 pm
by dontcareiii
Lupp you're very helpful but I am too dumb for this. I didn't mean to accuse you, if you say you checked it, you did. I probably just don't know how to tell if this macro works.

Anyway I found a way, going around the problem, so I am not sure it is solved. My problem is solved but question from topic isn't. Or rather it is solved and what I wanted is impossible in Writer or Calc.

I will now write how I managed to approximate my failed multiplication.

I used basic replace all, it worked like a charm
I divided my targets in three groups
stat_cost 1, [:digit:][:digit:][:digit:][:digit:], for 4digit high numbers
stat_cost 1, [789][:digit:][:digit:], for 3digits of 900s, 800s, 700s,
stat_cost 1, [456][:digit:][:digit:], so on
stat_cost 1, [123][:digit:][:digit:],

and replaced with desire higher values
stat_cost 1, 3850, 580
stat_cost 1, 3550, 550
stat_cost 1, 1850, 350
stat_cost 1, 1350, 280

2400 in under 2 minutes

Remember to firstly deal with 4digit cause if you do 3 first you will move them up to original 4 tier
As I didn't need exact numbers but approximations to much higher values it worked well
Ingenuity from stupid people for stupid people :knock: :bravo: