codemorphis.com Forum Index codemorphis.com
Software development: pure and simple.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programming Tip #12: The Array Component

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    codemorphis.com Forum Index -> Visual Programming Forum
View previous topic :: View next topic  

Please rate on a scale of 1-5 (5 being max)
how helpful (clarity, ease of understanding, relevance) this article was for you.
You must be logged in to vote.
1
0%
 0%  [ 0 ]
2
0%
 0%  [ 0 ]
3
0%
 0%  [ 0 ]
4
0%
 0%  [ 0 ]
5
100%
 100%  [ 2 ]
Total Votes : 2

Author Message
CodeMorphis



Joined: 02 Dec 2003
Posts: 252

PostPosted: Sun Nov 07, 2004 7:52 pm    Post subject: Programming Tip #12: The Array Component Reply with quote

Programming Tip #12: The Array Component


The Array component represents a common structure in computing used to store a list of data. You can use an Array component to maintain data lists throughout the lifetime of your program for quick access and updating.

Let's see how to add data to the Array component. Below, we see an instance of the Array component, which is available from the Data tab of the Component Tray. The Array component has a green background to indicate that it is an object component, which means that its data is accessible throughout the duration of the program. Note that by default, the active service of the component is the "Add item(s)" service, which allows us to add data to the component. Double click the blue input data port of the Array component to open the Input Argument Editor, where you can supply data. You can add integer data (whole numbers), float data (real numbers) or string data (textual strings). You can add one or more data in the Value edit field. To add more than one data, specify new data on separate lines. To add a new line in the Value edit field type Ctrl-Enter.



Double click the edit box in the Value column to open a larger dialog where you can view/edit the entire data specified. This makes it easier to add a lot of data:



Drag and drop a Service Call component (Program tab of the Component Tray) and connect it as below. Select the Array component and its Print service in the Service Call component (refer to the Synopsis Help module to see how to connect the Service Call component to the Array component. Also view the programming tip http://www.codemorphis.com/forums/viewtopic.php?t=19). If you run the program you will see the contents of the Array component in the console output area.



You can also use the "Num items" service of the Array component to get the current number of data elements:



The elements in the Array component are ordered with indices starting at 0. This means that the first element has an index of 0, the second element has an index of 1, the third an index of 2, etc. If we need to access a particular indexed element in the Array component we can use the "Get item" service as below:



In the program above, we set the argument of input data port 0 of the "Get item" service call to 3, indicating the fourth element in the array. Remember that the array elements are indexed from 0, so index 0 refers to the first element, index 1 refers to the second element, etc. Run the program and the fourth element gets written to the console output area, namely the number "4".

We can make things a little more interesting by changing the data elements in the Array component to strings. To do this, double click the blue input data port of the Array component (that has the "Add item(s)" service selected) and change the Type from Integer to String. Then double click the edit box for the Value and set the elements as below:



Now if you re-run the program, the output is "This is the fourth element" instead of "4".

You can also search for an element in the Array component using the "Find item" service. In the program layout below, we have set the input data port argument of the "Find item" service call to "This is the seventh element". If you run the program the result output in the console output area is the index of the searched element, namely 6. If the searched element is not found in the Array component then the index returned is -1, which of course is an invalid index since indices start at 0.



You can download this program here:
http://www.codemorphis.com/articles/tip12/program4.vpd

You can also delete specific elements from the data list with the "Delete" service. To delete elements you specify the starting index and the number of elements to delete. In the program layout below, we specified the start index as 1 and 2 for the number of elements to delete. The result is that the second and third elements get deleted. The resulting data list is output to the console output area. Note that the second and third elements are gone.



To specify the delete arguments, right click the Service Call component with the "Delete" service and select "Input Argument(s)":



You can also insert new elements in the Array component using the "Insert" service. Change the active service of the Service Call component with the "Delete" service to "Insert" by double clicking it and in the "Active Service" property, select the "Insert" service.

Right click the Service Call component with the newly selected "Insert" service call and select "Input Argument(s)". Set the elements to be inserted in the "Item(s)" field as below. The default insertion index is 0. This means that insertion of the new items will be made before the first element of the Array component's current data list.



Now if we run the program, we see the new inserted items in the Array's output list:



We can also change the value of a given item in the array with the "Set item" service. Below is the program layout using the "Set item" service.



We set the arguments of the "Set item" service call as below:



This caused the sixth item (index 5) to be changed with the new value "Changed item".


Using the services of the Array component we can see that it is possible to store, access and manipulate a set of data items throughout the lifetime of a program. Furthermore, it is possible to maintain the data elements in a desired order which you can access randomly using the "Get item" service. This random access is particularly useful when used in conjunction with the For Loop component (from the Logic tab of the Component Tray), for example, where we can loop through a range of indices and at each iteration, access a particular element in the array.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CodeMorphis



Joined: 02 Dec 2003
Posts: 252

PostPosted: Sun May 22, 2005 8:08 am    Post subject: Reply with quote

The programming tips are supplemental articles that cover particular visual programming concepts and tasks using Synopsis. As such, they appear in the Forums often in response to user inquiries and questions posted in the Forums or received offline. Feel free to copy or print these programming articles for your own use.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    codemorphis.com Forum Index -> Visual Programming Forum All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group