From b834517baac25495a9376b255cedb670545c74ad Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 20 Jan 2023 08:54:22 +0100 Subject: global-functions: implement $Grep... ... that returns the first line that matches a pattern. --- global-functions | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'global-functions') 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 ]; -- cgit v1.2.3-54-g00ecf