AutoHotkey Community

It is currently May 27th, 2012, 12:56 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: September 10th, 2011, 3:59 am 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
Ada, ALGOL, BASIC, C, C++, C#, DWScript, F#, Go, Java, Logo, OCaml, Pascal, Perl, PicoLisp, PureBasic, Seed7, Tcl, and more use For to step through a sequence like -6 to 6
D has foreach()
Forth omits uses do after the fact ("7 -6 do")
Fortran and REXX use Do
Python uses for with range()
Ruby uses do like this: "-6.upto(6) do |h|"
Sather has .upto
Smalltalk uses to: and do: ("-6 to: 6 do: [ :h |]")
(that's a lotta' langs!)

AutoHotkey stands out from all of these, having no provision for this type of loop. Instead, we initialize a counter and use a While loop.
I don't know how hard it would be to code, but Loop already does this with A_Index and 1 to loop's param.

I would like either a keyword Do or for For to be extended :wink:

_________________
Autofire, AutoClick, Toggle, SpamWindow Control Tools
Recommended: AutoHotkey_L


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2011, 9:29 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
This is easily solved via a function, which generates a range on the fly.
Code:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

For k,v in range(3, 5)
    MsgBox %v%

For k,v in range(1, 4)
    MsgBox %v%

s := ""
For k, v in range(-3, 6)
    s .= k "=" v "`n"
MsgBox % s

s := ""
For k, v in range(3, -6)
    s .= k "=" v "`n"
MsgBox % s

range(from, to)
{
    range := {}
    if (from < to)
        While, (from <= to)
            range[A_Index] := from++
    else
        While, (from >= to)
            range[A_Index] := from--
    return range
}

Usage is easy, just ignore the k part (which is A_LoopIndex) and use the second variable. Not exactly the same as in Python, but close.

(Should I post this function in the Scripts & Functions sub-forum?)

Edit: Updated the code and made optional parameter to required one. There was some problems... keep it simple.

Or you reverse the objects keys with values, so the value is index and the keyname is range value. In this case For k in range(1, 4) is enough, no ignoring of second value.

Code:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

For k in range(3, 5)
    MsgBox %k%

For k,v in range(1, 4)
    MsgBox %k%

s := ""
For k, v in range(-3, 6)
    s .= k "=" v "`n"
MsgBox % s

s := ""
For k, v in range(3, -6)
    s .= k "=" v "`n"
MsgBox % s

range(from, to)
{
    range := {}
    if (from < to)
        While, (from <= to)
            range[from++] := A_Index
    else
        While, (from >= to)
            range[from--] := A_Index
    return range
}

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2011, 10:02 am 
@tuncay, perhaps add a step?
Code:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

s := ""
For k, v in range(10, 0, -2)
    s .= k "=" v "`n"
MsgBox % "range(10, 0, -2)`n" s

s := "" ; same as above
For k, v in range(10, 0, 2)
    s .= k "=" v "`n"
MsgBox % "range(10, 0, 2)`n" s

s := ""
For k, v in range(3, 5)
    s .= k "=" v "`n"
MsgBox % "range(3, 5)`n" s

s := ""
For k,v in range(0, 10, 2)
    s .= k "=" v "`n"
MsgBox % "range(0, 10, 2)`n" s

s := ""
For k,v in range(4)
    s .= k "=" v "`n"
MsgBox % "range(4)`n" s

s := ""
For k, v in range(-3, 6)
    s .= k "=" v "`n"
MsgBox % "range(-3, 6)`n" s

s := ""
For k, v in range(3, -6, 2)
    s .= k "=" v "`n"
MsgBox % "range(3, -6, 2)`n" s

ExitApp
Esc::ExitApp

range(from, to="", step=1)
{
   if (to="")
   {
      to := from
      from := 1
   }
   Step:=Abs(Step)
   range := {}
   if (from < to)
      While, (from <= to)
       {
        range[A_Index] := from
        from += step
       } 
   else
      While, (from >= to)
       {
        range[A_Index] := from
        from -= step
       }
   return range
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2011, 11:24 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Good idea hugov. Here same without Abs() (instead -step is enough) and direct access to object example. Also updated to be able to get a string list, instead of an object:

Code:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

3to5 := range(3, 5)
MsgBox % 3to5[2]
MsgBox % range(3, 5, 1, "`n")

from := -9
to := 10
MsgBox % range(from, to)[19]
MsgBox % range(from, to, 1, ",")

s := ""
For k,v in range(10)
    s .= v . "`n"
MsgBox % s

s := ""
For k,v in range(-3, 2)
    s .= v . "`n"
MsgBox % s

s := ""
For k,v in range(2, 5, -2)
    s .= v . "`n"
MsgBox % s

s := ""
For k,v in range(5, -2, 2)
    s .= v . "`n"
MsgBox % s

range(from, to="", step=1, delim="")
{
    if (to = "")
        to := from, from := 1
    if (step < 0)
        step := -step
    if (delim != "")
    {
        range := ""
        if (from > to)
            While (from >= to)
                range .= from . delim, from -= step
        else
            While (from <= to)
                range .= from . delim, from += step
        StringTrimRight, range, range, % StrLen(delim)
    }
    else
    {
        range := {}
        if (from > to)
            While (from >= to)
                range[A_Index] := from, from -= step
        else
            While (from <= to)
                range[A_Index] := from, from += step
    }
    return range
}

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2011, 11:36 am 
Sweet - I'd replace
Code:
StringTrimRight, range, range, % StrLen(delim)

with
Code:
Range:=RTrim(Range, delim)
and post the func in the Scripts sections if I were you


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2011, 11:42 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Quote:
and post the func in the Scripts sections if I were you

I am working on it. I add documentation and make some test cases to be sure it works correct.

Why RTrim()? I remove the number of characters from the list, which may vary depending on the size of delim. In fact, I just remove last delimiter, which is added from the Loop.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2011, 11:50 am 
Tuncay wrote:
Why RTrim()?
I understand why, but RTRim is one less function to call as it skips the StrLen() and RTrim only removes the characters from the delimter, try it with
Code:
MsgBox % range(3, 5, 1, "-DELIMITER-")
But is doesn't matter that much, StringTrimRight just looks old in this modern looking code I think
perhaps
Code:
SubStr(range,1, (StrLen(Delim)*-1))
looks more modern ;-)
(StringTrimRight might be removed/replaced from ahk v2 if I recall correctly so that might be one reason although not a very compelling one)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2011, 1:09 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
On-topic: I don't see any particular need for another loop command.

Off-topic: RTrim("Unlimited-delimiter-", "-delimiter-") removes the trailing -, d, e, l, etc. and returns "Un". I doubt that's what you want. Also, StringTrimRight has already been removed from v2.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 11th, 2011, 1:35 am 
Offline

Joined: May 10th, 2011, 10:17 pm
Posts: 30
Location: Germany
Lexikos wrote:
On-topic: I don't see any particular need for another loop command.

a few billion years B.C... Two water living creatures meet at the coast. Says one "If we had legs, we could go to land". Says the other "I don´t see any particular need for legs". At this day the evolution of snails and worms began.

_________________
[AHK_L 1.1.07.03 Unicode x86 / Win7 Ultimate x64 SP1]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 11th, 2011, 2:05 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
To this day, snails and worms don't have legs. Obviously they were right; they didn't need legs. :roll:

The survival of the human race does not depend on this feature. We'll get along just fine with the tried and true Loop command.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 11th, 2011, 2:14 am 
Offline
User avatar

Joined: June 22nd, 2011, 4:02 pm
Posts: 171
Getting back on topic...

There is no need to add another loop command or use objects. A simple function seems to do the trick:
Code:
While InRange( index, 0, 10, 2 ) {
   ToolTip % index
   sleep 500
   }
Return

InRange( ByRef index, from, to, step = 1 )
   {
      ; Error checking
      n := from + to + step
      ErrorLevel := 1
      If n is not number
         Return False
      If ( A_Index = 0 )
         Return False

      ErrorLevel := 0      
      If ( step = 0 )
         Step := 1
      
      index := step * (A_Index - 1) + from
      if ( step < 0 ? index < to : index > to )
         Return False
      Else
         Return True
   }


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 11th, 2011, 2:22 am 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
Which needs to be posted at the bottom of any task-solution or Ask for Help reply, negating its usefulness to the point where nobody will ever use it :roll:

_________________
Autofire, AutoClick, Toggle, SpamWindow Control Tools
Recommended: AutoHotkey_L


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 11th, 2011, 2:44 am 
Offline
User avatar

Joined: June 22nd, 2011, 4:02 pm
Posts: 171
nimda wrote:
Which needs to be posted at the bottom of any task-solution or Ask for Help reply, negating its usefulness to the point where nobody will ever use it :roll:

Which post were you responding to?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 11th, 2011, 2:45 am 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
Yours

_________________
Autofire, AutoClick, Toggle, SpamWindow Control Tools
Recommended: AutoHotkey_L


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 11th, 2011, 3:07 am 
Offline
User avatar

Joined: June 22nd, 2011, 4:02 pm
Posts: 171
@nimda Are you claiming that my solution doesn't work or are you claiming that every problem has to be solved by adding a new command to the language?

The solution posted works for integer and floating point numbers. It also allows for positive or negative steps. I don't see the problem.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


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