Jump to content

Recommended Posts

Guest rathore
Posted

In a .ini file like this:

[section]

a=1

b=2

c=3

usually we read "a" & get "1", now is there also a way to find out the key if I know the value?

right now the way I can think of is:

-count the lines of the file & scan each line for the value

-when found get the position of "="

-extract the text to its left

now its not just a VERY long way, it also gets to be CPU hogging if the .ini has too many lines and I've to find out more than just a couple of keys.

are there some better alternatives?

Posted

Much better ways, but here is a silly way to do it.

$file="mytext.ini"
$what_I_want_to_look_for="1"
$Text = FileRead($File,FileGetSize($File))
$what="="&$what_I_want_to_look_for&@CRLF

$test2=StringSplit(StringTrimRight($Text,StringLen($text)-StringInStr($Text,$what)),@CRLF)
$test3=$test2[$test2[0]]
MsgBox(1,"",$test3)

AutoIt3, the MACGYVER Pocket Knife for computers.

Posted (edited)

One more approach:

$file="mytext.ini"
$look=1
$Text = FileRead($File,FileGetSize($File))
$test=StringSplit($Text,@CRLF)
$test3="notfound"
For $i=1 To $test[0]
$x=StringSplit($test[$i],"=")
If IsArray($x) and $x[0]=2 Then 
    If $x[2]=$look Then $test3=$x[1]
    EndIf
Next
MsgBox(1,"",$test3)
Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Guest rathore
Posted

I've checked out your first method, silly or not, its difficult to comprehend!

to save code lines and making many variables the following line is done very nicely but is confusing nonetheless!

$test2=StringSplit(StringTrimRight($Text,StringLen($text)-StringInStr($Text,$what)),@CRLF)

i'll check out the second way!

thanx a lot!!

Posted

HOLY CRAP you guys are so smart! (said like StrongBad, from HomeStarRunner.com) and if you don't know what I'm talking about you should definitely check it out B):whistle:

Posted

Some commented lines for ya for understanding method 2. and I put it in a function so that you can remove memory usage of variables after lookup is done.

MsgBox(1,"",reversefind("mytext.ini","1"))

Func reversefind($File,$look)
$test=StringSplit(FileRead($File,FileGetSize($File)),@CRLF) ; creates an array of each line
$test3="notfound"; sets up my can't find it
For $i=1 To $test[0]; how many lines
$x=StringSplit($test[$i],"=");creates an array with $x[1] as the key and $x[2] as the value
If IsArray($x) and $x[0]=2 Then; making sure to test only lines with keys and values
If $x[2]=$look Then $test3=$x[1]; if found, set $text3 to the key
EndIf
Next
Return $test3
EndFunc

AutoIt3, the MACGYVER Pocket Knife for computers.

Guest rathore
Posted

oh thanx but i'd have rather had some explanation for method 1, coz method 2 i did understand. i've even made a little modification to find out the key even if only part value is given. (using StringInStr).

and a lots of thanx... I'd still been using AU2's possibilites, AU3's advanced functions r just great!!

Posted

$file="mytext.ini"
$what_I_want_to_look_for="1"
$Text = FileRead($File,FileGetSize($File)); loads it all into $text
$what="="&$what_I_want_to_look_for&@CRLF; I use this to make sure it has =1@crlf
; nothing more, and nothing less
$test2=StringSplit(StringTrimRight($Text,StringLen($text)-StringInStr($Text,$what)),@CRLF); ok here I remove everything after $what, and then I make $test2 into an array
$test3=$test2[$test2[0]]; the last thing in the array is what I want since I removed everything out before this.
MsgBox(1,"",$test3)

Stringtrimright takes the length-the position I found the string at. say 50 key in of 75, would leave 25, so I remove the last 25.

AutoIt3, the MACGYVER Pocket Knife for computers.

Guest rathore
Posted

the explanatory notes did the job scriptkitty...

and bcording the functions wud certainly be of help to me sometime.

thanx guyz!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...