insert each item before a string in text file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

insert each item before a string in text file

05 Jan 2023, 03:26

Code: Select all

arr:=["item 1","b item","item 3"]

maincontent =
(

What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the 
printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy 
---
Why do we use it?
It is a long established fact that a reader will 
be distracted by the readable content of a page when 
looking at its layout. The point of using Lorem Ipsum 
---
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. 
It has roots in a piece of classical Latin literature from 45 BC,
making it over 2000 years old. Richard McClintock, a Latin professor 
at Hampden-Sydney College in Virginia, looked up one of the more obscure 
---

)
i need to insert array contents before "---"

how to get output like this

Code: Select all

output =

(
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the 
printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy 
***
item 1
---

Why do we use it?
It is a long established fact that a reader will 
be distracted by the readable content of a page when 
looking at its layout. The point of using Lorem Ipsum 
***
b item
---


Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. 
It has roots in a piece of classical Latin literature from 45 BC,
making it over 2000 years old. Richard McClintock, a Latin professor 
at Hampden-Sydney College in Virginia, looked up one of the more obscure 
***
item 3
---



)

anybody can point me in right direction ?
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: insert each item before a string in text file

05 Jan 2023, 12:03

Congratulations for finding a solution. Good job! Do you mind sharing your solution? It's obviously great that you've found it but at the time I am writing this 64 other people read your post and I am sure some of them are eager to learn about how this is done. So in the spirit of the forum, please share.
14.3 & 1.3.7
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: insert each item before a string in text file

05 Jan 2023, 12:16

Code: Select all

#Include tf.ahk
FileCreateDir, Converted
gui,2: Add, text ,, Drop file
Gui,2: Show, w300 h300
return




2GuiDropFiles:
Loop, Files, %A_GuiEvent%\*.*, R  
{
n:=0,Answer:=[],Answer1:=""
FileDelete, 01.md
FileDelete, 02.md
FileDelete, test.md
TF_RemoveBlankLines("!" . A_LoopFileFullPath)
TF_SplitFileByText(A_LoopFileFullPath, "\*\*Answers\*\*", Prefix = "file", "md", InFile = 1)


Loop
{
    FileReadLine, line1, 02.md, %A_Index%
	if ErrorLevel
        break
	
	Answer[A_Index]:=line1
	
}

Loop
{
    FileReadLine, line, 01.md, %A_Index%
	if ErrorLevel
        break
 
if RegExMatch(line, "---") {
	Answer1:=Answer[n]
	
	n++
If (n!=1) && (Answer1) {
FileAppend,
(
***
%Answer1%

---


),test.md
	}
}
else
{
FileAppend,
(
%line%

),test.md

}
}

Sleep, 10
Tooltip,% A_Index

FileRead, Contents, test.md
FileAppend,
(
%Contents%
),Converted\%A_LoopFileName%
}
Return

some parts in the script is an overkill, and reduce performance
it was just a 1 time need for me , with the idea of getting it done approach.
however hope this helps someone

tf.ahk can be found here https://github.com/hi5/TF
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: insert each item before a string in text file

05 Jan 2023, 12:44

Thank you. I did not run your script but this is what I had (based on your original post):

Code: Select all

arr:=["item 1","b item","item 3"]

maincontent =
(
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the
printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy
---
Why do we use it?
It is a long established fact that a reader will
be distracted by the readable content of a page when
looking at its layout. The point of using Lorem Ipsum
---
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text.
It has roots in a piece of classical Latin literature from 45 BC,
making it over 2000 years old. Richard McClintock, a Latin professor
at Hampden-Sydney College in Virginia, looked up one of the more obscure
---
)
for x,y in strsplit(maincontent,"---")
	res .=  (x=1?"":"`n---") . y . arr[x]
msgbox % res
14.3 & 1.3.7
ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: insert each item before a string in text file

06 Jan 2023, 07:51

strsplit makes it easy to look at the code
thanks
came in handy for one of my other task :bravo:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, pond_pop and 321 guests