String array vs. regexp 
Author Message
 String array vs. regexp

I am writing a program to convert a text using two sets of arrays (read
from a file): _from_ and _to_ (changes the text using from to to). So why
doesn't

for (x in from)
$0 ~ from[x] {gsub(from[x], to[x])}

work? Is there any problem with converting array values to regexps? I'm
using gawk at a Linux box. Thanks for any help.

PS. Or maybe

$0 ~ from[x] {for (x in from)... (but that doesn't tell what x is, unless I
                                  use x++)



Wed, 22 Mar 2000 03:00:00 GMT  
 String array vs. regexp


Quote:
> I am writing a program to convert a text using two sets of arrays (read
> from a file): _from_ and _to_ (changes the text using from to to). So why
> doesn't

> for (x in from)
> $0 ~ from[x] {gsub(from[x], to[x])}

> work?

Because of incorrect syntax. Try this:

{
    for (x in from)
        if ($0 ~ from[x])
            gsub(from[x], to[x])

Quote:
}
> Is there any problem with converting array values to regexps?

There is no problem using array elements as regular expressions, as long
as those array elements are strings. And in awk, all array elements are
strings when they are used as strings. The trick is knowing how to use
strings as regular expressions and, in particular, how to handle regular
expression metacharacters in those strings.

Here is an example using an awk script named sub.awk and Gwidon's
news article as input (which is embedded as comments within the
script itself):

$ cat sub.awk
BEGIN {
    from[1] = "Sat, "
    to[1]   = "Saturday, "

    from[2] = "4 Oct "
    to[2]   = "October Fourth, "

    from[3] = "1997 "
    to[3]   = "Nine{*filter*} Hundred Ninety-Seven "

    from[4] = "\\(x"
    to[4]   = "(i"

    from[5] = "\\[x\\]"
    to[5]   = "[i]"

Quote:
}

/^#/ {
    for (x in from)
        if ($0 ~ from[x])
            gsub(from[x], to[x])
    print

Quote:
}


# Newsgroups: comp.lang.awk
# Subject: String array vs. regexp
# Date: Sat, 4 Oct 1997 12:25:50 +0200
# Organization: Adam Mickiewicz University
# Lines: 15
#
# I am writing a program to convert a text using two sets of arrays (read
# from a file): _from_ and _to_ (changes the text using from to to). So why
# doesn't
#
# for (x in from)
# $0 ~ from[x] {gsub(from[x], to[x])}
#
# work? Is there any problem with converting array values to regexps? I'm
# using gawk at a Linux box. Thanks for any help.
#
# PS. Or maybe
#
# $0 ~ from[x] {for (x in from)... (but that doesn't tell what x is, unless I
#                                   use x++)
$ gawk -f sub.awk sub.awk

# Newsgroups: comp.lang.awk
# Subject: String array vs. regexp
# Date: Saturday, October Fourth, Nine{*filter*} Hundred Ninety-Seven 12:25:50 +0200
# Organization: Adam Mickiewicz University
# Lines: 15
#
# I am writing a program to convert a text using two sets of arrays (read
# from a file): _from_ and _to_ (changes the text using from to to). So why
# doesn't
#
# for (i in from)
# $0 ~ from[i] {gsub(from[i], to[i])}
#
# work? Is there any problem with converting array values to regexps? I'm
# using gawk at a Linux box. Thanks for any help.
#
# PS. Or maybe
#
# $0 ~ from[i] {for (i in from)... (but that doesn't tell what x is, unless I
#                                   use x++)
$ gawk --version | head -1
GNU Awk 3.0.34 (BETA))
$

Notice that, to substitute "[x]" with "[i]", the regular expression string
stored in from[5] must be "\\[x\\]", not "[x]". The GAWK Manual does an
excellent job of explaining why.

--
Jim Monty

Tempe, Arizona USA



Wed, 22 Mar 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Array String.match(Regexp)

2. Array to String and String to Array

3. array name ARRAY regexp

4. strings vs arrays in python

5. strings vs arrays in python -Reply

6. Multidimensional array vs. array of array

7. Convert 1D string Array to Numeric Array

8. convert a string array to a 2-d array

9. Adding strings manually to an array of strings

10. Stylistic question: returning strings vs. pointers to strings

11. equivalencing a charactor string array to a double array

12. string index vs split, for vs foreach

 

 
Powered by phpBB® Forum Software