| |||||||||||

| |||||||||||

String formatting, conditionals and loops
More Loops and Conditionals example from a Well House Consultants training course
More on More Loops and Conditionals [link]
Source code: g4 Module: P206
=head1 About this demo This example shows string formatting (both input cho(m)ping and output) and the illustrates many of the different conditionals and loops that are available in Perl =cut print "Please tell me the hour "; $hour = <STDIN>; chop $hour; # remove last character print "Please tell me the minute "; $min = <STDIN>; chomp $min; # remove last character is it's a new line # For keyboard input, which always ends in a new line, # chop and chomp will have the same effect on a scalar # HOWEVER # chomp will also work on a list # chop returns the character removes, chomp return NUM of chars removed $mindy = $min + ${hour} * 60 ; print "At the time of $hour:$min, we are ${mindy}mins into the day\n"; print "At the time of $hour:$min, we are $mindy"."mins into the day\n"; print "At the time of $hour:$min, we are $mindy","mins into the day\n"; # {} can be used to scope a variable name $hp = sprintf("%02d",$hour); $mp = sprintf("%02d",$min); print "At the time of $hp:$mp, we are $mindy","mins into the day\n"; printf "At the time of %02d:%02d, we are %dmins into the day\n", $hour, $min, $mindy ; print sprintf "At the time of %02d:%02d, we are %dmins into the day\n", $hour, $min, $mindy ; # ---------------------- Conditionals --------------------- if ($mindy < 350) { print "You are sleeping\n"; } print "You are still sleeping\n" if ($mindy < 350); unless ($mindy >= 350) { print "You are regaining beauty\n"; } print "You are still regaining beauty\n" unless ($mindy >= 350); $mindy < 350 and print ("Zzzzzzzz\n"); $mindy >= 350 or print ("ZZZZZZzz\n"); $mindy < 350 && print ("Zzzzzzzz\n"); $mindy >= 350 || print ("ZZZZZZzz\n"); # difference between and and && $mindy < 350 and $r = $s; $mindy < 350 && ($r = $s); print (($mindy < 350) ? "Snorting\n" : "Awake\n"); $state = (($mindy < 350) ? "Horizontal" : "Vertical"); print "$state\n"; print ("----------------------\n"); print (($mindy < 350) ? "sleeping\n" : ($mindy > 450) ? "working\n" : "commuting\n"); if ($mindy < 350) { print ("You are sleeping\n"); } elsif ($mindy > 450) { print ("You are working\n"); } else { print ("You are commuting\n"); } =head1 This will be coming ... Perl 5.10 and Perl 6 given ($mindy) { print ("sleeping\n") when $_ < 350; print ("working\n") when $_ > 450; print ("commuting\n") ; } =cut #------------------------ Loops -------------------------- while ($mindy < 500) { print "A) We have $mindy mins\n"; $mindy = $mindy + 50; } for ($mx = $mindy; $mx < 750; $mx += 50) { print "B) We have $mx mins\n"; } until ($mx > 1000) { print "C) We have $mx mins\n"; $mx += 50; } foreach $my ($mx .. 1060) { print "D) We have $my mins\n"; } # Cannot use $my following as it's an alias! # next one - poor practise anna: { print "E) We have $mx mins\n"; redo anna if ($mx++ < 1066); } # next one - awful practise!! barbara: print "F) We have $mx mins\n"; goto barbara if ($mx++ < 1070); print "G) We have $mx mins\n" while (++$mx < 1075); print ("H) We have ".$mx++." mins\n") until ($mx > 1085); __END__ Sample output: munchkin:ap1 grahamellis$ perl g4 Please tell me the hour 3 Please tell me the minute 45 At the time of 3:45, we are 225mins into the day At the time of 3:45, we are 225mins into the day At the time of 3:45, we are 225mins into the day At the time of 03:45, we are 225mins into the day At the time of 03:45, we are 225mins into the day At the time of 03:45, we are 225mins into the day You are sleeping You are still sleeping You are regaining beauty You are still regaining beauty Zzzzzzzz ZZZZZZzz Zzzzzzzz ZZZZZZzz Snorting Horizontal ---------------------- sleeping You are sleeping A) We have 225 mins A) We have 275 mins A) We have 325 mins A) We have 375 mins A) We have 425 mins A) We have 475 mins B) We have 525 mins B) We have 575 mins B) We have 625 mins B) We have 675 mins B) We have 725 mins C) We have 775 mins C) We have 825 mins C) We have 875 mins C) We have 925 mins C) We have 975 mins D) We have 1025 mins D) We have 1026 mins D) We have 1027 mins D) We have 1028 mins D) We have 1029 mins D) We have 1030 mins D) We have 1031 mins D) We have 1032 mins D) We have 1033 mins D) We have 1034 mins D) We have 1035 mins D) We have 1036 mins D) We have 1037 mins D) We have 1038 mins D) We have 1039 mins D) We have 1040 mins D) We have 1041 mins D) We have 1042 mins D) We have 1043 mins D) We have 1044 mins D) We have 1045 mins D) We have 1046 mins D) We have 1047 mins D) We have 1048 mins D) We have 1049 mins D) We have 1050 mins D) We have 1051 mins D) We have 1052 mins D) We have 1053 mins D) We have 1054 mins D) We have 1055 mins D) We have 1056 mins D) We have 1057 mins D) We have 1058 mins D) We have 1059 mins D) We have 1060 mins E) We have 1025 mins E) We have 1026 mins E) We have 1027 mins E) We have 1028 mins E) We have 1029 mins E) We have 1030 mins E) We have 1031 mins E) We have 1032 mins E) We have 1033 mins E) We have 1034 mins E) We have 1035 mins E) We have 1036 mins E) We have 1037 mins E) We have 1038 mins E) We have 1039 mins E) We have 1040 mins E) We have 1041 mins E) We have 1042 mins E) We have 1043 mins E) We have 1044 mins E) We have 1045 mins E) We have 1046 mins E) We have 1047 mins E) We have 1048 mins E) We have 1049 mins E) We have 1050 mins E) We have 1051 mins E) We have 1052 mins E) We have 1053 mins E) We have 1054 mins E) We have 1055 mins E) We have 1056 mins E) We have 1057 mins E) We have 1058 mins E) We have 1059 mins E) We have 1060 mins E) We have 1061 mins E) We have 1062 mins E) We have 1063 mins E) We have 1064 mins E) We have 1065 mins E) We have 1066 mins F) We have 1067 mins F) We have 1068 mins F) We have 1069 mins F) We have 1070 mins G) We have 1072 mins G) We have 1073 mins G) We have 1074 mins H) We have 1075 mins H) We have 1076 mins H) We have 1077 mins H) We have 1078 mins H) We have 1079 mins H) We have 1080 mins H) We have 1081 mins H) We have 1082 mins H) We have 1083 mins H) We have 1084 mins H) We have 1085 mins munchkin:ap1 grahamellis$ Learn about this subject
This module and example are covered on the following public courses:
* Perl Programming * Perl bootcamp * Learning to program in Perl * Perl Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering Perl are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "More Loops and Conditionals" training module. You'll find a description of the topic and some
other closely related examples on the "More Loops and Conditionals" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
not to do
things - check in your notes.This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||

PH: 01225 708225 • FAX: 01225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Jan 31 13:04:01 2016 • BUILD SYSTEM: WomanWithCat |