splice two paragraphs of text into each other

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
robinson
Posts: 213
Joined: 12 Sep 2019, 20:28

splice two paragraphs of text into each other

Post by robinson » 06 May 2024, 08:02

Hi, noob here.
Say I've got a paragraph of text saved in variable a,
and another paragraph of text (in a different language, it just so happens) saved in variable b.
How can I make a variable c which consists of...
[1st line of a]
[1st line of b]
[2nd line of a]
[2nd line of b]
[3rd line of a]
[3rd line of b], etc. etc. I think you get the idea.
It's for making dual-language .srt files.
Thanks!

User avatar
kunkel321
Posts: 1126
Joined: 30 Nov 2015, 21:19

Re: splice two paragraphs of text into each other

Post by kunkel321 » 06 May 2024, 08:14

Are you saying that you have a script already that (erroneously) combines the lines of text like that, and you want to fix it? Or do you mean that you have an existing text file formatted like that, and you want to fix that existing file?

If you already have code, you should post what you have so far.
ste(phen|ve) kunkel

User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: splice two paragraphs of text into each other

Post by mikeyww » 06 May 2024, 08:14

Code: Select all

#Requires AutoHotkey v2.0
a := '
(
a
c
e
g
h
i
j
)'
b := '
(
b
d
f
)'
c := mix(a, b)
MsgBox c, 'Result', 'Iconi'

mix(a, b) {
 a2  := StrSplit(a, '`n', '`r')
 b2  := StrSplit(b, '`n', '`r')
 out := ''
 Loop Max(a2.Length, b2.Length) {
  If A_Index <= a2.Length
   out .= a2[A_Index] '`n'
  If A_Index <= b2.Length
   out .= b2[A_Index] '`n'
 }
 Return Trim(out, '`n')
}

robinson
Posts: 213
Joined: 12 Sep 2019, 20:28

Re: splice two paragraphs of text into each other

Post by robinson » 06 May 2024, 12:07

@kunkel321
I mean I have a paragraph of English text with a carriage return after roughly every 10 words or so,
and a paragraph of Chinese text which is a direct translation of the English text, cut similarly narrow,
and I want to combine the two paragraphs into one, double-length paragraph,
in which the first line is English, and the line directly below it is its translation,
and the same for the second line, third line, and every line. So...
[English line 1]
[Chinese line 1]
[English line 2]
[Chinese line 2]

--EDIT--
Actually it'd be a lot more convenient if I could just highlight both paragraphs at once, one after the other,
and have the second one be recognised as a different paragraph
simply by the fact it has a few empty carriage returns before it, rather than something on every line.
So it recognises what's the first paragraph, what's the second paragraph,
then just pastes the combined paragraph right over the top of the two highlighted paragraphs.

robinson
Posts: 213
Joined: 12 Sep 2019, 20:28

Re: splice two paragraphs of text into each other

Post by robinson » 06 May 2024, 15:36

@mikeyww
Actually yeah Mikey you seem to have got it pretty much figured out there.
Thanks again!

vmech
Posts: 361
Joined: 25 Aug 2019, 13:03

Re: splice two paragraphs of text into each other

Post by vmech » 06 May 2024, 15:58

@mikeyww
Every block of strings should be divided by linefeed symbol.
Final trimming Trim(out, '`n') unneeded.
SRT-format requirements
Please post your script code inside [code] ... [/code] block. Thank you.

User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: splice two paragraphs of text into each other

Post by mikeyww » 06 May 2024, 16:21

That is fine with me. Thank you. No specific example of the input or output was provided, so I made up my own. Blank lines are also not provided between the text pairs, but they can be added if needed.

Post Reply

Return to “Ask for Help (v2)”