mIRC Home    About    Download    Register    News    Help

Print Thread
Shortening code #199020
08/05/08 12:14 AM
08/05/08 12:14 AM
Joined: Aug 2004
Posts: 7,252
Ontario, Canada
R
RusselB Offline OP
Planetary brain
RusselB  Offline OP
Planetary brain
R

Joined: Aug 2004
Posts: 7,252
Ontario, Canada
This is an example of one of the aliases I have in a script I'm working on
Code:
alias -l 169 {
  did -ra %srt 11 $did(%srt,169)
  if $4 {
    var %a = 1, %b = $get(0), %dcc_srt.nicks
    while %a <= %b {
      if $get(%a).file == $did(%srt,8).seltext {
        %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$get(%),44)
      }
      inc %a
    }
    did -r %srt 4
    didtok %srt 4 44 %dcc_srt.nicks
    did -ra %srt 12 0
    did -ra %srt 13,14 $numtok(%dcc_srt.nicks)
  }
  elseif $3 {
    var %a = 1, %b = $send(0), %dcc_srt.nicks
    while %a <= %b {
      if $send(%a).file == $did(%srt,6).seltext {
        %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$send(%),44)
      }
      inc %a
    }
    did -r %srt 4
    didtok %srt 4 44 %dcc_srt.nicks
    did -ra %srt 13 0
    did -ra %srt 12,14 $numtok(%dcc_srt.nicks)
  }
  elseif $2 {
    var %a = 1, %b = $send($did(%srt,4,$2),0), %dcc_srt.sends
    while %a <= %b {
      %dcc_srt.sends = $addtok(%dcc_srt.sends,$send($did(%srt,4,$2),%a).file,44)
      inc %a
    }
    var %a = 1, %b = $get($did(%srt,4,$2),0), %dcc_srt.gets
    while %a <= %b {
      %dcc_srt.gets = $addtok(%dcc_srt.gets,$get($did(%srt,4,$2),%a).file,44)
      inc %a
    }
    did -r %srt 6,8
    didtok %srt 6 44 %dcc_srt.sends
    didtok %srt 8 44 %dcc_srt.gets
    did -ra %srt 12 $numtok(%dcc_srt.sends)
    did -ra %srt 13 $numtok(%dcc_srt.gets)
    did -ra %srt 14 $calc($numtok(%dcc_srt.sends,44) + $numtok(%dcc_srt.gets,44))
  }
  elseif $1 {
    .scon $1
    var %a = 1, %b = $send(0), %dcc_srt.nicks, %dcc_srt.sends
    while %a <= %b {
      if $send(%a).cid == $cid {
        %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$send(%a),44)
        %dcc_srt.sends = $addtok(%dcc_srt.sends,$send(%a).file),44)
      }
      inc %a
    }
    var %a = 1, %b = $get(0), %dcc_srt.gets
    while %a <= %b {
      if $get(%a).cid == $cid {
        %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$get(%a),44)
        %dcc_srt.gets = $addtok(%dcc_srt.gets,$get(%a).file),44)
      }
      inc %a
    }
    did -r %srt 4,6,8
    didtok %srt 4 44 %dcc_srt.nicks
    didtok %srt 6 44 %dcc_srt.sends
    didtok %srt 8 44 %dcc_srt.gets
    did -ra %srt 12 $numtok(%dcc_srt.sends,44)
    did -ra %srt 13 $numtok(%dcc_srt.gets,44)
    did -ra %srt 14 $calc($numtok(%dcc_srt.sends,44) + $numtok(%dcc_srt.gets,44))
  }
  else {
    .scon -at1 did -a %srt $!network
    var %a = 1, %b = $send(0), %dcc_srt.nicks, %dcc_srt.sends
    while %a <= %b {
      %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$send(%a),44)
      %dcc_srt.sends = $addtok(%dcc_srt.sends,$send(%a).file),44)
      inc %a
    }
    var %a = 1, %b = $get(0), %dcc_srt.gets
    while %a <= %b {
      %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$get(%a),44)
      %dcc_srt.gets = $addtok(%dcc_srt.gets,$get(%a).file),44)
      inc %a
    }
    didtok %srt 4 44 %dcc_srt.nicks
    didtok %srt 6 44 %dcc_srt.sends
    didtok %srt 8 44 %dcc_srt.gets
    did -ra %srt 12 $send(0)
    did -ra %srt 13 $get(0)
    did -ra %srt 14 $calc($send(0) + $get(0))
  }
}

As you can tell, it's fairly lengthy (94 lines if I counted correctly), and I've got 9 more aliases the same length, so for the aliases I'm using nearly 950 lines of code. Can anyone see a way that this can be reduced? I've been staring at it for so long, I'm starting to see things that I've already tried and have rejected, due to the alterations not working.

Re: Shortening code [Re: RusselB] #199022
08/05/08 12:31 AM
08/05/08 12:31 AM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
qwerty  Offline
Hoopy frood
Q

Joined: Jan 2003
Posts: 2,523
There are a few errors that you'll want to fix first:

- $numtok is used without a 2nd parameter in a few places, eg $numtok(%dcc_srt.nicks)
- $get(%) and $send(%) (I believe you meant $get(%a) and $send(%a))
- $addtok(%dcc_srt.gets,$get(%a).file),44) again in a few places


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: Shortening code [Re: qwerty] #199025
08/05/08 12:49 AM
08/05/08 12:49 AM
Joined: May 2008
Posts: 329
A
AWEstun Offline
Fjord artisan
AWEstun  Offline
Fjord artisan
A

Joined: May 2008
Posts: 329
Originally Posted By: qwerty
There are a few errors that you'll want to fix first:

- $numtok is used without a 2nd parameter in a few places, eg $numtok(%dcc_srt.nicks)
- $get(%) and $send(%) (I believe you meant $get(%a) and $send(%a))
- $addtok(%dcc_srt.gets,$get(%a).file),44) again in a few places


That'd be an extra ) of the right then.


I registered; you should too.
Re: Shortening code [Re: AWEstun] #199026
08/05/08 12:52 AM
08/05/08 12:52 AM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
qwerty  Offline
Hoopy frood
Q

Joined: Jan 2003
Posts: 2,523
I'm not sure what you're talking about. I highlighted in red the character that should be removed.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Re: Shortening code [Re: qwerty] #199027
08/05/08 12:58 AM
08/05/08 12:58 AM
Joined: May 2008
Posts: 329
A
AWEstun Offline
Fjord artisan
AWEstun  Offline
Fjord artisan
A

Joined: May 2008
Posts: 329
Ah, I misunderstood.


I registered; you should too.
Re: Shortening code [Re: RusselB] #199028
08/05/08 01:01 AM
08/05/08 01:01 AM
Joined: Oct 2003
Posts: 3,918
Montreal, QC, Canada
A
argv0 Offline
Planetary brain
argv0  Offline
Planetary brain
A

Joined: Oct 2003
Posts: 3,918
Montreal, QC, Canada
There's way too much repetition there. Calling any command on the same input with a variation of only one or two parameters is a good sign that your code is bloated. The line
Code:
didtok %srt 4 44 %dcc_srt.nicks

is just one example of a line thats repeated far too often.

You should only see any unique command called once. Calling something more than once is a hint that you need to loop, or at least consider it.


- argv[0] on EFnet #mIRC
- "Life is a pointer to an integer without a cast"
Re: Shortening code [Re: argv0] #199031
08/05/08 01:49 AM
08/05/08 01:49 AM
Joined: Aug 2004
Posts: 7,252
Ontario, Canada
R
RusselB Offline OP
Planetary brain
RusselB  Offline OP
Planetary brain
R

Joined: Aug 2004
Posts: 7,252
Ontario, Canada
ok.. thanks to all, so far.. I think I got all the errors that were noted corrected, and re-worked my logic a bit to end up with this (so far)
Code:
alias -l 169 {
  did -ra %srt 11 $did(%srt,169)
  if $4 {
    var %a = 1, %b = $get(0), %dcc_srt.nicks
    while %a <= %b {
      %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$iif($get(%a).file == $did(%srt,8).seltext,$get(%a)),44)
      inc %a
    }
  }
  elseif $3 {
    var %a = 1, %b = $send(0), %dcc_srt.nicks
    while %a <= %b {
      %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$iif($send(%a).file == $did(%srt,6).seltext,$send(%a)),44)
      inc %a
    }
    did -ra %srt 13 0
    did -ra %srt 12 $numtok(%dcc_srt.nicks,44)
  }
  elseif $2 {
    var %a = 1, %b = $send($did(%srt,4,$2),0), %dcc_srt.sends
    while %a <= %b {
      %dcc_srt.sends = $addtok(%dcc_srt.sends,$send($did(%srt,4,$2),%a).file,44)
      inc %a
    }
    var %a = 1, %b = $get($did(%srt,4,$2),0), %dcc_srt.gets
    while %a <= %b {
      %dcc_srt.gets = $addtok(%dcc_srt.gets,$get($did(%srt,4,$2),%a).file,44)
      inc %a
    }
  }
  elseif $1 {
    .scon $1
    var %a = 1, %b = $send(0), %dcc_srt.nicks, %dcc_srt.sends
    while %a <= %b {
      %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$iif($send(%a).cid == $cid,$send(%a)),44)
      %dcc_srt.sends = $addtok(%dcc_srt.sends,$iif($send(%a).cid == $cid,$send(%a).file),44)
      inc %a
    }
    var %a = 1, %b = $get(0), %dcc_srt.gets
    while %a <= %b {
      %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$iif($get(%a).cid == $cid,$get(%a)),44)
      %dcc_srt.sends = $addtok(%dcc_srt.sends,$iif($get(%a).cid == $cid,$get(%a).file),44)
      inc %a
    }
  }
  else {
    .scon -at1 did -a %srt $!network
    var %a = 1, %b = $send(0), %dcc_srt.nicks, %dcc_srt.sends
    while %a <= %b {
      %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$send(%a),44)
      %dcc_srt.sends = $addtok(%dcc_srt.sends,$send(%a).file,44)
      inc %a
    }
    var %a = 1, %b = $get(0), %dcc_srt.gets
    while %a <= %b {
      %dcc_srt.nicks = $addtok(%dcc_srt.nicks,$get(%a),44)
      %dcc_srt.gets = $addtok(%dcc_srt.gets,$get(%a).file,44)
      inc %a
    }
  }
  if $3 {
    did -r %srt 4
    didtok %srt 4 44 %dcc_srt.nicks
    did -ra %srt 14 $lines(%srt,4)
    did -ra %srt $iif($4,12,13) 0
    did -ra %srt $iif($4,13,12) $numtok(%dcc_srt.nicks,44)
  }
  elseif $1 {
    did -r %srt 6,8
    didtok %srt 6 44 %dcc_srt.sends
    didtok %srt 8 44 %dcc_srt.gets
    did -ra %srt 12 $did(%srt,6).lines
    did -ra %srt 13 $did(%srt,8).lines
    did -ra %srt 14 $calc($did(%srt,12) + $did(%srt,13))
  }
}


Re: Shortening code [Re: RusselB] #199037
08/05/08 03:29 AM
08/05/08 03:29 AM
Joined: Oct 2005
Posts: 1,741
G
genius_at_work Offline
Hoopy frood
genius_at_work  Offline
Hoopy frood
G

Joined: Oct 2005
Posts: 1,741
Here is a somewhat condensed version of your code.

Code:
alias -l 169 {
  did -ra %srt 11 $did(%srt,169)
  if $4 { %dcc_srt.nicks = $169.loops(1,1,$get(0)) }
  elseif $3 {
    %dcc_srt.nicks = $169.loops(2,1,$send(0))
    did -ra %srt 13 0
    did -ra %srt 12 $numtok(%dcc_srt.nicks,44)
  }
  elseif $2 {
    %dcc_srt.sends = $169.loops(3,1,$send($did(%srt,4,$2),0))
    %dcc_srt.gets = $169.loops(4,1,$get($did(%srt,4,$2),0))
  }
  elseif $1 {
    .scon $1
    %dcc_srt.nicks = $169.loops(5a,1,$send(0))
    %dcc_srt.sends = $169.loops(5b,1,$send(0))
    %dcc_srt.nicks = $169.loops(6a,1,$get(0),%dcc_srt.nicks)
    %dcc_srt.sends = $169.loops(6b,1,$get(0),%dcc_srt.sends)
  }
  else {
    .scon -at1 did -a %srt $!network
    %dcc_srt.nicks = $169.loops(7a,1,$send(0))
    %dcc_srt.sends = $169.loops(7b,1,$send(0))
    %dcc_srt.nicks = $169.loops(8a,1,$get(0),%dcc_srt.nick)
    %dcc_srt.gets = $169.loops(8b,1,$get(0))
  }
  if $3 {
    did -r %srt 4
    didtok %srt 4 44 %dcc_srt.nicks
    did -ra %srt 14 $lines(%srt,4)
    did -ra %srt $iif($4,12,13) 0
    did -ra %srt $iif($4,13,12) $numtok(%dcc_srt.nicks,44)
  }
  elseif $1 {
    did -r %srt 6,8
    didtok %srt 6 44 %dcc_srt.sends
    didtok %srt 8 44 %dcc_srt.gets
    did -ra %srt 12 $did(%srt,6).lines
    did -ra %srt 13 $did(%srt,8).lines
    did -ra %srt 14 $calc($did(%srt,12) + $did(%srt,13))
  }
}

alias 169.loops {
  ;$1=type, $2=start, $3=end, $4-=input
  var %a = $2, %b = $3, %result = $4-
  while %a <= %b {
    if ($1 == 1) { %result = $addtok(%result,$iif($get(%a).file == $did(%srt,8).seltext,$get(%a)),44) }
    elseif ($1 == 2) { %result = $addtok(%result,$iif($send(%a).file == $did(%srt,6).seltext,$send(%a)),44) }
    elseif ($1 == 3) { %result = $addtok(%result,$send($did(%srt,4,$2),%a).file,44) }
    elseif ($1 == 4) { %result = $addtok(%result,$get($did(%srt,4,$2),%a).file,44) }
    elseif ($1 == 5a) { %result = $addtok(%result,$iif($send(%a).cid == $cid,$send(%a)),44) }
    elseif ($1 == 5b) { %result = $addtok(%result,$iif($send(%a).cid == $cid,$send(%a).file),44) }
    elseif ($1 == 6a) { %result = $addtok(%result,$iif($get(%a).cid == $cid,$get(%a)),44) }
    elseif ($1 == 6b) { %result = $addtok(%result,$iif($get(%a).cid == $cid,$get(%a).file),44) }
    elseif ($1 == 7a) { %result = $addtok(%result,$send(%a),44) }
    elseif ($1 == 7b) { %result = $addtok(%result,$send(%a).file,44) }
    elseif ($1 == 8a) { %result = $addtok(%result,$get(%a),44) }
    elseif ($1 == 8b) { %result = $addtok(%result,$get(%a).file,44) }
    inc %a
  }
}


It sacrifices a bit of speed (calls loops twice due to mIRC's lack of arrays) in order to make it shorter. The more while loops that are replaced, the shorter the code becomes when compared to the non-condensed version.

-genius_at_work

Re: Shortening code [Re: genius_at_work] #199042
08/05/08 04:01 AM
08/05/08 04:01 AM
Joined: Aug 2004
Posts: 7,252
Ontario, Canada
R
RusselB Offline OP
Planetary brain
RusselB  Offline OP
Planetary brain
R

Joined: Aug 2004
Posts: 7,252
Ontario, Canada
Thanks.. I'll see how the timing factor is changed and make a decision if I'd rather have the longer code (which might be faster), or the shorter code (possibly slower, due to the factors you pointed out)