|
|
Joined: Dec 2002
Posts: 68
Babel fish
|
OP
Babel fish
Joined: Dec 2002
Posts: 68 |
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.
|
|
|
|
Joined: Dec 2002
Posts: 417
Fjord artisan
|
Fjord artisan
Joined: Dec 2002
Posts: 417 |
$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
Intelligence: It's better to ask a stupid question, then to prove it by not asking....
|
|
|
|
Joined: Dec 2002
Posts: 68
Babel fish
|
OP
Babel fish
Joined: Dec 2002
Posts: 68 |
The problem with that is some numbers could be a different number of digits, so they'd return incorrect values.
|
|
|
|
Joined: Dec 2002
Posts: 417
Fjord artisan
|
Fjord artisan
Joined: Dec 2002
Posts: 417 |
What triggers the 192d29h1m what does it refer to a Time or Location refferance?
Intelligence: It's better to ask a stupid question, then to prove it by not asking....
|
|
|
|
Joined: Dec 2002
Posts: 2,962
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,962 |
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: 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.
Spelling mistakes, grammatical errors, and stupid comments are intentional.
|
|
|
|
Joined: Dec 2002
Posts: 1,922
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 1,922 |
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".
|
|
|
|
Joined: Dec 2002
Posts: 2,962
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 2,962 |
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.
Spelling mistakes, grammatical errors, and stupid comments are intentional.
|
|
|
|
Joined: Aug 2003
Posts: 1,831
Hoopy frood
|
Hoopy frood
Joined: Aug 2003
Posts: 1,831 |
Might be simpler to use $duration() mootoo {
var %a = $$1
!.echo -q $regsub(%a,/(\d+[a-z])/gi,\1 $chr(32),%a)
echo -a $duration($duration(%a))
}
|
|
|
|
Joined: Dec 2002
Posts: 1,922
Hoopy frood
|
Hoopy frood
Joined: Dec 2002
Posts: 1,922 |
Sounds reasonable. I might start to use pattern names in demonstration scripts too.
|
|
|
|
|
|