1 / 35

CIT 383: Administrative Scripting

Learn about boolean expressions, conditionals, loops, and arrays in administrative scripting. Understand how to use comparison and boolean operators effectively for efficient scripting.

Download Presentation

CIT 383: Administrative Scripting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIT 383: Administrative Scripting Flow Control and Arrays CIT 383: Administrative Scripting

  2. CIT 383: Administrative Scripting Topics • Boolean Expressions • Conditionals • While Loops • Creating Arrays • Indexing Arrays • Array Methods • Iterators

  3. CIT 383: Administrative Scripting What are true and false? false is • false, the one member of the FalseClass • nil, the one member of NilClass true is • true, the one member of the TrueClass • anything that’s not false, including 0, “”, NaN,...

  4. CIT 383: Administrative Scripting Comparison Operators Binary operators that return true or false. == equals != not equals > greater than >= greater than or equal to < less than <= less than or equal to

  5. CIT 383: Administrative Scripting Boolean Operators Operate on Boolean values. ! not not not (lower precedence) && and and and (lower precedence) || or or or (lower precedence)

  6. CIT 383: Administrative Scripting Boolean Expressions x == 0 y > 1 z <= 5 x == 0 && y > 1 x == 0 && y > 1 || z <= 5 is_x_zero = x == 0 big_y = y > 2**16 is_x_zero && big_y

  7. CIT 383: Administrative Scripting Conditionals • if statement • if statement with else clause • elsif • if expressions • unless statement • post-conditionals • case statement

  8. CIT 383: Administrative Scripting false false expression x%2 == 0 true true expr expr code color = hilight if statement if expression code end • if x % 2 == 0 • color = hilight • end

  9. CIT 383: Administrative Scripting Differences from Java Parentheses not required around expression • expression begins after if and space • expression ends with newline If statement must be terminated by an end • There is no punctuation like Java’s braces.

  10. CIT 383: Administrative Scripting if statement with else if expression code1 else code2 end false true expression code1 code2

  11. CIT 383: Administrative Scripting Multiple conditionals with elsif if expression1 code1 elsif expression2 code2 . . . elsif expressionN codeN else code end if x == 1 name = “one” elsif x == 2 name = “two” elsif x == 3 name = “three” elsif x == 4 name = “four” else name = “lots” end

  12. CIT 383: Administrative Scripting if expressions Ruby has no statements, only expressions. • Everything returns a value, even if it’s nil. • if returns the value of the last expression evaluated. Example: name = if x == 1 then “one” elsif x == 2 then “two” elsif x == 3 then “three” elsif x == 4 then “four” else “many” end

  13. CIT 383: Administrative Scripting unless statement unless expression code end true expression false expr code • unless expression • code1 • else • code2 • end

  14. CIT 383: Administrative Scripting Post-conditionals Pre-conditional form if expression then code end Equivalent post-conditional form code if expression Examples puts message if debug > 1 puts message if !quiet && verbose

  15. CIT 383: Administrative Scripting Case statement name = case when x == 1 then “one” when x == 1 then “one” when x == 1 then “one” when x == 1 then “one” else “many” end Case statement Equivalent if statement name = if x == 1 then “one” elsif x == 2 then “two” elsif x == 3 then “three” elsif x == 4 then “four” else “many” end

  16. CIT 383: Administrative Scripting Alternate case statement format name = case x when 1 then “one” when 2 then “two” when 3 then “three” when 4 then “four” else “many” end

  17. CIT 383: Administrative Scripting x = 0 false x < 10 true expr puts x x = x + 1 while loop while expression code end false expression true expr code x = 0 while x < 10 puts x x = x + 1 end

  18. CIT 383: Administrative Scripting until loop until expression code end true expression false expr code x = 0 until x > 10 puts x x = x + 1 end x = 0 true x > 10 false expr puts x x = x + 1

  19. CIT 383: Administrative Scripting Arrays in Ruby a = [10, 20, 30, 40, 50, 60] a[0] 10 20 a[1] 30 a[2] a[3] 40 a[4] 50 a[5] 60 a[-1]

  20. CIT 383: Administrative Scripting Arrays of Strings Define the same way as arrays of integers a = [‘spam’, ‘and’, ‘eggs’, ‘for’, ‘breakfast’] Alternative syntax for strings w/o spaces: a = %w[spam and eggs for breakfast] a = %w|spam and eggs for breakfast| %W works like double quoted strings a = [“\t”, “\n”, “\r”] a = %W(\t \n \r)

  21. CIT 383: Administrative Scripting Creating Empty Arrays Use the [] notation a = [] Create like any other object a = Array.new Similar to str = “” str = String.new

  22. CIT 383: Administrative Scripting Arrays of Different Types Ruby arrays can have data of different types a=[1,2.718,‘a string’,“\t”,[3,4]] Use like any other array a[0] == 1 a[1] == 2.718 a[2] == ‘a string’ a[3] == “\t” a[4] == [3, 4]

  23. CIT 383: Administrative Scripting Indexing Arrays Single indexes a[n] is nth element from beginning starting at 0 a[-n] is nth element from end starting at -1 Double indexes (slicing) a[0,1] == a[0] a[0,2] == [ a[0], a[1] ] a[1,3] == [ a[1], a[2], a[3] ] a[-2, 2] == [ a[-2], a[-1] ]

  24. CIT 383: Administrative Scripting Index Assignments Single index a[n] = 5 sets nth element to be 5 If a[n] isn’t already defined, extends array to be long enough to have an nth element. Double index a[0,2] = [10,20] sets first two elements to 10,20 a[0,0] = [1,2,3] inserts 3 elements at beginning a[0,2] = [] deletes first two elements of array

  25. CIT 383: Administrative Scripting Array Operations Concatenation a = [1,2,3] + [4,5] # [1,2,3,4,5] a = a + [6,7,8] # [1,2,3,4,5,6,7,8] Subtraction [1,2,3,4,5,6] – [2,3,4] # [1,5,6] Append a = [] a << 1 # [1] a << 2 << 3 # [1, 2,3] a << [4,5,6] # [1,2,3,[4,5,6]]

  26. CIT 383: Administrative Scripting Array Operations Union: | Concatenates then removes duplicates. Intersection: & Array of elements in both arrays, no duplicates. Examples • a = [1,1,2,2,3,3,4] • b = [5,5,4,4,3,3,2] • a | b # [1,2,3,4,5] • b | a # [5,4,3,2,1] • a & b # [2,3,4] • b & a # [4,3,2]

  27. CIT 383: Administrative Scripting Array Methods reverse Reverses order of array elements. sort Sorts array elements from lowest to highest All array elements must be of same type. sort! Previous methods return altered array. Methods ending in ! change array in place.

  28. CIT 383: Administrative Scripting Using Arrays w/o Indexing We often need just a list of items to: • Add items from the list. • Remove items from the list. • Perform an action for each item of the list. We don’t need to deal with indices since • The 5th item isn’t special. • We just want to add, remove, or do something for each item of the list. • Leaves only one choice: add and remove from • End of array (push and pop) • Beginning of array (shift and unshift)

  29. CIT 383: Administrative Scripting 10 20 30 10 20 30 40 Push and pop push adds an element to end of array. a[0] a[0] a.push(40) a[-1] a[-1]

  30. CIT 383: Administrative Scripting 10 20 30 10 20 30 40 Push and pop pop removes an element from end of array. a[0] a[0] x = a.pop a[-1] a[-1] x == 40

  31. CIT 383: Administrative Scripting 10 20 30 5 10 20 30 Shift and unshift unshift adds an element to start of array. a[0] a[0] a.unshift(5) a[-1] a[-1]

  32. CIT 383: Administrative Scripting 5 10 20 10 20 30 30 Shift and unshift shift removes an element f/ start of array. a[0] a[0] x = a.shift a[-1] a[-1] x == 5

  33. CIT 383: Administrative Scripting The each iterator Each executes code for each item of array • Assigns current element to |variable| • Executes block of code. • Sets current element to next element. Example: langs = %w(python ruby scheme) langs.each do |language| puts “#{language} is good.” end

  34. CIT 383: Administrative Scripting The each iterator array.each do |var| code end n = 0 var = array[n] code is n last element? false n = n + 1 true

  35. CIT 383: Administrative Scripting References • Michael Fitzgerald, Learning Ruby, O’Reilly, 2008. • David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. • Hal Fulton, The Ruby Way, 2nd edition, Addison-Wesley, 2007. • Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2nd edition, Pragmatic Programmers, 2005.

More Related