AutoHotkey Community

It is currently May 27th, 2012, 1:37 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 417 posts ]  Go to page Previous  1 ... 10, 11, 12, 13, 14, 15, 16 ... 28  Next
Author Message
 Post subject:
PostPosted: April 17th, 2010, 9:16 pm 
Offline

Joined: October 18th, 2007, 9:21 pm
Posts: 343
Location: Saarland, Germany
@Laszlo: I would like to come back to your last post, please.

@All:
From the list AutoHotkey examples needing attention I looked at Date manipulation.
This is considered incorrect by someone (maybe the site manager), and the author is again unknown to me:
Code:
time = 03 07 2009 7:30pm EST
StringSplit, time, time, %A_Space%
month := time1
date := time2
year := time3
hours := time4
zone := time5
StringSplit, hours, hours, :
hour := hours1
StringLeft, minutes, hours2, 2
StringRight, ampm, hours2, 2
If (ampm = "pm")
  hour += 12
time := year . month . date . hour . minutes
time += 12, hours
FormatTime, TimeString, % time
MsgBox Eastern Time %TimeString%

I would like to replace with:
Code:
DateString := "March 7 2009 7:30pm EST"

; split the given string with RegExMatch
Needle := "^(?P<mm>\S*) (?P<d>\S*) (?P<y>\S*) (?P<t>\S*) (?P<tz>\S*)$"
RegExMatch(DateString, Needle, $)

; split the time with RegExMatch
Needle := "^(?P<h>\d+):(?P<min>\d+)(?P<xm>[amp]+)$"
RegExMatch($t, Needle, $)

; convert am/pm to 24h format
$h += ($xm = "am") ? 0 : 12

; knitting YYYYMMDDHH24MI format
_YYYY := $y
_MM   := Get_MonthNr($mm)
_DD   := SubStr("00" $d, -1) ; last 2 chars
_HH24 := SubStr("00" $h, -1) ; last 2 chars
_MI   := $min
YYYYMMDDHH24MI := _YYYY _MM _DD _HH24 _MI

; add 12 hours as requested
EnvAdd, YYYYMMDDHH24MI, 12, Hours
FormatTime, HumanReadable, %YYYYMMDDHH24MI%, d/MMM/yyyy  HH:mm

; add 5 hours to convert to different timezone (GMT)
EnvAdd, YYYYMMDDHH24MI, 5, Hours
FormatTime, HumanReadable_GMT, %YYYYMMDDHH24MI%, d/MMM/yyyy  HH:mm

; output
MsgBox, % "Given: " DateString "`n`n"
        . "12 hours later:`n"
        . "(" $tz "):`t" HumanReadable "h`n"
        . "(GMT):`t" HumanReadable_GMT "h`n"


;---------------------------------------------------------------------------
Get_MonthNr(Month) { ; convert named month to 2-digit number
;---------------------------------------------------------------------------
    If (Month = "January")
        Result := "01"
    Else If (Month = "February")
        Result := "02"
    Else If (Month = "March")
        Result := "03"
    Else If (Month = "April")
        Result := "04"
    Else If (Month = "May")
        Result := "05"
    Else If (Month = "June")
        Result := "06"
    Else If (Month = "July")
        Result := "07"
    Else If (Month = "August")
        Result := "08"
    Else If (Month = "September")
        Result := "09"
    Else If (Month = "October")
        Result := "10"
    Else If (Month = "November")
        Result := "11"
    Else If (Month = "December")
        Result := "12"
    Return, Result
}


FYI: I stop messing with new stuff, while there is discussion about old stuff.

Wolf

_________________
Wolf

Schön wär's, wenn's schön wär!


Last edited by wolf_II on April 19th, 2010, 12:25 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 17th, 2010, 10:15 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
Regarding Fork, the task doesn't mention numbering the processes; so it seems all that's needed is this.
Code:
#Persistent
#SingleInstance OFF

MsgBox,4,Fork,Run another process?
IfMsgBox, Yes
 Run,% A_ScriptFullPath

In general, if you have tested code that you know works, just replace anything that's marked as untested. In my opinion, untested code should not be on Rosetta to begin with (you can see my rant in an earlier post in this thread).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 17th, 2010, 11:01 pm 
Offline

Joined: October 18th, 2007, 9:21 pm
Posts: 343
Location: Saarland, Germany
@jaco0646:

Regarding Fork, how about this ?
Code:
MsgBox, 4, Fork, Start another process?
IfMsgBox, Yes
    Run, "%A_AhkPath%" "%A_ScriptFullPath%"
MsgBox, 0, Fork, Stop this process.

Persistent is not nice here, in my personal opinion. Also I can imagine situations, where "Run,% A_ScriptFullPath" will not work.

jaco0646 wrote:
you can see my rant in an earlier post in this thread

I must have overlooked that. Which page, please?
Edit: Never mind I will look for it myself!
Edit again: It's on page 11.

Wolf

_________________
Wolf

Schön wär's, wenn's schön wär!


Last edited by wolf_II on April 19th, 2010, 12:34 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 18th, 2010, 11:52 pm 
Offline

Joined: October 18th, 2007, 9:21 pm
Posts: 343
Location: Saarland, Germany
The idea is to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different, and to aid a person with a grounding in one approach to a problem in learning another.

Compute the Root mean square of the numbers 1..10.

@Laszlo:
In the light of "The idea (1)" and the job description (2), I still maintain my previous statement of "sufficiently generalized". I can also see your point of view, that the code is not generally usable, as in who is ever going to ask for calculation of RMS of integers a through b? Apart from teachers?

I would like to ask for your permission to publish your code w/o loop as a second possible method. To show what I have in mind, I have edited (2)
again with a (I believe much needed) explanation. I have made no attempt in taking any credit for that code.

Wolf

_________________
Wolf

Schön wär's, wenn's schön wär!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 19th, 2010, 2:07 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
wolf_II wrote:
permission to publish your code w/o loop
Granted. Everything I post to this forum is free for any purpose and use. (I did not post these function to Rosetta Code, or to Euler, because I don't have the time to thoroughly test them. If you find the scripts correct, feel free to publish them.)

Btw, if you take the problem description literally, the shortest code is
Code:
MsgBox 6.204837
or
Code:
MsgBox % Sqrt(38.5)
or
Code:
MsgBox % Sqrt((1+2*2+3*3+4*4+5*5+6*6+7*7+8*8+9*9+10*10)/10)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 19th, 2010, 9:37 pm 
Offline

Joined: October 18th, 2007, 9:21 pm
Posts: 343
Location: Saarland, Germany
[giggle] MsgBox 6.204837 [/giggle]

Today's contribution: Look-and-say sequence
The original thought is from forum member tomoe_uehara, see page 11. Some code is from forum member jaco0646, some from forum member Laszlo, also page 11.
All the talk about Look And Say (as far as I can tell) is from January 30/31 2010. At the time of this writing, there is still some non-functional code with the label This example is untested. Please check that it's correct, debug it as necessary, and remove this message.
For whom it may interest, the old code is:
Code:
; Untested, but should work.

Gui, +OwnDialogs
Gui, Add, Edit, vStart w500 Center
Gui, Add, Button, gStart w500, Look-and-say!
Gui, Show,, Look-and-say sequence - AutoHotkey
Return
Start:
Gui, Submit, NoHide
MsgBox % LaS(Start)
Return
LaS(Start, NumRept=5, SeparatingChar="|")
{
   Loop, %NumRept%
   {
      End .= SeparatingChar
      Loop, parse, Start
      {
         If (LoopField = A_LoopField)
            InARow++
         Else
         {
            End .= InARow
            End .= LoopField
            InARow := 1
         }
         LoopField := A_LoopField
      }
   }
   Return SubStr(End, 2)
}

jaco0646's code (on my PC) seems to always exit (after an initial value of 1) on the 23rd iteration with Exit code: -1073741819. I have not further investigated. Laszlo's code appears to be stable for a minimum of 42 iterations.
My contribution is to collect the contributions of others (who have not had the time), perform some tests to decide which are the most suitable bits for Rosetta, and glue everything together with a pinch of (my) personal taste. Here it goes:
Code:
AutoExecute:
    Gui, -MinimizeBox
    Gui, Add, Edit, w500 r20 vInput, 1
    Gui, Add, Button, x155 w100 Default, &Calculate
    Gui, Add, Button, xp+110 yp wp, E&xit
    Gui, Show,, Look-and-Say sequence
Return


ButtonCalculate:
    Gui, Submit, NoHide
    GuiControl,, Input, % LookAndSay(Input)
Return


GuiClose:
ButtonExit:
    ExitApp
Return


;---------------------------------------------------------------------------
LookAndSay(Input) {
;---------------------------------------------------------------------------
    ; credit for this function goes to AutoHotkey forum member Laszlo
    ; http://www.autohotkey.com/forum/topic44657-161.html
    ;-----------------------------------------------------------------------
    Loop, Parse, Input          ; look at every digit
        If (A_LoopField = d)    ; I've got another one! (of the same value)
            c += 1                  ; Let's count them ...
        Else {                  ; No, this one is different!
            r .= c d                ; remember what we've got so far
            c := 1                  ; It is the first one in a row
            d := A_LoopField        ; Which one is it?
        }
    Return, r c d
}

I must say that I feel a bit insecure about adding childish comments like I did. But they helped me a lot in figuring out, why and how that code is working in the first place. Please don't hesitate to criticize, and I will remove the inline comments on Rosetta webpage.

_________________
Wolf

Schön wär's, wenn's schön wär!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 20th, 2010, 6:44 pm 
Offline

Joined: October 18th, 2007, 9:21 pm
Posts: 343
Location: Saarland, Germany
This is the existing code for 24 game
Code:
#SingleInstance OFF
#Persistent
SetBatchLines, -1
Loop, 4
   Random, r%A_Index%, 1, 9
Gui, +OwnDialogs
Gui, Add, Text,, The numbers: %r1%, %r2%, %r3%, and %r4% - Good luck!
Gui, Add, Edit, vAns w500
Gui, Add, Button, w500, Submit
Gui, Show,, 24 Game
Return
ButtonGo:
Gui, Submit, NoHide
Wrong :=
Loop, 9
{
   If A_Index in %r1%,%r2%,%r3%,%r4%
      Continue
   Else If InStr(Ans, A_Index)
   {
      wrong := 1
      Break
   }
}
If (Eval(Ans) = 24 && !wrong)
   Goto, WIN
MsgBox,, 24 Game, Incorrect solution!`n%Ans%
Return
WIN:
MsgBox, 4, 24 Game, Correct solution!`n%Ans%`nPlay again?
IfMsgBox, Yes
   Reload
ExitApp
Eval(x=0)
{
   Return % v, VarSetCapacity(b,44,0), NumPut(102|1<<8,b,0,"s"), NumPut(&b+32,b,4), NumPut(1,b,33,"c"), cb:=RegisterCallback("stub"), NumPut(&b,NumGet(cb+28),4), DllCall("GlobalFree","uint",cb), NumPut(StrLen(x)+1,b,34,"s"), NumPut(&x,b,36), v:=stub() ; Taken almost directly (deleted x:="expression", as x is the functions parameter) from Lexikos at http://www.autohotkey.com/forum/topic32071.html (one-liner contest), may or may not be functional. If no, I'd appreciate someone correcting it.
}
stub()
{
   Return
}

It has got the following two labels attached:
Quote:
This example is untested. Please check that it's correct, debug it as necessary, and remove this message.
Quote:
Some lines in this example are too long (more than 80 characters). Please fix the code if it's possible and remove this message.

I have found the following problems with the old code:
1. The Submit button does nothing. I added gButtonGo for more testing.
2. the one-liner code (on my PC) seems to be not working with AutoHotkey v.1.0.48.03, but with the older version 1.0.47.04. I have not investigated further.
3. The ButtonGo routine does not check correctly for errors. E.g. When given (6, 8, 8, 1), a false result of 6+8+8+1+1 is considered correct.

New code:
Code:
AutoExecute:
    Title := "24 Game"
    Gui, -MinimizeBox
    Gui, Add, Text, w230 vPuzzle
    Gui, Add, Edit, wp vAnswer
    Gui, Add, Button, w70, &Generate
    Gui, Add, Button, x+10 wp Default, &Submit
    Gui, Add, Button, x+10 wp, E&xit


ButtonGenerate:
    Loop, 4
        Random, r%A_Index%, 1, 9
    Puzzle = %r1%, %r2%, %r3%, and %r4%
    GuiControl,, Puzzle, The numbers are:  %Puzzle%  - Good luck!
    GuiControl,, Answer ; empty the edit box
    ControlFocus, Edit1
    Gui, -Disabled
    Gui, Show,, %Title%
Return ; end of auto execute section


ButtonSubmit: ; check solution
    Gui, Submit, NoHide
    Gui, +Disabled
    RegExMatch(Answer, "(\d)\D+(\d)\D+(\d)\D+(\d)", $)
    ListPuzzle := r1 "," r2 "," r3 "," r4
    ListAnswer := $1 "," $2 "," $3 "," $4
    Sort, ListPuzzle, D,
    Sort, ListAnswer, D,
    If (ListPuzzle = ListAnswer) And Eval(Answer) = 24 {
        MsgBox, 4, %Title%, Correct solution! Play again?
        IfMsgBox, Yes
            Gosub, ButtonGenerate
        Else
            ExitApp
    } Else {
        MsgBox, 0, %Title%, Incorrect solution!`n%Answer%
        ControlFocus, Edit1
        Gui, -Disabled
        Gui, Show
    }
Return


GuiClose:
GuiEscape:
ButtonExit:
    ExitApp
Return


;---------------------------------------------------------------------------
Eval(Expr) { ; evaluate expression using separate AHK process
;---------------------------------------------------------------------------
    ; credit for this function goes to AutoHotkey forum member Laszlo
    ; http://www.autohotkey.com/forum/topic9578.html
    ;-----------------------------------------------------------------------
    static File := "24$Temp.ahk"

    ; delete old temporary file, and write new
    FileDelete, %File%
    FileContent := "#NoTrayIcon`r`n"
                .  "FileDelete, " File "`r`n"
                .  "FileAppend, `% " Expr ", " File "`r`n"
    FileAppend, %FileContent%, %File%

    ; run AHK to execute temp script, evaluate expression
    RunWait, %A_AhkPath% %File%

    ; get result
    FileRead, Result, %File%
    FileDelete, %File%
    Return, Result
}

_________________
Wolf

Schön wär's, wenn's schön wär!


Report this post
Top
 Profile  
Reply with quote  
PostPosted: April 21st, 2010, 12:25 am 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
I added inverted index here

Nice to see new activity on this thread.

I have been working on an iphone app, and am really missing ahk on mac.
I have always wanted to translate ahk commands and functions into other languages. May be we can post the c code for ahk commands and programming idioms on rosettacode and get people to translate them into more portable languages...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 21st, 2010, 12:39 am 
Offline

Joined: October 18th, 2007, 9:21 pm
Posts: 343
Location: Saarland, Germany
While I was taking a nap on the couch, some insight must have come over me. I woke up knowing that my code (24 game) will fail on something like(1, 3, 8, 1), when answering with 8*3*1**1. This is incorrectly accepted, but violates this rule:
Quote:
Only multiplication, division, addition, and subtraction operators/functions are allowed.

Corrected code:
Code:
AutoExecute:
    Title := "24 Game"
    Gui, -MinimizeBox
    Gui, Add, Text, w230 vPuzzle
    Gui, Add, Edit, wp vAnswer
    Gui, Add, Button, w70, &Generate
    Gui, Add, Button, x+10 wp Default, &Submit
    Gui, Add, Button, x+10 wp, E&xit


ButtonGenerate: ; new set of numbers
    Loop, 4
        Random, r%A_Index%, 1, 9
    Puzzle = %r1%, %r2%, %r3%, and %r4%
    GuiControl,, Puzzle, The numbers are:  %Puzzle%  - Good luck!
    GuiControl,, Answer ; empty the edit box
    ControlFocus, Edit1
    Gui, -Disabled
    Gui, Show,, %Title%
Return ; end of auto execute section


ButtonSubmit: ; check solution
    Gui, Submit, NoHide
    Gui, +Disabled

    ; check numbers used
    RegExMatch(Answer, "(\d)\D+(\d)\D+(\d)\D+(\d)", $)
    ListPuzzle := r1 "," r2 "," r3 "," r4
    ListAnswer := $1 "," $2 "," $3 "," $4
    Sort, ListPuzzle, D,
    Sort, ListAnswer, D,
    If Not ListPuzzle = ListAnswer {
        MsgBox, 48, Error - %Title%, Numbers used!`n%Answer%
        Goto, TryAgain
    }

    ; check operators used
    StringReplace, $, $, +,, All
    StringReplace, $, $, -,, All
    StringReplace, $, $, *,, All
    StringReplace, $, $, /,, All
    StringReplace, $, $, (,, All
    StringReplace, $, $, ),, All
    Loop, 9
        StringReplace, $, $, %A_Index%,, All
    If StrLen($) > 0
    Or InStr(Answer, "**")
    Or InStr(Answer, "//")
    Or InStr(Answer, "++")
    Or InStr(Answer, "--") {
        MsgBox, 48, Error - %Title%, Operators used!`n%Answer%
        Goto, TryAgain
    }

    ; check result
    Result := Eval(Answer)
    If Not Result = 24 {
        MsgBox, 48, Error - %Title%, Result incorrect!`n%Result%
        Goto, TryAgain
    }

    ; if we are sill here
    MsgBox, 4, %Title%, Correct solution! Play again?
    IfMsgBox, Yes
        Gosub, ButtonGenerate
    Else
        ExitApp
Return


TryAgain: ; alternative ending of routine ButtonSubmit
    ControlFocus, Edit1
    Gui, -Disabled
    Gui, Show
Return


GuiClose:
GuiEscape:
ButtonExit:
    ExitApp
Return


;---------------------------------------------------------------------------
Eval(Expr) { ; evaluate expression using separate AHK process
;---------------------------------------------------------------------------
    ; credit for this function goes to AutoHotkey forum member Laszlo
    ; http://www.autohotkey.com/forum/topic9578.html
    ;-----------------------------------------------------------------------
    static File := "24$Temp.ahk"

    ; delete old temporary file, and write new
    FileDelete, %File%
    FileContent := "#NoTrayIcon`r`n"
                .  "FileDelete, " File "`r`n"
                .  "FileAppend, `% " Expr ", " File "`r`n"
    FileAppend, %FileContent%, %File%

    ; run AHK to execute temp script, evaluate expression
    RunWait, %A_AhkPath% %File%

    ; get result
    FileRead, Result, %File%
    FileDelete, %File%
    Return, Result
}


PS.: Thanks to forum moderator (jaco0646?), who has edited my previous not-logged-in post.
PPS.: Whitespace characters are not accepted anymore in the answer, and that's OK.

Wolf

_________________
Wolf

Schön wär's, wenn's schön wär!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 26th, 2010, 9:24 am 
Offline

Joined: October 18th, 2007, 9:21 pm
Posts: 343
Location: Saarland, Germany
Nth_root, not solved previously.
Code:
p := 0.000001

MsgBox, % nthRoot( 10, 7131.5**10, p) "`n"
        . nthRoot(  5, 34.0      , p) "`n"
        . nthRoot(  2, 2         , p) "`n"
        . nthRoot(0.5, 7         , p) "`n"


;---------------------------------------------------------------------------
nthRoot(n, A, p) { ; http://en.wikipedia.org/wiki/Nth_root_algorithm
;---------------------------------------------------------------------------
    x1 := 1
    x2 := A / n
    While Abs(x1 - x2) > p {
        x1 := x2
        x2 := ((n-1)*x2+A/x2**(n-1))/n
    }
    Return, x2
}

_________________
Wolf

Schön wär's, wenn's schön wär!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 27th, 2010, 7:20 am 
Offline

Joined: October 18th, 2007, 9:21 pm
Posts: 343
Location: Saarland, Germany
Execute a Markov algorithm contains the following label:
Quote:
This example is under development. It was marked thus on 24/12/2009. Please help complete the example.
Code:
#Persistent
#SingleInstance, OFF
SetBatchLines, -1
StringCaseSense, On
Gui, +OwnDialogs
Gui, Add, CheckBox, w500 vVerbose, Verbose?
Gui, Add, Edit, w500 h250 vRules Center +WantReturn +WantTab, Rules
Gui, Add, Button, w500 gMarkov, Markov It!
Gui, Add, Edit, w500 h250 vString Center +WantReturn +WantTab, String
Gui, Show,, Markov Algorithm RosettaCode Example
Return
Markov:
Gui, Submit, NoHide
StringReplace, Rules, Rules, `r,, All
Loop, parse, Rules, `n
{
 If (!A_LoopField || RegExMatch(A_LoopField, "Um)^[ \t]*#.*$"))
  Continue
 If (RegExMatch(A_LoopField, "Um)^(.+)[ \t]*->[ \t]*(\.?)(.*)$", _) = 0)
 {
  MsgBox, 4, Markov Algorithm - ERROR, You have an error in your ruleset on line %A_Index%. Ignore line and continue?
  IfMsgBox, No
   Return
  Continue
 }
 _1 := _1
 _3 := _3
 StringReplace, String, String, %_1%, %_3%
 While !ErrorLevel
  StringReplace, String, String, %_1%, %_3%
 If Verbose
  Out .= A_Index . ": " . String . "`n"
 If (_2 = ".")
  Break
}
If Verbose
 MsgBox,, Markov Algorithm, %Out%
Else
 MsgBox,, Markov Algorithm, %String%
Return
GuiClose:
ExitApp

I am sorry to say, but IMHO there are serious flaws in the code. It will incorrectly insert space characters for all of the given rulesets. For ruleset 2 it will also incorrectly insert a dot, showing that termination is not working ... The list goes on. I found it easier and more fun to start from scratch, providing a user friendly interface and work on the interpreter for a Markov Algorithm. Here it comes:

Code:
;---------------------------------------------------------------------------
; Markov Algorithm.ahk
; by wolf_II
;---------------------------------------------------------------------------
; interpreter for a Markov Algorithm
;---------------------------------------------------------------------------



;---------------------------------------------------------------------------
AutoExecute: ; auto-execute section of the script
;---------------------------------------------------------------------------
    #SingleInstance, Force          ; only one instance allowed
    #NoEnv                          ; don't check empty variables
    StartupDir := A_WorkingDir      ; remember startup directory
    SetWorkingDir, %A_ScriptDir%    ; change directoy
    StringCaseSense, On             ; case sensitive comparisons
    ;-----------------------------------------------------------------------
    AppName := "Markov Algorithm"
    Gosub, GuiCreate
    Gui, Show,, %AppName%

Return



;---------------------------------------------------------------------------
GuiCreate: ; create the GUI
;---------------------------------------------------------------------------
    ; GUI options
    Gui, -MinimizeBox
    Gui, Add, Edit, y0 h0 ; catch the focus

    ; Ruleset
    Gui, Add, GroupBox, w445 h145 Section, Ruleset
    Gui, Add, Edit, xs+15 ys+20 w300 r8 vRuleset
    Gui, Add, Button, x+15 w100, Load Ruleset
    Gui, Add, Button, wp, Save Ruleset
    Gui, Add, Button, w30, 1
    Gui, Add, Button, x+5 wp, 2
    Gui, Add, Button, x+5 wp, 3
    Gui, Add, Button, xs+330 y+6 wp, 4
    Gui, Add, Button, x+5 wp, 5

    ; String
    Gui, Add, GroupBox, xs w445 h75 Section, String
    Gui, Add, Edit, xs+15 ys+20 w300 vString
    Gui, Add, Button, x+15 w100, Apply Ruleset
    Gui, Add, Button, xp wp Hidden, Stop
    Gui, Add, CheckBox, xs+15 yp+30 vSingleStepping, Single Stepping?

    ; Output
    Gui, Add, GroupBox, xs w445 h235 Section, Output
    Gui, Add, Edit, xs+15 ys+20 w415 r15 ReadOnly vOutput HwndhOut

Return



;---------------------------------------------------------------------------
GuiClose:
;---------------------------------------------------------------------------
    ExitApp

Return



;---------------------------------------------------------------------------
ButtonLoadRuleset: ; load ruleset from file
;---------------------------------------------------------------------------
    Gui, +OwnDialogs
    FileSelectFile, RulesetFile,,, Load Ruleset, *.markov
    If Not SubStr(RulesetFile, -6) = ".markov"
        RulesetFile .= ".markov"
    If FileExist(RulesetFile) {
        FileRead, Ruleset, %RulesetFile%
        GuiControl,, Ruleset, %Ruleset%
    } Else
        MsgBox, 16, Error - %AppName%, File not found:`n`n"%RulesetFile%"

Return



;---------------------------------------------------------------------------
ButtonSaveRuleset: ; save ruleset to file
;---------------------------------------------------------------------------
    Gui, +OwnDialogs
    Gui, Submit, NoHide
    FileSelectFile, RulesetFile, S16,, Save Ruleset, *.markov
    If Not SubStr(RulesetFile, -6) = ".markov"
        RulesetFile .= ".markov"
    FileDelete, %RulesetFile%
    FileAppend, %Ruleset%, %RulesetFile%
    Gui, Show

Return



;---------------------------------------------------------------------------
Button1: ; http://rosettacode.org/wiki/Execute_a_Markov_algorithm#Ruleset_1
;---------------------------------------------------------------------------
    GuiControl,, Output ; clear output
    GuiControl,, String, I bought a B of As from T S.
    GuiControl,, Ruleset,
    (LTrim
    # This rules file is extracted from Wikipedia:
    # http://en.wikipedia.org/wiki/Markov_Algorithm
    A -> apple
    B -> bag
    S -> shop
    T -> the
    the shop -> my brother
    a never used -> .terminating rule
    )

Return



;---------------------------------------------------------------------------
Button2: ; http://rosettacode.org/wiki/Execute_a_Markov_algorithm#Ruleset_2
;---------------------------------------------------------------------------
    GuiControl,, Output ; clear output
    GuiControl,, String, I bought a B of As from T S.
    GuiControl,, Ruleset,
    (LTrim
    # Slightly modified from the rules on Wikipedia
    A -> apple
    B -> bag
    S -> .shop
    T -> the
    the shop -> my brother
    a never used -> .terminating rule
    )

Return



;---------------------------------------------------------------------------
Button3: ; http://rosettacode.org/wiki/Execute_a_Markov_algorithm#Ruleset_3
;---------------------------------------------------------------------------
    GuiControl,, Output ; clear output
    GuiControl,, String, I bought a B of As W my Bgage from T S.
    GuiControl,, Ruleset,
    (LTrim
    # BNF Syntax testing rules
    A -> apple
    WWWW -> with
    Bgage -> ->.*
    B -> bag
    ->.* -> money
    W -> WW
    S -> .shop
    T -> the
    the shop -> my brother
    a never used -> .terminating rule
    )

Return



;---------------------------------------------------------------------------
Button4: ; http://rosettacode.org/wiki/Execute_a_Markov_algorithm#Ruleset_4
;---------------------------------------------------------------------------
    GuiControl,, Output ; clear output
    GuiControl,, String, _1111*11111_
    GuiControl,, Ruleset,
    (LTrim
    ### Unary Multiplication Engine, for testing Markov Algorithm implementations
    ### By Donal Fellows.
    # Unary addition engine
    _+1 -> _1+
    1+1 -> 11+
    # Pass for converting from the splitting of multiplication into ordinary
    # addition
    1! -> !1
    ,! -> !+
    _! -> _
    # Unary multiplication by duplicating left side, right side times
    1*1 -> x,@y
    1x -> xX
    X, -> 1,1
    X1 -> 1X
    _x -> _X
    ,x -> ,X
    y1 -> 1y
    y_ -> _
    # Next phase of applying
    1@1 -> x,@y
    1@_ -> @_
    ,@_ -> !_
    ++ -> +
    # Termination cleanup for addition
    _1 -> 1
    1+_ -> 1
    _+_ ->
    )

Return



;---------------------------------------------------------------------------
Button5: ; http://rosettacode.org/wiki/Execute_a_Markov_algorithm#Ruleset_5
;---------------------------------------------------------------------------
    GuiControl,, Output ; clear output
    GuiControl,, String, 000000A000000
    GuiControl,, Ruleset,
    (LTrim
    # Turing machine: three-state busy beaver
    #
    # state A, symbol 0 => write 1, move right, new state B
    A0 -> 1B
    # state A, symbol 1 => write 1, move left, new state C
    0A1 -> C01
    1A1 -> C11
    # state B, symbol 0 => write 1, move left, new state A
    0B0 -> A01
    1B0 -> A11
    # state B, symbol 1 => write 1, move right, new state B
    B1 -> 1B
    # state C, symbol 0 => write 1, move left, new state B
    0C0 -> B01
    1C0 -> B11
    # state C, symbol 1 => write 1, move left, halt
    0C1 -> H01
    1C1 -> H11
    )

Return



;---------------------------------------------------------------------------
ButtonApplyRuleset: ; flow control for Algorithm
;---------------------------------------------------------------------------
    ; prepare
    Gui, Submit, NoHide
    GuiControl,, Output ; clear
    Controls(False) ; disable
    Count := 0
    Subst := True
    Stop  := False

    ; keep substituting for as long as necessary
    While, Subst {
        Subst := False ; reset control variable
        IfEqual, Stop, 1, Break
        Gosub, Algorithm
    }

    ; clean up
    Output("Substitution count: " Count)
    Controls(True) ; re-enable

Return



;---------------------------------------------------------------------------
ButtonStop: ; this button is initially hidden
;---------------------------------------------------------------------------
    Stop := True

Return



;---------------------------------------------------------------------------
Algorithm: ; http://rosettacode.org/wiki/Execute_a_Markov_algorithm
;---------------------------------------------------------------------------
    ; Parse the ruleset and apply each rule to the string. Whenever a rule
    ; has changed the string goto first rule. Continue until a encountering
    ; a terminating rule, or until no further changes to the strings are
    ; made.
    ;-----------------------------------------------------------------------
    Loop, Parse, Ruleset, `n, `r ; always start from the beginning
    {
        ; check for comment
        If SubStr(A_LoopField, 1, 1) = "#"
            Continue ; get next line

        ; split a rule into $Search, $Terminator and $Replace
        LookFor := "(?P<Search>.+) -> (?P<Terminator>\.?)(?P<Replace>.+)"
        RegExMatch(A_LoopField, LookFor, $)

        ; single stepping through possible substitutions
        If SingleStepping
            MsgBox,, %AppName%, % ""
            . "Rule = """ A_LoopField """`n`n"
            . "Search`t= """ $Search """`n"
            . "Replace`t= """ $Replace """`n"
            . "Termintor`t= """ ($Terminator ? "True" : "False") """`n"

        ; try to substitute
        StringReplace, String, String, %$Search%, %$Replace%, UseErrorLevel

        ; any success?
        If ErrorLevel {     ; yes, substitution done
            Count++         ; keep count
            Subst := True   ; set control variable
            Output(String)  ; write new string to output
        }

        ; terminate?
        If $Terminator {    ; yes, terminate
            Stop := True    ; set control variable
            Break           ; back to flow control
        }

        ; we are not yet terminated ...
        If Subst            ; but we just did a substitution
            Break           ; back to flow control
    }

Return



;---------------------------------------------------------------------------
Controls(Bool) { ; [en|dis]able controls
;---------------------------------------------------------------------------
    Enable  := Bool ? "+" : "-"
    Disable := Bool ? "-" : "+"
    Loop, 2
        GuiControl, %Disable%ReadOnly, % "Edit" A_Index + 1
    Loop, 7
        GuiControl, %Disable%Disabled, % "Button" A_Index + 1
    GuiControl, %Disable%Disabled, Edit4
    GuiControl, %Disable%Hidden, Button10
    GuiControl, %Enable%Hidden, Button11
    GuiControl, %Disable%Disabled, Button12
}



;---------------------------------------------------------------------------
Output(Text) { ; append text to output
;---------------------------------------------------------------------------
    static EM_REPLACESEL = 0xC2
    global hOut
    Text .= "`r`n"
    SendMessage, EM_REPLACESEL,, &Text,, ahk_id %hOut%
}



;---------- end of file ----------------------------------------------------


I put some effort in writing well structured code, that hobby programmers like me can understand. I have not submitted my code to Rosetta yet, because I am not sure, if this community (that, I in a sense, I took the freedom to represent) would agree with it.

I am looking forward to get some feedback.

Wolf

_________________
Wolf

Schön wär's, wenn's schön wär!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 27th, 2010, 11:03 am 
Offline
User avatar

Joined: September 5th, 2009, 2:06 pm
Posts: 1718
Location: Somewhere near you
Does anyone know the link to the rank list?

_________________
Image
The quick onyx goblin jumps over the lazy dwarf


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 27th, 2010, 11:33 am 
Offline

Joined: October 18th, 2007, 9:21 pm
Posts: 343
Location: Saarland, Germany
http://rosettacode.org/wiki/Rosetta_Cod ... popularity
http://rosettacode.org/wiki/Special:Mos ... Categories

I'm not sure which one you are after.

tinku99's post is here:
http://www.autohotkey.com/forum/viewtop ... &start=140

_________________
Wolf

Schön wär's, wenn's schön wär!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 27th, 2010, 12:13 pm 
Offline
User avatar

Joined: September 5th, 2009, 2:06 pm
Posts: 1718
Location: Somewhere near you
Rosetta Code -- Rank by Popularity wrote:
Sample output on 30 October 2009:
1. 351 - Tcl
2. 314 - Python
3. 307 - Ruby
4. 265 - Common Lisp
5. 265 - OCaml
6. 263 - C
7. 260 - J
8. 258 - Ada
9. 250 - Perl
10. 242 - Java
11. 237 - E
12. 236 - Haskell
13. 227 - AutoHotkey
14. 226 - D
15. 213 - R
16. 195 - ALGOL 68
17. 194 - C++
18. 185 - Forth
19. 171 - JavaScript
20. 161 - Fortran

Wow, nice! 13th.. =)

_________________
Image
The quick onyx goblin jumps over the lazy dwarf


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 27th, 2010, 12:30 pm 
Offline

Joined: October 18th, 2007, 9:21 pm
Posts: 343
Location: Saarland, Germany
But that was more than 6 months ago. Today we are 13th, if you don't count the categories that are not programming languages. :D

Image

The old code was considered incorrect. Corrected code (with output in ListView):
Code:
Progress, b2 w120 zh0 fs9, Please wait ...
Sleep, 10
FileDelete, url.txt
URLDownloadToFile, http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=5000, url.txt
Loop, Read, url.txt
{
    If InStr(A_LoopReadLine, "<li>") {
        reg = title=\"Category:(.+?)"
        RegExMatch(A_LoopReadLine, reg, name)
        RegExMatch(A_LoopReadLine, "(\d*)\smembers", count)
        List .= count1 "|" name1 "`n"
    }
}
Sort, List, RN
Gui, -MinimizeBox
Gui, Add, ListView, w363 r30 Grid, Rank|Members|Category
Loop, Parse, List, `n
{
    StringSplit, Item, A_LoopField, |
    LV_Add("", A_Index, Item1, Item2)
}
LV_ModifyCol(3, 250)

Progress, Off
Gui, Show,, Rosetta Categories
Return

GuiClose:
ExitApp

_________________
Wolf

Schön wär's, wenn's schön wär!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 417 posts ]  Go to page Previous  1 ... 10, 11, 12, 13, 14, 15, 16 ... 28  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group