Is it possible to use a variable as the index to a true multidimensional array? I have an gawk 4.1.1 script that runs through Cisco configuration files, and I want to create an array that looks like:
myarray[sitecode][switchname]
Where sitecode and switchname are pulled from the FILENAME being processed, and then additional indexes beyond that to calculate various things on a per-switch basis. For those two indexes above, I want to set both the index and the value to the same variable. So eventually I could have an array that looks like:
myarray[nyc01][switch01][Vlan100][192.168.100.1]
myarray[nyc01][switch01][Vlan101][192.168.101.1]
myarray[nyc01][switch02][Vlan200][192.168.200.1]
The code below illustrates what I am trying to do:
#!/bin/bash
awk '{
var1="variable1"
var2="variable2"
array[var1]=var1
array[var1][var2]=var2
print array[var1][var2]
}'
I get this error:
awk: cmd. line:6: (FILENAME=- FNR=1) fatal: attempt to use scalar `array["variable1"]' as an array
I sort of understand why it's happening. I have declared var1 and var2 to be scalar variables. But is there a work around for what I am trying to do?