Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

build a varibale name with value included into an array

Status
Not open for further replies.

maccarone

Technical User
Jun 20, 2002
17
0
0
IT
I need to use the name of a variable that I create using the character included into an array.

The arrayRN contains "a,b,c,d,e,f,g,h,i,l,m,n,o,p"
and with a for loop I would use each time a different value for DN and RN variable like DNa, RNa, DNb, RNb, DNc, RNc and so on

for (i in arrayRN) {
DN<here I have to put value of the array that could be from a to q> =substr($3,1,length($3)-1)
if (NF==12) {
RN<here I have to put value of the array that could be from a to q> = $12
}
else {
RN<here I have to put value of the array that could be from a to q> = ""
}
getline
if ($0 ~/Origine =/) {
break
}
}

I tried with:
RN{arrayRN}
RN[arrayRN]
RN(arrayRN)

Is there any way to do it?

Thanks

Massimo
 
Hi

The second is the one that should work : [tt]RN[arrayRN][/tt]. Then you will get the array elements like [tt]RN["a"][/tt] populated.

If you want to have exactly [tt]RNa[/tt] name like you mentioned, [tt]SYMTAB["RN" arrayRN][/tt] will do it, but that will not work in all Awk implementations.

Feherke.
feherke.ga
 
HI,
I've tried with this

BEGIN {
stringa_RN="a,b,c,d,e,f,g,h,i,l,m,n,o,p,q"
split(stringa_RN,arrayRN,",")
}

{

for (i in arrayRN) {

SYMTAB["RN" arrayRN = "prova"
print SYMTAB["RN" arrayRN
}

}



and I obtain the following error:

gawk: test_array.awk:12: SYMTAB["RN" arrayRN = "prova"
gawk: test_array.awk:12: ^ syntax error
gawk: test_array.awk:14: print SYMTAB["RN" arrayRN
gawk: test_array.awk:14: ^ unexpected newline or end of string

I'm using the gawk for Windows.

Thanks

massimo
 
Hi

There are minor syntax errors in your code :
Code:
[teal]{[/teal]
    [b]for[/b] [teal]([/teal]i [b]in[/b] arrayRN[teal]) {[/teal]

        SYMTAB[teal][[/teal][i][green]"RN"[/green][/i] arrayRN[teal][[/teal]i[teal]][highlight]][/highlight] =[/teal] [i][green]"prova"[/green][/i]
        [b]print[/b] SYMTAB[teal][[/teal][i][green]"RN"[/green][/i] arrayRN[teal][[/teal]i[teal]][highlight]][/highlight][/teal]
    [teal]}[/teal]

[teal]}[/teal]

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top