tags:

views:

24

answers:

3

Hi All,

Been wondering if this is doable in AWK for some time but always worked around it in the past.

Below I initialize an array with 3 months of the year... for readability I ommited the other 9 months. These months are then used in the if-statement as part of a regular expression, but AWK doesn't like it. I can't seem to find anything in the awk/gawk manuals regarding this kind of semantic... am I really stuck with restating the same code 12 times? Also is it possible to use arr[i] in the loop as a substring of a variable name? I wrote pseudo-code below to give an idea of what I'm trying to accomplish. I know it's doable in SNOBOL ;-) Thanks!

  BEGIN {   
        arr[0] = "AUG"
        arr[1] = "SEP"
        arr[2] = "OCT"
    }
    {
        for(i in arr)
        {
            if($1 ~ /arr[i]/)
            {
             #Controls flows into here if $1 matches AUG, SEP, OCT
             #Furthermore, pretend I want to intialize a variable like AUGseen:
                       arr[i]seen = 1
            }
        }
    }

If either of these things are doable I greatly appreciate pointers!

+3  A: 

You can use match for dynamic regex'es.

if(match($1, arr[i]))
DarkDust
If I understand the question correctly this is not responsive. The OP appears to want to do `match($1, arr)` and have it return true if $1 matches *any* element of `arr`.
dmckee
Tested, works great, so upvoted. Any idea if the dynamic variable names are doable too? Thx
jparanich
dmckee, DarkDust had it correct.... I was worried about it being mis-interpreted the way you have though.
jparanich
Scratch that, then.
dmckee
@jparanich: No, dynamic variable names are not possible AFAIK, but just use another array: `seen[i] = 1`.
DarkDust
+1  A: 

I don't think that awk supports this concept, but using a function will be just as efficient:

# fail is the default return code, the user should insure that it does not
# exist as a key to arr
function amatch(candidate, arr, fail) {
  for (i in arr) 
      if ( match(candidate,array[i]) ) return i;
  return fail;
}
dmckee
This is useful to have too, thanks dmckee. I haven't really ventured outside of the different variations of awk (other than gawk), so maybe it has been covered in some other awk implementation.
jparanich
A: 

here's an alternative, it doesn't use arrays. (you can keep the array for other purposes)

BEGIN {
  dates="AUG|SEP|OCT"

}
{
  if( $1 ~ dates) {
     print ...
  }

}
ghostdog74