Count (down|up)

By Memoli on Nov 06, 2010

Simple /countdown and /countup snippet

Example:

/countdown 5 1
5
4
3
2
1

or

/countup 1 5

1
2
3
4
5

Have Fun :)

alias countdown {
  var %i $1
  while (%i >= $2) {
    inc %down 1
    .timer.down. $+ %down 1 %down say %i
    dec %i
  }
  unset %down
}
alias countup {
  var %i $1
  while (%i <= $2) {
    inc %up 1
    .timer.up. $+ %up 1 %up say %i
    inc %i
  }
  unset %up
}

Comments

Sign in to comment.
Memoli   -  Nov 07, 2010

Well everyone has his own script style :D and mine script style is that one what i posted :)

but ty for other suggestions

 Respond  
Jethro   -  Nov 07, 2010

I've found another method to go about the count script:

alias count {
  $iif($isfile(count.txt),write -c count.txt) | var %w write count.txt
  var %t tokenize 32, %n $iif($regex($$1,/(up|down)$/iS),$$?"Enter a number:") 
  while ($0 <= $!) %t $1- $v1 | %t $iif($regml(1) = up,$2-,$sorttok($2-,32,nr))
  %w $1 | %w $* | .play $iif(#,#,-e) count.txt
}

Commands is either /count up or /count down
Then once a popup shows up, just enter a SINGLE number. No "1" is needed.

It's smaller than sunslayer's :P

 Respond  
Jethro   -  Nov 07, 2010

Gee, thanks for the input, Jaytea and sunslayer. Oh sunslayer, that was an unintentional typo...:p

 Respond  
jaytea   -  Nov 07, 2010

Download's method, in its simplicity, is arguably the best method here (as far as just performing the count up or count down is concerned) :P it can be useful to abstract and combine in the way sunslayer did, but oftentimes it's just not suitable and leads to less efficient and less readable code.

that said, here's another method (without any validation) that uses $timer().reps as a counter:

alias count .timercount $calc(($1 - $2) ^ 2 ^ .5 + 1) 1 say $!calc( $2 $iif($1 < $2, -) +$timer(count).reps)
 Respond  
sunslayer   -  Nov 07, 2010

you can combine the two aliases into one

alias count {
  .noop $regsubex($1 $$2,/(\d*)\W(\d*)/,)
  var %v1 = $regml(1),%v2 = $regml(2),%v = $iif(%v1 < %v2,+,-),%a = 0
  while ($counttrue(%v1,%v2,%v)) {
    .timercounter [ $+ [ %a ] ] 1 %a $iif(#,say,echo -a) %v1
    $iif(%v = +,inc %v1,dec %v1)
    inc %a
  }
}
alias -l counttrue { return $iif(($1 <= $2 && $3 = +) || ($1 >= $2 && $3 = -),1,0) }

depending on whether $1 < $2 it will count up/down
and btw Jethro_> Just enter a single number grater than 1 to count.

 Respond  
Jethro   -  Nov 06, 2010

Yeah my mistake. I saw it wrong.

 Respond  
Memoli   -  Nov 06, 2010

Jethro_ my alias works fine for me:

/countdown 3 1
23:38:51 3
23:38:52 2
23:38:53 1

 Respond  
Jethro   -  Nov 06, 2010

Download, your countdown alias won't work. Your use the >= and you decreased it from 1 - it won't do a thing. To fix that and the workaround is:

alias countdown {
  var %i $$1
  while (%i <= $iif($$2 isnum,$2)) {
    inc %down 1
    .timer.down. $+ %down 1 %down echo -a $calc($calc($2 - %i)+1)
    inc %i
  }
  unset %down
}

You have to make a check to ensure that the $2 entered is a number, not something else.

If I were you, I'd do it with regsubex, and I don't need to specify the $1 to have a countdown:

alias countup {
  tokenize 32 $regsubex($str(.,$iif($$1 isnum,$1)),/(.)/g,$+($chr(32),\n)) 
  scon -r .timer 1 $!calc( $* * 1) $iif(#,say,echo -a) $*
}
alias countdown {
  tokenize 32 $sorttok($regsubex($str(.,$iif($$1 isnum,$1)),/(.)/g,$+($chr(32),\n)),32,nr) 
  $iif(#,say,echo -a) $*
}

Just enter a single number grater than 1 to count.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.