Ring Documentation Walkthrough #7.1 – Lists 2

I’m back!, Sorry for the absence, i’ve been busy & sick for the past days.
I will continue at the Lists topic, we will learn today new 5 function in the lists.
Find, Sort, Reverse & Insert. Enjoy!


Find:

find (list, itemValue) #Returns the item index
find (list, itemValue, column) #Searches in a specific column
and returns the item index.

Example:

top10_visitedCities = ['Bangkok', 'London', 'Paris', 'Singapore', 'New York', 'Istanbul', 'Dubai', 'Kuala Lumpur', 'Hong Kong', 'Barcelona']

Dubai_index = find (top10_visitedCities, 'Dubai') 
see Dubai_index #output : 7

NOTE: The index in Ring starts from 1 not 0 like the other languages.

Search in a specific column:

Asia = [ ['China', 'Beijing'] , ['Japan', 'Tokyo'] ]

Listen here because it may get complicated, in the previous variable ‘Asia‘,
I defined 2 arrays in it : [China, Beijing] & [Japan, Tokyo].
Column #1 contains ‘China‘ and ‘Japan
Column #2 contains ‘Beijing‘ and ‘Tokyo
We can rewrite the code like this :

#         Column1    Column2
Asia = [ ['China', 'Beijing'],
         ['Japan', 'Tokyo'] ]
see find (Asia, 'China', 1) #output : 1, Japan is the second item
in that column.
see find (Asia, 'Japan', 1) #output : 2

see find (Asia, 'Beijing', 2) #output : 1, Tokyo is the second item
in that column.
see find (Asia, 'Tokyo', 2) #output : 2

BinarySearch:

list = [5, 4, 1, 8, 2, 9]
list = sort(list)
see binarysearch(list, 2) #output: 2

Sort:

sort (list, (column) ) #Sorts the list normally or sorts it
based on a specific column

Example:

list = [ 5, 3, 1 ]
list = sort(list)
see list #output : 1, 3, 5
list = ['d', 'e', 'i', 'a', 'b', 'z']
list = sort(list)
see list

Example 2:

list = [ ['b', 1],
         ['c', 2],
         ['a', 3] ]
list = sort (list, 1) 
see list #output : list is sorted based on the first column
which is the alphas. 

list = sort (list, 2) 
see list #output : list is sorted based on the second column
which is the numbers. 

Reverse:

list = [ 1, 2, 3 ]
list = reverse (list) 
see list #output : 3, 2, 1

Insert:

insert (list, index, value)

The item will be inserted after the index.

list = [ 1, 2, 4 ]
insert (list, 2, 3)
see list #output : 1, 2, 3, 4

Done!, thank you for following the tutorial.

See you in the last part of lists.

amrESSAM.

Advertisements
Ring Documentation Walkthrough #7.1 – Lists 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s