Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Variables declared inside a function awk

Tags:

shell

awk

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.

like image 318
user1562262 Avatar asked Oct 27 '25 20:10

user1562262


1 Answers

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];}'
like image 80
Karoly Horvath Avatar answered Oct 30 '25 17:10

Karoly Horvath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!