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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing array subscript strings

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
Is there any way(besides building a reversed array)
to get at the subscript strings of an awk array?

Say:
array[string1]
array[string2]

for (i in array) {
if (i ~ /.*2/) {
print array
}
}

Of course I know how awk keeps it's indices and arrays.
I am just saying that without a construct like:

array[1] = "string1"
array[2] = "string2"

On the other side of the array and an option
passed back and forth in the function call to activate
the reverse lookup of subscripts is there an easier
way anyone has ever used or seen to access subscript
literals?
 
Here is what I needed.
I know this has probably been done before, but
I had to reinvent the wheel to figure it out.
If I am not mistaken this functional set could really
help with some intuitive indexing operations.
Can anybody see a use for it?


function build_rev_array(ss, rev) {
print ss
rev[cnt] = ss ; return rev[cnt]
}

function access_by_subscr(arr,opt,s, i) {
print s
if (opt == "0" && s == "0") {
for (all in arr) {
print "Subscript = ", all
print "Value = " ,arr[all]
i++
}
return i
} else {
print "Searching", s
for (all in arr) {
if (all ~ s) {
return arr[all]
}
}
}
}

function searcher(arr,str, ret) {
ret = access_by_subscr(arr,1,str)
if (ret) {
print ret
} else {
print "No matching ss found."
}
}


BEGIN {
n = 0
while (n < 5) {
n++
printf &quot;Number or name subscript: &quot;
getline ss < &quot;-&quot;
got = build_rev_array(ss)
printf &quot;Input: &quot;
getline name[got] < &quot;-&quot;
print &quot;Value: &quot;, name[got], &quot;Count: &quot;, n
}
jiggy = access_by_subscr(name,0,0) ; print jiggy,&quot;elems&quot;

printf &quot;SS pat to search out: &quot;
getline search < &quot;-&quot;
searcher(name,search)
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top