[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.axapta.programming

Is there a function to check if an element exists in array (in_arr

Roman Kucko

12/13/2005 9:09:00 AM

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.
8 Answers

Luegisdorf

12/13/2005 10:18:00 AM

0

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

12/13/2005 12:39:00 PM

0

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

12/13/2005 12:51:00 PM

0

If you work with a container, you've got the conFind() function, but for
an array... :-S

Roman Kucko wrote:
> 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

Anish Abslom

12/13/2005 1:01:00 PM

0


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

12/13/2005 1:06:00 PM

0


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

Anish Abslom

12/13/2005 1:06:00 PM

0


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

Luegisdorf

12/13/2005 2:18:00 PM

0

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

"Player" wrote:

> If you work with a container, you've got the conFind() function, but for
> an array... :-S
>
> Roman Kucko wrote:
> > 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
>

Roman Kucko

12/13/2005 4:46:00 PM

0

Thank you all for your help.
I think I will try your solutions.

Roman