[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
interpreting a strongly typed regexp constant variable as literal regula
From: |
Craig |
Subject: |
interpreting a strongly typed regexp constant variable as literal regular expression? |
Date: |
Sat, 30 May 2020 11:28:50 -0500 |
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
- interpreting a strongly typed regexp constant variable as literal regular expression?,
Craig <=