Discussion:
Is there a function to check if an element exists in array (in_arr
(too old to reply)
Roman Kucko
2005-12-13 09:09:02 UTC
Permalink
Hello,
I am new to this forum and this is my first question.
I was looking for some function to check if an element exists in array. In
other languages there are functions like in_array(), is_in(), exists() etc.
I don't believe that AXAPTA has no such function and every time I have to go
through all the array elements to check if one of them is the one which I am
looking for.

PS. I know that I write own:
.....
for (i=0;i<=LastArrayElementNr;i++)
{
if (MyArray[i]==ElementISearchFor)
{
return true;///I have found the element
}
}
...
but I think that Axapta should have such function INSIDE :) Am I wrong?
Whould be grateful for any answer, explanation or arguments.
Luegisdorf
2005-12-13 10:18:03 UTC
Permalink
Hi Roman

Not sure what you rellay will do, but it looks like you want to check the
length of an array? In facts this is bad done in Axapta with 'simple' arrays:
I propose to use the Class List or type container; both of them has count
possiblities or you just increment a counter when you put a new value to your
array.

Example for a list:
List list = new List(Types::String);
ListIterator listIterator = new ListIterator(List);
;
list.addEnd('a');
list.addEnd('b');
list.addEnd('c');
list.addEnd('d');
list.addEnd('e');


listIterator = new ListIterator(list);

while (listIterator.more())
{
info(listIterator.value());
listIterator.next();
}

Example for a container:
container con;
int i;
;
con += 'a';
con += 'b';
con += 'c';
con += 'd';
con += 'e';

for (i = 1; i <= conlen(con); i++)
{
info(conpeek(con, i));
}

Example with a counter (but if course a little bit ugly)
str strArray[];
int i;
int j;
;
i++; strArray[i] = 'a';
i++; strArray[i] = 'b';
i++; strArray[i] = 'c';
i++; strArray[i] = 'd';
i++; strArray[i] = 'e';
i++; strArray[i] = 'f';

for (j = 1; j <= i; j++)
{
info(strArray[j]);
}


Hope this helps a little.
Best regards
Patrick
Roman Kucko
2005-12-13 12:39:02 UTC
Permalink
Thank you for the answer, but it is not what I ment.
I think there should be a function telling if an element is already in the
array or the array still doesn't have such element.

I'll explain it in other words: I have an array:
EmplId EmployeesWhichAreInterestingForMe[];
then I am going through all the projects, counting their revenues, costs (in
specific way) and then I add that Employee to the array, but when I add that
employee to the array - he might already be there - so I have to check if it
is there or not.
I need an analogy of function

boolean in_array(TypeOfElement _elementToSearchFor, Array _arr)
{
returns true if one of array _arr elements is equal to
_elementToSearchFor and returns false if array _arr does not have the
_elementToSearchFor in it;
}

Having such functions in C, C++, PHP, Delphi, etc... it is so obvious to me
that there MUST be such function in Axapta :)

Roman
Player
2005-12-13 12:50:30 UTC
Permalink
If you work with a container, you've got the conFind() function, but for
an array... :-S
Post by Roman Kucko
Thank you for the answer, but it is not what I ment.
I think there should be a function telling if an element is already in the
array or the array still doesn't have such element.
EmplId EmployeesWhichAreInterestingForMe[];
then I am going through all the projects, counting their revenues, costs (in
specific way) and then I add that Employee to the array, but when I add that
employee to the array - he might already be there - so I have to check if it
is there or not.
I need an analogy of function
boolean in_array(TypeOfElement _elementToSearchFor, Array _arr)
{
returns true if one of array _arr elements is equal to
_elementToSearchFor and returns false if array _arr does not have the
_elementToSearchFor in it;
}
Having such functions in C, C++, PHP, Delphi, etc... it is so obvious to me
that there MUST be such function in Axapta :)
Roman
Luegisdorf
2005-12-13 14:18:02 UTC
Permalink
Hi Roman

You can just use a Map:

Map map = new Map (Types::string, Types::void);

while select any tables which emplId
{
if (! map.exists(any table.emplId))
{
map.insert(any table.emplid, null);
}
}

Use a MapIterator to iterate all Keys.

Best regards
Patrick
Post by Player
If you work with a container, you've got the conFind() function, but for
an array... :-S
Post by Roman Kucko
Thank you for the answer, but it is not what I ment.
I think there should be a function telling if an element is already in the
array or the array still doesn't have such element.
EmplId EmployeesWhichAreInterestingForMe[];
then I am going through all the projects, counting their revenues, costs (in
specific way) and then I add that Employee to the array, but when I add that
employee to the array - he might already be there - so I have to check if it
is there or not.
I need an analogy of function
boolean in_array(TypeOfElement _elementToSearchFor, Array _arr)
{
returns true if one of array _arr elements is equal to
_elementToSearchFor and returns false if array _arr does not have the
_elementToSearchFor in it;
}
Having such functions in C, C++, PHP, Delphi, etc... it is so obvious to me
that there MUST be such function in Axapta :)
Roman
Anish Abslom
2005-12-13 13:01:03 UTC
Permalink
Hi Roman,

It is very early to say that whether there is a single line function to
check whether an element exist in an array .Yes it is possible to find the
element if you can specify the index of the element in the array .

This can be done by
array.exist(array index);

But if you need to check for element by content then please try this peace
of code.

array Test_array = new array(types::String); //initializing the string array
Test_array.value(1,"Axapta"); // Adding contents to the array
Test_array.value(2,"Microsoft");
//**************************************************************
if ( strscan(Test_array.toString(),"Axapta
",0,strlen(Test_array.toString()))!=0) //checks for the content
{
print “Element exist……”;
pause;
}

This will avoid your looping…

hope this will help you in giving a clue to solve your issues related to
array........

With regards

Anish
Anish Abslom
2005-12-13 13:06:02 UTC
Permalink
Hi Roman,

It is very early to say that whether there is a single line function to
check whether an element exist in an array .Yes it is possible to find the
element if you can specify the index of the element in the array .

This can be done by
array.exist(array index);

But if you need to check for element by content then please try this
peace of code.

array Test_array = new array(types::String); //initializing the string array
Test_array.value(1,"Axapta"); // Adding contents to the array
Test_array.value(2,"Microsoft");
//**************************************************************
if ( strscan(Test_array.toString(),"Axapta
",0,strlen(Test_array.toString()))!=0) //checks for the content
{
print “Element exist……”;
pause;
}

This will avoid your looping…

Hope this will help you in finding a solution for your issue……..

With regards

Anish
Roman Kucko
2005-12-13 16:46:05 UTC
Permalink
Thank you all for your help.
I think I will try your solutions.

Roman
Anish Abslom
2005-12-13 13:06:03 UTC
Permalink
Hi Roman,

It is very early to say that whether there is a single line function to
check whether an element exist in an array .Yes it is possible to find the
element if you can specify the index of the element in the array .

This can be done by
array.exist(array index);

But if you need to check for element by content then please try this
peace of code.

array Test_array = new array(types::String); //initializing the string array
Test_array.value(1,"Axapta"); // Adding contents to the array
Test_array.value(2,"Microsoft");
//**************************************************************
if ( strscan(Test_array.toString(),"Axapta
",0,strlen(Test_array.toString()))!=0) //checks for the content
{
print “Element exist……”;
pause;
}

This will avoid your looping…

Hope this will help you in finding a solution for your issue……..

With regards

Anish
Joseph
2006-03-24 08:56:02 UTC
Permalink
Post by Luegisdorf
Hi Roman
Not sure what you rellay will do, but it looks like you want to check the
I propose to use the Class List or type container; both of them has count
possiblities or you just increment a counter when you put a new value to your
array.
List list = new List(Types::String);
ListIterator listIterator = new ListIterator(List);
;
list.addEnd('a');
list.addEnd('b');
list.addEnd('c');
list.addEnd('d');
list.addEnd('e');
listIterator = new ListIterator(list);
while (listIterator.more())
{
info(listIterator.value());
listIterator.next();
}
container con;
int i;
;
con += 'a';
con += 'b';
con += 'c';
con += 'd';
con += 'e';
for (i = 1; i <= conlen(con); i++)
{
info(conpeek(con, i));
}
Example with a counter (but if course a little bit ugly)
str strArray[];
int i;
int j;
;
i++; strArray[i] = 'a';
i++; strArray[i] = 'b';
i++; strArray[i] = 'c';
i++; strArray[i] = 'd';
i++; strArray[i] = 'e';
i++; strArray[i] = 'f';
for (j = 1; j <= i; j++)
{
info(strArray[j]);
}
Hope this helps a little.
Best regards
Patrick
Can anyone please tell me how i can use confind?
Note:
My container contains 3 fields being:
RoomId
Complains
Date

what I need is how to search for the same date in this container so that
using sets I can remove the dupplicates?
Loading...