bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: interpreting a strongly typed regexp constant variable as literal re


From: Craig
Subject: Re: interpreting a strongly typed regexp constant variable as literal regular expression?
Date: Sat, 30 May 2020 16:33:42 -0500

Ok,

Suggested awk feature/addition:

So, currently, in order to have @/val/ in example 2 be used/evaluated
as a regular expression statement like /^a/ in example 1; the switch
block containing the strongly typed regular expression has to be dumped
into a temporary awk script.  This temporary awk script is then
called/executed to produced the desired result.

I'd be nice to just be able to just add @ before the switch keyword,
( aka @switch) to indicate to awk that at runtime the strongly typed
regular expression(s) in switch block should be updated with constants
before awk executes the @switch block.

A general approach to getting around having to generate/shell out to
a temporary awk script in order to replace the typed regular expressions
in a switch case with constants would be to add a way to indicate to awk
that at run-time, there are strongly typed regular expressions in
the a switch block that need to be evaluated before the switch block
is run.

1) switch block specific method
Add @switch to indicate need to fill-in/evaluate the the strongly typed
regular expression(s) in switch case lines with constants before switch
block is entered. Only strongly typed constants that typeof() returned
regexp would be changed as if the regexpr had a / at the start & end.
aka "case @foo :" foo changed to a string with / before and after,
so  "case /foo_string/"

2) Genaral method, not specific to switch block
A @{ ... @} block around the switch block would tell awk to first
re-evaluate the strongly typed regular expression constant variable(s)
contained within the @{ ... @} block before executing the awk code
within the @{ ... @} block.  The @{ ... @} block is a general approach
to make things easier to gawk & understand what's going on.
It would allow any strongly typed regular expressions in a awk block
statement to be evaluated at-run before the block within the @{...@}
is evaluated.
Note:  This would not parse/evaluate sub-blocks within the @{ ... #} block
example uses:

 * "case @/foo/ :" would be interpreted as if "case /foo/ : was the line

 * @func_name(val) would result in the value returned by the function
    specified by @func_name
 * function() could be called too if results of function() assigned to a
   regexpr variable & function returned a value that could be used in switch
   case line

Thanks,
Craig




On Sat, May 30, 2020 at 11:28 AM Craig <sargonemail@gmail.com> wrote:

> Ok,
>
> This was originally asked in comp.lang.awk and was asked to be
> sent to the bug list.
> ( https://groups.google.com/forum/#!topic/comp.lang.awk/DjvDi3E1-XU )
>
> Ok, as of awk 4.2, regular expression constants are a first-class
> data type using the syntax @/foo/.
> Per  Edition 5.1 of "GAWK: Effective AWK Programming: A User’s
> Guide for GNU Awk", Section 6.1.2.2 "Strongly Typed Regexp Constants",
> /@var/ should be useable in the case part of a switch statement.
> Basic, working script examples discussed provided at the end of
> this message.
>
> What is the syntax to get 'case @/var/ :' in script 2
> to be interpreted as an actual case constant regular expression
> and not a string?
> e.g In script 2, the line 'case @/var/ :' needs to be evaluated as
> if the line was script 1's line, 'case /var/ :' and not as if the
> line was script 3's line, 'case "@/var/".
>
> For the given examples,  Script 1 generates the desired output.
> The desired output for script 2 should be the same the output
> generated by script 1.
> As provided, in script 2, the 'Yes" is printed as the 4th record
> instead of 2nd record because @/var/ isn't interpreted as an
> actual regular expression, but as a string (as in script 3's
> example).
>
> typeof() shows the script 2 variable as being type regexp.
>
> Thanks,
> Craig
>
> ===== script 1
> awk '{
> switch($0) {
>    case /^a/ :
>       print "Yes"
>    break
>    default :
>       print "NR: " NR
>    break
>    }
> }' << END
> g
> a
> c
> @/a/
> END
>
> ==== script 1 output:
> NR: 1
> Yes
> NR: 3
> NR: 4
>
> ===== script 2
> awk '{ var = @/^a/
> switch($0) {
>    case @/var/ :
>       print "Yes"
>    break
>    default :
>       print "NR: " NR
>    break
>    }
> }' << END
> g
> a
> c
> @/var/
> END
>
> ==== script 2 output
> NR: 1
> NR: 2
> NR: 3
> Yes
>
> ===== script 3
> awk '{ var = @/^a/
> switch($0) {
>    case "@/var/" :
>       print "Yes"
>    break
>    default :
>       print "NR: " NR
>    break
>    }
> }' << END
> g
> a
> c
> @/var/
> END
>
> ==== script 3 output
> NR: 1
> NR: 2
> NR: 3
> Yes
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]