We Recommend

Mastering Regular Expressions Mastering Regular Expressions
As this book shows, a command of regular expressions is an invaluable skill. Regular expressions allow you to code complex and subtle text processing that you never imagined could be automated. Regular expressions can save you time and aggravation. They can be used to craft elegant solutions to a wide range of problems. Once you've mastered regular expressions, they'll become an invaluable part of your toolkit. You will wonder how you ever got by without them.


Posted By

hansamann on 04/17/07


Tagged

Switch groovyseries for if else else-if while


Versions (?)


Who likes this?

5 people have marked this snippet as a favorite

jsbournival
bootz15
hendersk
mbanfi
adaptives


Groovy Series: Groovy Control Structures


Published in: Groovy 


URL: http://hansamann.podspot.de/files/grails_podcast_episode_40.mp3

This snippet gives you some interesting examples about Groovy Control Structures. Listen to the audio to get the best learning experience. Simply right-click on the title above to download the mp3 file of this part of the series.

The Groovy Series is part of the Grails Podcast and can be subscribed to via: http://hansamann.podspot.de/rss. The series is produced by Dierk König and Sven Haiges, further information about the topic of this episode can be found in the book

Groovy in Action - http://groovy.canoo.com/gina

  1. //if / else if / else
  2. def x = 1
  3.  
  4.  
  5. //conditional ?:
  6. def obj = new Object()
  7. def txt = obj ? 'true' : 'false'
  8. assert txt == 'true'
  9.  
  10. def myList = []
  11. assert 'empty' == (myList ? 'not empty' : 'empty')
  12.  
  13. //powerful switch statement, isCase() :-)
  14. def someValue = 5
  15. def feedback = ''
  16. switch (someValue) {
  17. case 1..3 : feedback = 'quite ok'; break
  18. case 3..6 : feedback = 'needs improvement'; break
  19. default: feedback = 'unknown'
  20. }
  21. assert (feedback == 'needs improvement')
  22.  
  23. //java.lang.Class contains an isCase() method, we can classify by class
  24. someValue = 5.5
  25. feedback = ''
  26. switch (someValue) {
  27. case Float: feedback = /It's a Float/; break
  28. case Integer: feedback = /It's an Integer/; break
  29. case BigDecimal: feedback = /It's a BigDecimal/; break
  30. default: feedback = 'You are using an unknown number type!'
  31. }
  32. assert (feedback == /It's a BigDecimal/)
  33.  
  34.  
  35. //switching on Lists
  36. someValue = 4
  37. feedback = ''
  38. switch (someValue) {
  39. case [1,2,3]: feedback = 'One, Two or Three' ; break
  40. case [4,5,6]: feedback = 'Four, Five or Six'; break;
  41. }
  42. assert (feedback == 'Four, Five or Six')
  43.  
  44. //matcher with switch
  45. someValue = 'Hello World'
  46. feedback = ''
  47. switch (someValue) {
  48. case ~/Guten Tag/: feedback = 'Guten Tag'; break;
  49. case ~/^Hello.*/: feedback = 'Starts with Hello'
  50. }
  51. assert (feedback == 'Starts with Hello')
  52.  
  53. //for
  54. //pretty standard java5 for loop in groovy
  55. def myList = ['groovy',' ', 'is', ' ', 'cool']
  56. def result = ''
  57. for (item in myList) {
  58. result += item
  59. }
  60. assert (result == 'groovy is cool')
  61.  
  62.  
  63. //using ranges in for loops
  64. result = new StringBuffer()
  65. for (character in 'a'..'f') {
  66. result << character
  67. }
  68. assert (result.toString() == 'abcdef')
  69.  
  70. //using for with a matcher
  71. def times = []
  72. def text = 'Get up at 8:00, have coffee at 8:15 and go to work around 11:00'
  73. def timeMatcher = text =~ /\d{1,2}:\d\d/
  74. for (match in timeMatcher) times << match
  75. assert (times.join(',') == '8:00,8:15,11:00')
  76.  
  77. //classic for loops will come with groovy 1.1 :-)

Report this snippet 

You need to login to post a comment.