Lec-02-BWN - 9/1/2009 Lecture 02 ECE364: Software Tools...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
9/1/2009 1 ECE364: Software Tools Laboratory Lecture 2 August 31, 2009 1 Lecture 02 Indexed arrays Additional KornShell functionality Basic regular expressions Quotes Shell commands Case statement 2 Arrays in ksh An array can be declared with: Arr=(1 2 3 4 foo) set -A Arr 1 2 3 4 foo You can access an array like so: $ print ${Arr[2]} 3 $ print ${Arr[*]} 1 2 3 4 foo You can assign individual elements too (the index will be added if it doesn't exist): Arr[5]="hi there" 3 Arrays in ksh Unlike C, ksh supports sparse arrays Sparse[5]=8 Sparse[15]=12 Sparse[19]=7 If the indices aren't consecutive, how do we know the array's size? How do we know the indices for all values? 4 Special operators # and ! You can obtain a list (a string of whitespace- separated values) of every element in an array: print ${Array[*]} print ${Array[@]} The size of an array can be found by using the # operator: ${#Array[*]} or ${#Array[@]} The array subscripts can be found by using the ! operator: ${!Array[*]} or ${!Array[@]} 5 Indexed array example #! /bin/ksh A[5]=34 A[1]=3 A[2]=56 A[100]=89 print "Size of array: ${#A[*]}" print "Array indices: ${!A[*]}" for I in ${!A[*]} do print "A[${I}]=${A[I]}" done exit 0 6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
9/1/2009 2 Indexed array output Size of array: 4 Array indices: 1 2 5 100 A[1]=3 A[2]=56 A[5]=34 A[100]=89 7 Read array example #! /bin/ksh print "Data_File:" cat Data_File print print "Formatted output:" while read -A Data # Split on whitespace do # (spaces and tabs) for Item in ${Data[*]} do printf "%6.2f\n" $Item done done < Data_File 8 Output Data_File: 1 2 3 77 12 12.6 6.8 7 2 1.0 -3 -5.5 Formatted output 1.00 2.00 3.00 77.00 12.00 12.60 6.80 7.00 2.00 1.00 -3.00 -5.50 9 Binary operators You remember our friends from ECE 264, right? << n Shift left n bits >> n Shift right n bits Bitwise AND ^ Bitwise EXCLUSIVE OR | Bitwise OR ~ Bitwise negation 10 Binary operations #! /bin/ksh integer A=2#1101 integer B=2#0110 integer C (( C = A & B )) ((C A|B) ) (( C = A | B )) print "(( C = $A | $B )) C = $C" (( C = A ^ B )) print "(( C = $A ^ $B )) C = $C" exit 0 Results (the values actually displayed are in base 10): (( C = 2#1101 | 2#110 )) C = 2#1111 (( C = 2#1101 ^ 2#110 )) C = 2#1011 11 More binary operations #! /bin/ksh integer A=2#1101 integer B=2#0110 integer C (( C = A << 2 )) print "(( C = $A << 2 )) C = $C" (( C =B> > 1 )) (( C B >> 1 )) print "(( C = $B >> 2 )) C = $C" (( C = ~B )) print "(( C = ~$B )) C = $C" exit 0 Results in: (( C = 2#1101 << 2 )) C = 2#110100 (( C = 2#110 >> 1 )) C = 2#11
Background image of page 2
9/1/2009 3 Scientific functions All angles are in radians abs Absolute value acos Arc cosine asin Arc sine atan Arc tangent cos Cosine cosh Hyperbolic cosine exp Exponential with base e int Greatest integer <= to value of expression (floor)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/01/2009 for the course PSY 120 taught by Professor Donnely during the Spring '08 term at Purdue University-West Lafayette.

Page1 / 11

Lec-02-BWN - 9/1/2009 Lecture 02 ECE364: Software Tools...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online