Pooh program example 17-for-lib.p
# co-routines / generators as used with for statement println( ~msg '*** range of numbers ***' ) for i range( ~from 1 ~to 10 ) println( ~msg 'value ' .. i ) end println( ~msg '*** for each key and value in table ***' ) days = { 'sun' : 'Sunday' , 'mon' : 'Monday' , 'tue' : 'Tuesday' , 'wed' : 'Wednesday' , 'thur' : 'Thursday' , 'fri' : 'Friday' , 'sat' : 'Shabbat' } for [ shortname, fullname ] each( ~table days ) println( ~msg shortname .. ' => ' .. fullname ) end println( ~msg '*** for each key and value in table ***' ) for dayval each( ~table days ) println( ~msg dayval[ 1 ] .. ' => ' .. dayval[ 2 ] ) end println( ~msg '*** for each key in table ***' ) for val keys( ~table days ) println( ~msg val ) end println( ~msg '*** for each value in table ***' ) for val values( ~table days ) println( ~msg val ) end
*** range of numbers *** value 1 value 2 value 3 value 4 value 5 value 6 value 7 value 8 value 9 value 10 *** for each key and value in table *** mon => Monday fri => Friday tue => Tuesday thur => Thursday sat => Shabbat sun => Sunday wed => Wednesday *** for each key and value in table *** mon => Monday fri => Friday tue => Tuesday thur => Thursday sat => Shabbat sun => Sunday wed => Wednesday *** for each key in table *** mon fri tue thur sat sun wed *** for each value in table *** Monday Friday Tuesday Thursday Shabbat Sunday WednesdayTrace output for 17-for-lib.p
003|println( ~msg '*** range of numbers ***' )... 005|for ... range( ~from 1 ~to 10 )... 005| for i = 1 006| println( ~msg 'value ' .. i:1 )... 005| end 005| for ... 005| for i = 2 006| println( ~msg 'value ' .. i:2 )... 005| end 005| for ... 005| for i = 3 006| println( ~msg 'value ' .. i:3 )... 005| end 005| for ... 005| for i = 4 006| println( ~msg 'value ' .. i:4 )... 005| end 005| for ... 005| for i = 5 006| println( ~msg 'value ' .. i:5 )... 005| end 005| for ... 005| for i = 6 006| println( ~msg 'value ' .. i:6 )... 005| end 005| for ... 005| for i = 7 006| println( ~msg 'value ' .. i:7 )... 005| end 005| for ... 005| for i = 8 006| println( ~msg 'value ' .. i:8 )... 005| end 005| for ... 005| for i = 9 006| println( ~msg 'value ' .. i:9 )... 005| end 005| for ... 005| for i = 10 006| println( ~msg 'value ' .. i:10 )... 005| end 005| for ... 005| end # finish for loop 009| println( ~msg '*** for each key and value in table ***' )... 011| days = { 'sun' : 'Sunday' , 'mon' : 'Monday' , 'tue' : 'Tuesday' , 'wed' : 'Wednesday' , 'thur' : 'Thursday' , 'fri' : 'Friday' , 'sat' : 'Shabbat' } 021| for ... each( ~table days:{ 'mon' : -> 'Monday', 'fri' : -> 'Friday', 'tue' : -> 'Tuesday', 'thur' : -> 'Thursday', 'sat' : -> 'Shabbat', 'sun' : -> 'Sunday', 'wed' : -> 'Wednesday' } )... 021| for shortname:'mon' , fullname:-> 'Monday'] 022| println( ~msg shortname:'mon' .. ' => ' .. fullname:'Monday' )... 021| end 021| for ... 021| for shortname:'fri' , fullname:-> 'Friday'] 022| println( ~msg shortname:'fri' .. ' => ' .. fullname:'Friday' )... 021| end 021| for ... 021| for shortname:'tue' , fullname:-> 'Tuesday'] 022| println( ~msg shortname:'tue' .. ' => ' .. fullname:'Tuesday' )... 021| end 021| for ... 021| for shortname:'thur' , fullname:-> 'Thursday'] 022| println( ~msg shortname:'thur' .. ' => ' .. fullname:'Thursday' )... 021| end 021| for ... 021| for shortname:'sat' , fullname:-> 'Shabbat'] 022| println( ~msg shortname:'sat' .. ' => ' .. fullname:'Shabbat' )... 021| end 021| for ... 021| for shortname:'sun' , fullname:-> 'Sunday'] 022| println( ~msg shortname:'sun' .. ' => ' .. fullname:'Sunday' )... 021| end 021| for ... 021| for shortname:'wed' , fullname:-> 'Wednesday'] 022| println( ~msg shortname:'wed' .. ' => ' .. fullname:'Wednesday' )... 021| end 021| for ... 021| end # finish for loop 025| println( ~msg '*** for each key and value in table ***' )... 027| for ... each( ~table days:{ 'mon' : -> 'Monday', 'fri' : -> 'Friday', 'tue' : -> 'Tuesday', 'thur' : -> 'Thursday', 'sat' : -> 'Shabbat', 'sun' : -> 'Sunday', 'wed' : -> 'Wednesday' } )... 027| for dayval = [ 'mon', -> 'Monday' ] 028| println( ~msg dayval[1]:'mon' .. ' => ' .. dayval[2]:'Monday' )... 027| end 027| for ... 027| for dayval = [ 'fri', -> 'Friday' ] 028| println( ~msg dayval[1]:'fri' .. ' => ' .. dayval[2]:'Friday' )... 027| end 027| for ... 027| for dayval = [ 'tue', -> 'Tuesday' ] 028| println( ~msg dayval[1]:'tue' .. ' => ' .. dayval[2]:'Tuesday' )... 027| end 027| for ... 027| for dayval = [ 'thur', -> 'Thursday' ] 028| println( ~msg dayval[1]:'thur' .. ' => ' .. dayval[2]:'Thursday' )... 027| end 027| for ... 027| for dayval = [ 'sat', -> 'Shabbat' ] 028| println( ~msg dayval[1]:'sat' .. ' => ' .. dayval[2]:'Shabbat' )... 027| end 027| for ... 027| for dayval = [ 'sun', -> 'Sunday' ] 028| println( ~msg dayval[1]:'sun' .. ' => ' .. dayval[2]:'Sunday' )... 027| end 027| for ... 027| for dayval = [ 'wed', -> 'Wednesday' ] 028| println( ~msg dayval[1]:'wed' .. ' => ' .. dayval[2]:'Wednesday' )... 027| end 027| for ... 027| end # finish for loop 031| println( ~msg '*** for each key in table ***' )... 033| for ... keys( ~table days:{ 'mon' : -> 'Monday', 'fri' : -> 'Friday', 'tue' : -> 'Tuesday', 'thur' : -> 'Thursday', 'sat' : -> 'Shabbat', 'sun' : -> 'Sunday', 'wed' : -> 'Wednesday' } )... 033| for val = 'mon' 034| println( ~msg val:'mon' )... 033| end 033| for ... 033| for val = 'fri' 034| println( ~msg val:'fri' )... 033| end 033| for ... 033| for val = 'tue' 034| println( ~msg val:'tue' )... 033| end 033| for ... 033| for val = 'thur' 034| println( ~msg val:'thur' )... 033| end 033| for ... 033| for val = 'sat' 034| println( ~msg val:'sat' )... 033| end 033| for ... 033| for val = 'sun' 034| println( ~msg val:'sun' )... 033| end 033| for ... 033| for val = 'wed' 034| println( ~msg val:'wed' )... 033| end 033| for ... 033| end # finish for loop 037| println( ~msg '*** for each value in table ***' )... 039| for ... values( ~table days:{ 'mon' : -> 'Monday', 'fri' : -> 'Friday', 'tue' : -> 'Tuesday', 'thur' : -> 'Thursday', 'sat' : -> 'Shabbat', 'sun' : -> 'Sunday', 'wed' : -> 'Wednesday' } )... 039| for val = 'Monday' 040| println( ~msg val:'Monday' )... 039| end 039| for ... 039| for val = 'Friday' 040| println( ~msg val:'Friday' )... 039| end 039| for ... 039| for val = 'Tuesday' 040| println( ~msg val:'Tuesday' )... 039| end 039| for ... 039| for val = 'Thursday' 040| println( ~msg val:'Thursday' )... 039| end 039| for ... 039| for val = 'Shabbat' 040| println( ~msg val:'Shabbat' )... 039| end 039| for ... 039| for val = 'Sunday' 040| println( ~msg val:'Sunday' )... 039| end 039| for ... 039| for val = 'Wednesday' 040| println( ~msg val:'Wednesday' )... 039| end 039| for ... 039| end # finish for loop