diff options
author | Christian Hesse <mail@eworm.de> | 2023-01-20 08:54:22 +0100 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2023-01-20 11:26:53 +0100 |
commit | b834517baac25495a9376b255cedb670545c74ad (patch) | |
tree | 424aa3534f221ee729f863f2134c7ff96ed2c54e | |
parent | 072d34947321fff4afa4e7df059874c3523eaccd (diff) |
global-functions: implement $Grep...
... that returns the first line that matches a pattern.
-rw-r--r-- | global-functions | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/global-functions b/global-functions index cfd9091..ac5770b 100644 --- a/global-functions +++ b/global-functions @@ -30,6 +30,7 @@ :global GetRandom20CharAlNum; :global GetRandom20CharHex; :global GetRandomNumber; +:global Grep; :global HexToNum; :global IfThenElse; :global IsDefaultRouteReachable; @@ -367,6 +368,26 @@ :return [ :rndnum from=0 to=[ $EitherOr [ :tonum $1 ] 4294967295 ] ]; } +# return first line that matches a pattern +:set Grep do={ + :local Input ([ :tostr $1 ] . "\n"); + :local Pattern [ :tostr $2 ]; + + :if ([ :typeof [ :find $Input $Pattern ] ] = "nil") do={ + :return []; + } + + :do { + :local Line [ :pick $Input 0 [ :find $Input "\n" ] ]; + :if ([ :typeof [ :find $Line $Pattern ] ] = "num") do={ + :return $Line; + } + :set Input [ :pick $Input ([ :find $Input "\n" ] + 1) [ :len $Input ] ]; + } while=([ :len $Input ] > 0); + + :return []; +} + # convert from hex (string) to num :set HexToNum do={ :local Input [ :tostr $1 ]; |