3

I am using awk. I would like to modularize my code and I would like to know whether variables declared inside a function is a local or global.. For instance

  main script
  update()

  function update()
  {
      array[1]="hi"
  }

I would like to know whether the array which is declared inside the function is a local or a global .. If it is not local then.. what is the concept of local variable in awk.

4

1 に答える 1

6

They are global:

awk 'function update() { array[1]="hi" } BEGIN { update(); print array[1];}'
hi

To make it local, you need a little trick, pass it as an argument:

awk 'function update(array) { array[1]="hi" } BEGIN { update(); print array[1];}'
于 2012-07-30T07:52:09.907 に答える