Ring Documentation Walkthrough #7.2 – Lists 3

Glad you made it through to this part, this is the final part in the lists chapter.

We will learn today about these points:

  1. Nested Lists.
  2. Copy Lists.
  3. First-Class Lists (Storing, Passing & Returning).
  4. Using lists during definition.
  5. Passing lists to functions.
  6. Access List Items by String Index (Dictionaries).
  7. Pass parameters to functions using dictionaries.

You may find this post longer, so you may want to read this post on 2 times, any question please put it in the comments.

Nested Lists:

Well, it’s common thing in many other language but i have to cover it.

list = [ [ 'Messi', '28 years old' ], 
       [ 'Cristiano Ronaldo', '31 years old'] ]

see list

see list[1] #output: Messi, 28 years old.
see list[1][1] #output: Messi
see list[1][2] #output: 28 years old.

see list[2] #output: Cristiano Ronaldo, 31 years old.
see list[2][1] #output: Cristiano Ronaldo
see list[2][2] #output: 31 years old.

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


Copy Lists:

Nothing new here, but here’s an example.

list = ['Arduino']
list2 = list
list2[1] = 'Raspberry Pi'
see list[1] + nl #output: Arduino
see list2[1] #output: Raspberry Pi

First-class lists:

Also nothing new here, Lists are first-class citizens where we can:

  1. store lists in varaibles
    list = [1, 2, 3]
  2. pass lists to functions
    for i in return_me( [1, 2, 3] )
       see i
    next
    func return_me x
       return x
    #output: 123
  3. return lists from functions
    for i in return_me( [1, 2, 3] )
       see i
    next
    func return_me x
       return x
    #output: 123

Using Lists during definition:

This may be new for you, so here’s an example.

#index:   1
list = [ 'na', list[1], list[1] ]
for i in list
   see i + ' '
next #output na na na

#index:  1  2  3
list = [ 2, 0, 1, list[3] ]
for i in list
   see i
next #output: 2011

Passing Lists to Functions:

list = ['Dead']
addsomething(list, 'pool')
see list[1] + list[2]

func addsomething x, y
add (x, y)
#output : Deadpool.

Access List Items by String Index (Dictionaries):

This is something most likely as Dictionaries in Python :

x = {'Movie' : 'The Revenant'}

In Ring:

list = [ ['Movie', 'The Revenant'],
         ['Starring', 
              [ 
                'Leonardo Di Caprio as Hugh Glass',
                'Tom Hardy as John Fitzgerald',
                'Domhnall Gleeson as Captain Andrew Henry'
              ] 
         ] ]
see list['Movie'] + nl + nl
see 'Starring : ' + nl
see list['Starring']

BUT,

of course we can make it easier, that was just for explaining the main idea.
Remember this operator :  ? We’re using it today, finally.

For more about the operators in Ring go check this link here

list = [ :Movie = 'The Revenant',
         :Starring = ['Leonardo Di Caprio as Hugh Glass', 'Tom Hardy as John Fitzgerald','Domhnall Gleeson as Captain Andrew'] 
       ]
see list['Movie'] + nl + nl
see 'Starring : ' + nl
see list['Starring'] #output is the same as the previous complicated one.

Last thing, Pass parameters to functions using dictionaries:

info = [ :Name = 'Facebook', :First_launch = 'February 4, 2004', :Founder = 'Mark Zuckerberg', :Headquarters = 'Menlo Park, California, US' ]

print_info( info )

func print_info data

see 'Name : ' + data['Name'] + nl +
    'First Launch : ' + data['First_launch'] + nl +
    'Founder : ' + data['Founder'] + nl +
    'Headquarters : ' + data['Headquarters']

Done!, This post maybe bigger than the previous posts and has more content.
but the things in this post are kind of related to each other.

Thank you and see you in the next chapter.

amrESSAM.

Advertisements
Ring Documentation Walkthrough #7.2 – Lists 3

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