| View previous topic :: View next topic |
| Author |
Message |
biatche
Joined: 23 Feb 2008 Posts: 58
|
Posted: Wed May 07, 2008 12:51 pm Post subject: [How] Text Manipulation |
|
|
Say I have a textfile
abc.txt
which reads
| Code: |
i love autohotkey
the fox is brown
i hate monkeys
|
and def.txt
which reads
| Code: |
i love autohotkey
the fox is too brown
i hate monkeys
|
now say i wanna add this text to line 2. "the fox is NOT brown" what commands should i be looking at? i went through the command list, i thought fileread loop and fileappend are related, but i can't be sure as im still fairly new at this... maybe theres a better way, what would you all recommend?
also, i'd like this same code to be applied to def.txt so that itll be altered to "the fox is NOT too brown"
there a way to do this? |
|
| Back to top |
|
 |
sinkfaze
Joined: 19 Mar 2008 Posts: 113
|
|
| Back to top |
|
 |
biatche
Joined: 23 Feb 2008 Posts: 58
|
Posted: Sun May 11, 2008 1:50 pm Post subject: |
|
|
| Code: | Sub3:
matchsection = (colors)
Loop, read, %file%
{
LineNumber = %A_Index%
Loop, parse, A_LoopReadLine,`n,`r %A_Space% %A_Tab%
{
IfInString, A_LoopField, %MatchSection% ; find match
{
MatchLine := LineNumber+1
FileReadLine, MatchLineContent, %file%, %MatchLine%
MsgBox, %LineNumber% %MatchLineContent%
return
}
}
}
return |
| Code: |
(funny)
the fox is brown
the cat is green
(colors)
the dogs are blue
that mouse is white
this tiger is orange
|
any idea what im doing wrong here? the IfInString section doesn't seem to loop.
what im trying to do is, manipulate text within each section... in this case i wish to manipulate all the lines below (colors) |
|
| Back to top |
|
 |
biatche
Joined: 23 Feb 2008 Posts: 58
|
Posted: Sun May 11, 2008 8:04 pm Post subject: |
|
|
anyone got ideas? am i on the right track?  |
|
| Back to top |
|
 |
Zed Gecko
Joined: 23 Sep 2006 Posts: 82
|
Posted: Sun May 11, 2008 8:22 pm Post subject: |
|
|
I am not shure what your final goal is,
but your last sample of a textfile looks very similar to an inifile.
for example: | Code: | [funny]
1=the fox is brown
2=the cat is green
[colors]
1=the dogs are blue
2=that mouse is white
3=this tiger is orange
|
therfore i i suggest to take a look at IniRead and IniWrite,
maybe they make things easier for you. _________________ 1) All my code can be reused in ANY way. 2) Please check the help and the forum-search, before posting questions; the answer is out there... |
|
| Back to top |
|
 |
biatche
Joined: 23 Feb 2008 Posts: 58
|
Posted: Sun May 11, 2008 9:34 pm Post subject: |
|
|
then would you know how i can get them into a variable in the case is like this
[section i want]
??random??key=the cat is black ; ??random??line
??random??key=the cat is black ; ??random??line |
|
| Back to top |
|
 |
Z Gecko Guest
|
Posted: Mon May 12, 2008 1:32 am Post subject: |
|
|
I am not shure what you exactly mean,
something like this?
| Code: | SearchTerm = the dogs are blue
currentsection = colors
loop
{
IniRead, LookUpVar, myfile.ini, %currentsection%, %A_Index%
if (LookUpVar = SearchTerm)
{
FoundKey := A_Index
MsgBox, Found SearchTerm: %SearchTerm% in Section: %currentsection% on Key: %Foundkey%
break
}
if (LookUpVar = "ERROR")
{
MsgBox, No Match for SearchTerm: %SearchTerm% in Section: %currentsection%
break
}
} |
this will find the (first) Key of a given section, that contains the requested string |
|
| Back to top |
|
 |
biatche
Joined: 23 Feb 2008 Posts: 58
|
Posted: Mon May 12, 2008 6:26 am Post subject: |
|
|
ok, put it this way, what if the searchterm is like
the ??? is ???
the problem is, you don't know what's ???.. could be a dragon for all you know
this is the difficult part for me which is why i chose filereadline method |
|
| Back to top |
|
 |
Z Gecko Guest
|
Posted: Mon May 12, 2008 6:49 am Post subject: |
|
|
oh, i see now
but this is actually not depending on the way you store the strings,
the method is always the same,
on some point you will loop through the data and do a comparison.
Simple comparisons like If and IfInString will not work well in your case.
I suggest to use regular expressions for that.
RegExMatch is kind of the big-brother of IfInString,
and RegExReplace can be used to change strings.
But unfortunately if have no further experience with regex, so someone else will have to jump in from this point .
You could maybe ask here for help: http://www.autohotkey.com/forum/topic13545.html |
|
| Back to top |
|
 |
|