3

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!

jparanich
  • 8,372
  • 4
  • 26
  • 34
  • You don't want to create a variable like "AUGseen" -- dynamic variable names are nasty. You should use an array for that too: `seen[arr[i]] = 1` – glenn jackman Aug 24 '10 at 20:02

3 Answers3

3

You can use match for dynamic regex'es.

if(match($1, arr[i]))
DarkDust
  • 90,870
  • 19
  • 190
  • 224
1

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 --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • 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 Aug 24 '10 at 17:46
1

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
  • 327,991
  • 56
  • 259
  • 343