HomeForumResearchGuide
<< back to How To home

This program shows how we can search for values in an array of data instances (or check if a value exists).

Let's assume that we have an array of String instances; create a new file Main.dn and open it in a text editor. We're going to use the data.query.Search interface, the command-line output interface, and use the String type:

component provides App requires io.Output out, data.query.Search search {
	
}

The App interface has one function called main. Any executable component will implement App, indicating it has a main method that Dana can launch.

We now need to implement our App interface, which we'll do like this:

component provides App requires io.Output out, data.query.Search search {

int App:main(AppParam params[])
	{
	String lst[] = new String[](new String("one"), new String("two"), new String("three"), new String("one"))
	
	String values[] = lst.find(String.[string], new String("one"))
	
	return 0
	}

}

This program first creates an array of String instances which we'll be searching over. It then uses the find function of data.query.Search, as an associative function, and provides as parameters the data type field to match against using reflective field selection, plus a String instance which has that field set to the value we're going to search for.

The find function will return an array of all instances from the lst array which match the requested value for this field.

We can now compile the program using the Dana compiler. Open a command-prompt in the directory containing your source code file and type:

dnc Main.dn

And pressing enter. This will compile your component.

We run the program using Dana's interpreter by typing:

dana Main

And pressing enter. You should see the current date and time printed every second. Press Ctrl + C to quit the program.

The full list of standard interfaces that you can use in requires directives can be viewed at the Dana API pages along with documentation of each one. In this example we used: