vin1 Posted February 24, 2013 Posted February 24, 2013 Hi all I want to create a random phrase generator that loads three phrase parts (start between and end of phrase) from ini files. Whats the simplest way to do that?
PhoenixXL Posted February 24, 2013 Posted February 24, 2013 (edited) Here we goexpandcollapse popupFunc RandomPhrase_From_KeyValue( $sIniFile ) ;Read the Section Names Local $asSection = IniReadSectionNames( $sIniFile ) If @error Then Return SetError(1, @error, -1 ) ;Choose a Random Section Name $asSection = $asSection[ _Random ( 1, $asSection[0] ) ] ;Now read the section Local $asRet = IniReadSection( $sIniFile, $asSection ) If @error Then Return SetError(2, @error, -1 ) ;Return a random value Return $asRet[ _Random( 1, $asRet[0][0] ) ][1] EndFunc ;This function returns the Max Value when the Min value is equal to it, ;By native function it returns zero which causes problem Func _Random( $iMin, $iMax ) If $iMin = $iMax Then Return $iMax Return Random( $iMin, $iMax, 1 ) EndFunc ;Our Test Local $Phrase = RandomPhrase_From_KeyValue( "Test.ini" ) ;INI in ScriptDir If @error Then Exit MsgBox( 16, "Error", "Code: " & @error ) <> -1 ;Ask if you have any questions MsgBox( 64, "Phrase", $Phrase ) #cs The INI file - Test.ini [XL] 1 = hello 2 = nice 3 = yes xx = nothing qwerty = keyboard [AN] 1_Phrase = Something 2_Phrase = Xcellence [Good] First = Good Second = Better Third = Best #ce Ask for you have any queries Regards Edited February 24, 2013 by PhoenixXL My code: PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners. MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.
Solution kylomas Posted February 25, 2013 Solution Posted February 25, 2013 (edited) @PhoenixXL, I think the OP want a phrase consisting of a beginning, a middle and an end with each part containing a random selection appropriate to it's position in the phrase. There is a name for these kinds of things but it escapes me at the moment. @vin1, This might be a start expandcollapse popup#include <Constants.au3> #include <array.au3> ;---------------------------------------------------------------------------------------------------------- ; create test INI file ;---------------------------------------------------------------------------------------------------------- FileDelete(@ScriptDir & '\phrase.ini') Local $str $str &= '00=The cow' & @LF $str &= '01=I' & @LF $str &= '02=He' & @LF $str &= '03=She' & @LF $str &= '04=They' & @LF $str &= '05=All the girls' & @LF $str &= '06=Most of the fish' & @LF $str &= '07=A priest, a lawyer and a doctor' & @LF $str &= '08=Some days' & @LF $str &= '09=Sometimes' & @LF $str &= '10=Whenever I' & @LF $str &= '11=Whenever you' & @LF $str &= '12=Whenever they' & @LF IniWriteSection(@ScriptDir & '\phrase.ini', "Start", $str) $str = '' $str &= '01=were walking' & @LF $str &= '02=sat on' & @LF $str &= '03=said' & @LF $str &= '04=asked' & @LF $str &= '05=left for the day' & @LF $str &= '06=were fishing' & @LF $str &= '07=went to' & @LF $str &= '08=came from' & @LF $str &= '09=jumped on' & @LF $str &= '10=jumped off' & @LF IniWriteSection(@ScriptDir & '\phrase.ini', "Middle", $str) $str = '' $str &= '01=a boat' & @LF $str &= '02=a house' & @LF $str &= '03=OK' & @LF $str &= '04=supper' & @LF $str &= '05=school' & @LF $str &= '06=church' & @LF $str &= '07=home' & @LF $str &= '08=hell' & @LF $str &= '09=AutoIT' & @LF $str &= '10=a tack' & @LF $str &= '11=car' & @LF IniWriteSection(@ScriptDir & '\phrase.ini', "End", $str) ;---------------------------------------------------------------------------------------------------------- ; loop on a display of random phrases ;---------------------------------------------------------------------------------------------------------- Local $ret = 1 While $ret = 1 $ret = MsgBox($mb_okcancel, 'My Random Phrase', _ini('Start') & ' ' & _ini('Middle') & ' ' & _ini('End')) WEnd Func _ini($part) Local $aSect = IniReadSection(@ScriptDir & '\phrase.ini', $part) If @error = 1 Then MsgBox($mb_ok, 'INI Read Section Error', 'Press <ENTER> to exit') Exit EndIf Return $aSect[Random(1, UBound($aSect, 1) - 1, 1)][1] EndFunc ;==>_ini Note - For anything of significant size (you define significant) you might want to consider an SQLite solution (really not much more code than the above). kylomas edit: corrected EOL for INI population. Changed @CRLF to @LF as extra lines were being generated Edited February 25, 2013 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
vin1 Posted February 26, 2013 Author Posted February 26, 2013 (edited) thank you very much Edited February 26, 2013 by vin1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now