Skip to main content
More descriptive title.
Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

awk referring to an Referencing array elementelements by strings, and initialising arrays in awk

Tweeted twitter.com/StackUnix/status/1073050013352239104
added 136 characters in body
Source Link
#!/usr/bin/env bash
awk '
  BEGIN {
    arr[A]=1;
    arr[B]=1;
    arr[C]=1;
    arr[E]=1;
    arr[J]=8;
    arr[Q]=10;
    print arr[J]
  }'

the above command outputs the latest set value for arr['subscript'], in this case 10 that is value of arr[Q] just before print and not 8 that is the value of arr[J].

Also, like in the script above, I don't want to be able to assign in few line command valuevalues to all arr['subscript']arr['A'], arr['B'], arr['C'] and arr['E'] that have same value 1 one line at a time, rather pass an array of subscripts as one of the parameters and not gocommon value as the other parameter to a function that handles the logic of assigning them values one by one like in the command abovevalue.

#!/usr/bin/env bash
awk '
  BEGIN {
    arr[A]=1;
    arr[B]=1;
    arr[C]=1;
    arr[E]=1;
    arr[J]=8;
    arr[Q]=10;
    print arr[J]
  }'

the above command outputs the latest set value for arr['subscript'], in this case 10 that is value of arr[Q] just before print and not 8 that is the value of arr[J].

Also I want to be able to assign in few line command value to all arr['subscript'] that have same value, and not go assigning them values one by one like in the command above.

#!/usr/bin/env bash
awk '
  BEGIN {
    arr[A]=1;
    arr[B]=1;
    arr[C]=1;
    arr[E]=1;
    arr[J]=8;
    arr[Q]=10;
    print arr[J]
  }'

the above command outputs the latest set value for arr['subscript'], in this case 10 that is value of arr[Q] just before print and not 8 that is the value of arr[J].

Also, like in the script above, I don't want to assign values to arr['A'], arr['B'], arr['C'] and arr['E'] that have same value 1 one line at a time, rather pass an array of subscripts as one of the parameters and common value as the other parameter to a function that handles the logic of assigning them value.

edited body
Source Link
#!/usr/bin/env bash
awk `'
  BEGIN {
    arr[A]=1;
    arr[B]=1;
    arr[C]=1;
    arr[E]=1;
    arr[J]=8;
    arr[Q]=10;
    print arr[J]
  }`'

the above command outputs the latest set value for arr['subscript'], in this case 10 that is value of arr[Q] just before print and not 8 that is the value of arr[J].

Also I want to be able to assign in few line command value to all arr['subscript'] that have same value, and not go assigning them values one by one like in the command above.

#!/usr/bin/env bash
awk `
  BEGIN {
    arr[A]=1;
    arr[B]=1;
    arr[C]=1;
    arr[E]=1;
    arr[J]=8;
    arr[Q]=10;
    print arr[J]
  }`

the above command outputs the latest set value for arr['subscript'], in this case 10 that is value of arr[Q] just before print and not 8 that is the value of arr[J].

Also I want to be able to assign in few line command value to all arr['subscript'] that have same value, and not go assigning them values one by one like in the command above.

#!/usr/bin/env bash
awk '
  BEGIN {
    arr[A]=1;
    arr[B]=1;
    arr[C]=1;
    arr[E]=1;
    arr[J]=8;
    arr[Q]=10;
    print arr[J]
  }'

the above command outputs the latest set value for arr['subscript'], in this case 10 that is value of arr[Q] just before print and not 8 that is the value of arr[J].

Also I want to be able to assign in few line command value to all arr['subscript'] that have same value, and not go assigning them values one by one like in the command above.

edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264
Loading
Source Link
Loading