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!

finding a value in an array

Status
Not open for further replies.

cuttersw

IS-IT--Management
Aug 10, 2006
9
US
What is the simplest way to determine if an element in an array contains a particular value?

I have tried the following without success (Always returns "No!"):

[tt]
split("SMTP,smtp,X400,x400,X500,x500,MS,ms,RFAX,rfax,CCMAIL,ccmail",ValidTypes)
variable = "X400"
if (variable in ValidTypes) { print "Yes" } else { print "No!" } # << This does not work:
[/tt]


Regards,

Shaun
 
awk won't split on commas unless you tell it to. Furthermore, the keys of your array would be "1", "2", "3", etc.
Code:
BEGIN {
  split("SMTP,smtp,X400,x400,X500,x500",
    tmp, "," )
  for (k in tmp)
    valid_types[ tmp[k] ]
  variable = "X400"
  if (variable in valid_types)
    print "yes"
  else
    print "no"
}
 
I eventually figured out the missing comma from the split function, but what i didn't realize was "in" worked on the key rather than the value.

Once i got past the missing comma I came up with this earlier and it worked too..

Code:
BEGIN {
  split("SMTP,smtp,X400,x400,X500,x500,MS,ms,RFAX,rfax,CCMAIL,ccmail",ValidTypes,",")
  variable = "e400"
  for (l=1;l<=length(ValidTypes);l++) 
    if (variable == ValidTypes[l]) vok=l
  if (vok) 
    print "yes"
  else
    print "No"
}

Thanks again for your help futurelet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top