Jump to content
  • Advertisement
Sign in to follow this  
GameMasterXL

searching arrays for specific values?

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do i search an array for a specific value? like i am wanting to find a variable name inside of one array that was read in through my tokens. So my whole varname array is 100 rows down and 16 columns across, so my function reads in each token adding them to an array untill the name has come to an end then i want to see if the name gotten is in my varname array. How would i achieve this? I am finding this hard to do. Edit: these are character arrays.

Share this post


Link to post
Share on other sites
Advertisement
Having a hard time understanding, and it is most likely just me. But this is kinda what I got from you..

possible solution if I am on the right track:


int i,j, array[100,16];
String strSearch;
Bool blnFound = False; //Determines if it was found

cout << "What ever they want there search criteria to be";
cin >> strSearch;


for(i = 0; i < 100; i++)
{
for (j = 0; j < 16; j++)
{
if( array[i,j] == strSearch )
{
blnFound = True;
cout << "Search has found string";
break;
}
}
}

if(blnFound)
{
cout << "Your search was not found";
}

Something like that, im prolly mixing my VB and C++ cause the past 3 weeks thats why I've been coding in ( VB ) hope that helps!

Share this post


Link to post
Share on other sites
Sorry for my bad explanation. Is what i am creating is a parser and i am implimenting a variable system to it so i read in the variables names get the value after it bla bla bla. Then if i have read 3 variable all ready:

V x = 5
V y = 5 * 3
V z = 2 / 5

Then what if my forth one had V w = x * z then i will be calling two variable names. So i am using a character array to store the variable names in and the value of each variable is stored in the same element of a second int array. So like x(variable name)is in element 1 of varname[] and 5 is in element 1 of varval[]. So on my last variable V w = x * z which will be in the forth element of bothe varname[] and varval[] how would i search for the two variable names x and z through my varname[] character array? So like i search one name first x that was declared more down the line of the array so if the user put in the source file V w = x instead of the above how could the parser search for x in varname[] array? Thanks for any help.

PS: i am using character arrays not strings. Sorry for bad explanation

Share this post


Link to post
Share on other sites


If I'm understanding you're problem correctly, it sounds like you're using Parallel Array's and you want to find data in one by searching the other. Naturally the association between a variable stored in one array( you're character array ), and the data stored in the other is the array index; So you search the variable array until the data is found, return the index, and then index into the data array.

Here's a quick and dirty example...

const int _SIZE = 100;

char variableArray[_SIZE];
int dataArray[_SIZE];

//... code that fills the array's ...

char targetVariable;
int targetIndex = _SIZE + 1;

cout << "Enter the variable to search for\n";
cin >> targetVariable;

for(int i = 0; i < _SIZE; ++i)
{
if(variableArray == targetVariable)
targetIndex = i;
}

if(targetIndex == (_SIZE + 1))
cout << "Variable not found\n";
else
cout << "The data assocaited with that variable is " << dataArray[targetIndex] << endl;





Hope that helps

Share this post


Link to post
Share on other sites
Thanks bothe for all your help. Does this allow you to search two dimensional arrays? i wish they had an easy function for array searches, i think i will make one for my parser [smile].

Share this post


Link to post
Share on other sites
In general, having parallel arrays like that is a sign something is wrong. There are much nicer tools at your disposal. For example:


std::map<std::string, int> variables;

// To store a variable's value:
std::string name = getVariableName();
int value = getVariableValue(); // however it is that you actually get these...
variables.insert(make_pair(name, value));

// To look up a variable:
int result = (*variables.find(value)).second;
// The find call returns an iterator; dereferencing the iterator (using its
// unary operator*) returns a key/value pair; the "second" member of that is
// the value. This needs to be broken down into steps in the case that the
// value might not be there, since otherwise the dereference is invalid and will
// give undefined behaviour. Consult the doc (linked below) for more info.

// There is also a more convenient means provided, but be aware that this will
// "auto-vivify" any variable names that don't yet exist - i.e. trying to get
// a variable name that doesn't exist will create that variable with a value
// of 0 (the default value for ints):
variables[name] = value;
result = variables[name];
int bogus = variables[std::string("not declared yet muahaha")]; // bogus == 0
// and now that variable "not declared yet muahahaha" is stored in the map.



The std::map automatically implements a tree data structure that will allow for a binary search (more efficient than linear once you get a significant number of values), and expand to hold as many variables as you need. It implements a "map" of "keys" of type std::string, to "values" of type int.

Similarly, std::string manages a bit of text data and will not be limited in its length. Also, since std::string is an object, it can (and does) implement a comparison operator, which is what allows the map to work with it. (Thus you don't need to write any of the code for comparing a single variable name to the input; you can make std::map work with a char* "key", but extra work is needed).

All much cleaner and will avoid making you much about with character arrays or with keeping two arrays in sync :D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You must know that map container is very slow.

Share this post


Link to post
Share on other sites
Quote:
Original post by GameMasterXL
i wish they had an easy function for array searches


Well you never told us what language your using so i'll assume C++, the C++ standard library provides a whole familly of search/find generic algorithms including a set of binary search routines, check out all here section 5.

std::basic_string also has a whole bunch of search/find member functions too and if thats not enough then the C++ boost library has a string algorithms package that can work with any string type including c-style strings, it also has a regular expressions package.

Quote:
Original post by Anonymous Poster
You must know that map container is very slow.


Very constructive comment, now go learn C++ properly and learn algorithms & data structures while your at it.

[Edited by - snk_kid on June 15, 2005 6:15:58 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

#include <stdlib.h>
#include <string.h>

typedef struct Variable_ Variable;
struct Variable_ {
const char *name;
void *value;
};

typedef struct VariableArray_ VariableArray;
struct VariableArray_ {
Variable *array;
size_t size;
char sorted;
};

int cmpVariableName(const void *v1, const void *v2) {
const Variable *var1 = v1;
const Variable *var2 = v2;
return strcmp(v1->name, v2->name);
}

void *FindVariable(const char *varName, VariableArray *vars) {
Variable key = {0};
key.name = varName;
if (!vars->sorted) {
qsort(vars->array, vars->size, sizeof *vars->array, cmpVariableName);
vars->sorted = 1;
}
return bsearch(&key, vars->array, vars->size, sizeof *vars->array, cmpVariableName);
}

void *GetVariableValue(const char *varName, VariableArray *vars) {
Variable var = FindVariable(varName, vars);
return var.value;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!