mIRC Homepage
Posted By: phrozenfire Retrieving Numbers - 26/11/03 01:00 AM
In 192d29h1m, how could I get the number before the d, the h and the m? Any of the numbers can change, or certain sections (e.g. '192d' or '29h') could be missing.
Posted By: Othello Re: Retrieving Numbers - 26/11/03 01:47 AM

$left(text,N) Returns the N left characters of text.

$right(text,N) Returns the N right characters of text.

$left(text,1-4) Returns 192d'
or '

$right(text,3-) Returns 29h
Posted By: phrozenfire Re: Retrieving Numbers - 26/11/03 02:25 AM
The problem with that is some numbers could be a different number of digits, so they'd return incorrect values.
Posted By: Othello Re: Retrieving Numbers - 26/11/03 02:57 AM
What triggers the 192d29h1m what does it refer to a Time or Location refferance?
Posted By: starbucks_mafia Re: Retrieving Numbers - 26/11/03 03:28 AM
A regular expression would be a simple way to do what you want. Here's an example alias that takes the '192d29h12m'-type string as it's parameter:
Code:
moo {
  [color:red]; Call regular expression. All results will be [i]x[/i] amount of numbers ending with a single letter.[/color]
  !.echo -q $regex(parsetime, $$1, /(\d+[a-z])/gi)
  var %i = 1
  [color:red]; Loop through results of regular expression call[/color]
  while $regml(parsetime, %i) {
    [color:red]; Assign rightmost character (the letter) to %char. Assign everything else to %num.[/color]
    var %char = $right($ifmatch,1), %num = $left($ifmatch,-1)
    [color:red]; If %char is d set %num variable %days[/color]
    if (%char == d) var %days = %num
    [color:red]; Hours checking/assignment...[/color]
    elseif (%char == h) var %hours = %num
    [color:red]; Mins checking/assignment...[/color]
    elseif (%char == m) var %mins = %num
    inc %i
  }
  echo -a %days $+ days %hours $+ hours %mins $+ mins
}


Hopefully it all makes sense with the comments I've added. Of course you'll probably want more than just days, hours, and minutes, you can just add more checks in the same format as the others.
Posted By: Online Re: Retrieving Numbers - 26/11/03 03:46 AM
Yep, I thought about this solution too, but didn't know how to clearly state it. Btw, you can use $regml(N) to directly reference to the last $regex call without having to use "pattern names".
Posted By: starbucks_mafia Re: Retrieving Numbers - 27/11/03 02:57 PM
Personally I don't like using $regml() with unnamed result sets. In many ways it's like storing the result in a global variable - any other function you call between $regex() and $regml() could potentially use a regular expression also and overwrite the results. Of course I know my example didn't use any, but since it was just a basic demonstration it will almost certainly be changed with God-knows-what put in there. Even then the chances are slim of a $regml() 'collision' but it's better to have peace of mind IMO.
Posted By: Iori Re: Retrieving Numbers - 28/11/03 01:26 AM
Might be simpler to use $duration()
Code:
mootoo {
  var %a = $$1
  !.echo -q $regsub(%a,/(\d+[a-z])/gi,\1 $chr(32),%a)
  echo -a $duration($duration(%a))
}
Posted By: Online Re: Retrieving Numbers - 28/11/03 10:12 AM
Sounds reasonable. I might start to use pattern names in demonstration scripts too.
© mIRC Discussion Forums