IT 441: Network Services Administration
Class #04 Exercise

Complete by: 2/15/2018


Log in to users.cs.umb.edu

Practice with numeric comparisons

perl>  print 3 == 3 ;

perl>  print 349 == 5 ;

perl>  print 0+ (349 == 5) ;

perl>  print 349 > 5 ;

perl>  $float1 = 9.9997 ; 

perl>  $float2 = 9.9995 ; 

perl>  print $float1 == $float2 ; 

perl>  print 0+ ($float1 == $float2) ;

perl>  print abs($float1 - $float2) < 0.001 ;
(What do you think the 0+ part does?)
(What do you think the expression abs($float1 - $float2) < 0.001 is doing for us here?)

Practice with string comparisons

perl>  print 'hello' eq 'hello' ;

perl>  print 'hello' eq 'Hello' ;

perl>  print 0+ ('hello' eq 'Hello') ;

perl>  print 'hello' ne 'Hello' ;

perl>  print 'hello' gt 'Hello' ;

perl>  print 0+ ('hello' lt 'Hello') ;

perl>  print 'hello' ge 'Hello' ;

Practice with boolean operators

perl>  print 3 == 3 && 'hello' eq 'hello' ;

perl>  print 3 == 3 && 'hello' eq 'Hello' ;

perl>  print 0+ (3 == 3 && 'hello' eq 'Hello') ;

perl>  print 3 == 3 || 'hello' eq 'Hello' ;

perl>  print 0+ ('hello' eq 'Hello' || 3 == 4) ;

perl>  print ! ('hello' eq 'Hello' || 3 == 4) ;

perl>  print 0+ (! 'hello' gt 'Hello') ;

Practice with true/false values in Perl

perl>  if (1) { print "true" ; } else { print "false" ; }

perl>  if (100) { print "true" ; } else { print "false" ; }

perl>  if (-100) { print "true" ; } else { print "false" ; }

perl>  if (-1) { print "true" ; } else { print "false" ; }

perl>  if (0) { print "true" ; } else { print "false" ; }

perl>  if (0.0) { print "true" ; } else { print "false" ; }

perl>  if ("0") { print "true" ; } else { print "false" ; }

perl>  if ("00") { print "true" ; } else { print "false" ; }

perl>  if ('Red Sox') { print "true" ; } else { print "false" ; }

perl>  if ('') { print "true" ; } else { print "false" ; }

perl>  if (' ') { print "true" ; } else { print "false" ; }

perl>  if ('true') { print "true" ; } else { print "false" ; }

perl>  if ('false') { print "true" ; } else { print "false" ; }

perl>  if (undef) { print "true" ; } else { print "false" ; }

Practice with user input

perl>  print "Enter a whole number: " ; $user_val = <> ;

perl>  print "You entered: " . $user_val ;

perl>  print "Multiplied by 2: " . $user_val * 2 ;

perl>  print "Indeed, $user_val was the number that you entered!" ;

perl>  chomp($user_val) ;

perl>  print "Indeed, $user_val was the number that you entered!" ;

perl>  print "Enter a floating-point (decimal) number: " ; $user_dec1 = <> ;

perl>  print "Enter a floating-point (decimal) number: " ; $user_dec2 = <> ;

perl>  if (abs($user_dec1 - $user_dec2) < 0.0001) { print "They are (practically) equal." ; } else { print "They are sufficiently different." ; }

perl>  print "Enter a string: " ; $user_string = <> ;

perl>  chomp($user_string) ;

perl>  print "Your string repeated 5 times: " . $user_string x 5 ; 
(Why are we using the chomp function here?)

End the Perl Session

perl>  exit

End the script session

Use the exit command. You should then have a file called typescript in your current working directory.