Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4.  
  5. =head1
  6.  
  7. Lesson 1
  8.  
  9. This stuff contained between =head and =cut is called POD (Plain Old Documentation) and it's used for extended documentation inside of a script.
  10. It is ignored during compile/run time.
  11.  
  12. The first thing you need to learn is about strings, lists, arrays, and scalars.
  13.  
  14. =cut
  15.  
  16. my $string = 'Michael Fontanesi';
  17.  
  18. =head2
  19.  
  20. This is a simple string. $string is a variable that contains Michael Fontanesi. You can print this variable.
  21.  
  22. =cut
  23.  
  24. print $string;
  25. # outputs Michael Fontanesi
  26.  
  27. =head3
  28.  
  29. You can change the value of a string very easily.
  30.  
  31. =cut
  32.  
  33. $string = 'Connor Yates';
  34. print $string;
  35. # outputs Connor Yates. Rememeber, it originally was Michael Fontanesi.
  36.  
  37. =head4
  38.  
  39. You can also combine strings very easily. This is called "concatentation"
  40.  
  41. =cut
  42.  
  43. my $first = 'Michael';
  44. my $last = 'Fontanesi';
  45. my $full = "$first $last";
  46. print $full;
  47. # outputs Michael Fontanesi
  48.  
  49. $full = $first . $last;
  50. # periods are frequnetly used in concatentation. It combines $first and $last with no space.
  51. # outputs MichaelFontanesi
  52.  
  53. =head5
  54.  
  55. The next thing you need to learn about is lists. It's exactly how it sounds, it's a list of things.
  56. The important part of lists is that they ARE NOT NAMED.
  57.  
  58. =cut
  59.  
  60. # ( 'Michael', 'Fontanesi', 'Connor', 'Yates', );
  61. # this would cause an error if it wasn't commented out. You will see how lists are used later.
  62.  
  63. =head6
  64.  
  65. The way that lists are normally used is in array context. This is so we have a named variable to we can call.
  66.  
  67. =cut
  68.  
  69. my @array = ('Michael', 'Fontanesi', 'Connor', 'Yates');
  70.  
  71. =head7
  72.  
  73. The order of the values in the array is preserved. Arrays have "indexes" so you can refer to specific values within
  74. the array. This is called an array slice. Since array slices refer to a single value, it is a string. In computer languages,
  75. the first value is 0 instead of 1
  76.  
  77. =cut
  78.  
  79. my $array_slice = $array[0];
  80. # outputs Michael
  81. $array_slice = $array[1];
  82. # outputs Fontanesi
  83. $array_slice = $array[2];
  84. # outputs Connor
  85. $array_slice = $array[3];
  86. # outputs Yates
  87.  
  88. =head8
  89.  
  90. Arrays are important. Let's do stuff with them. To iterate through each value in an array (OR LIST),
  91. you use a foreach (or for) loop.
  92.  
  93. =cut
  94.  
  95. my @names = ('Michael, 'Connor', 'Joey', 'Barton');
  96. foreach my $name (@names) {
  97. print "Fuck you $name\n";
  98. }
  99. # outputs Fuck you Michael
  100. # Fuck you Connor
  101. # Fuck you Joey
  102. # Fuck you Barton
  103.  
  104. # what if we only want to say Fuck you to Barton?
  105.  
  106. my @names = ('Michael, 'Connor', 'Joey', 'Barton');
  107. foreach my $name (@names) {
  108. print "Fuck you $name\n" if $name eq 'Barton';
  109. }
  110. # outputs Fuck you Barton
  111.  
  112. =head9
  113.  
  114. So that's great and all. In older languages like C, programs are absolutely riddled with foreach and if loops and it gets
  115. very hard to read. Modern languages make working with lists and arrays easier with "list operators". The most important ones
  116. are map, grep, and sort.
  117.  
  118. map iterates through each value of an array or list and does whatever operation you tell it to.
  119. grep is the comparison operator and will filter a list or array with your given parameters.
  120. sort does exactly what you think. You can specify how you want things sorted (i.e. ascending, descending).
  121.  
  122. Let's use these
  123.  
  124. =cut
  125.  
  126. my @animals = ('DOG', 'CAT', 'BAT', 'BUFFALO', 'ELEPHANT', 'TIGER');
  127. # let's make all of these lowercase
  128.  
  129. my @lowercase = map { lc($_) } @animals;
  130. # @lowercase now contains dog, cat, bat, buffalo, elephant, tiger
  131. # the $_ is a special variable in perl meaning "the current value" so we don't have to make a separate string variable
  132. # like we did with foreach loops
  133.  
  134. # You also don't need to make a new array variable! You can modify the values in the original array
  135.  
  136. @animals = map { lc($_) } @animals;
  137.  
  138. # Now, what if we don't want all the values in this array? Let's filter it with grep.
  139. @animals = ('DOG', 'CAT', 'BAT', 'BUFFALO', 'ELEPHANT', 'TIGER');
  140. @animals = grep { /BAT/ } @animals;
  141. # @animals only consists of the value BAT now. We can also do
  142. @animals = grep { !/BAT/ } @animals;
  143. # ! is negation, or "not" like in "not equal to", so now @animals contains everything except BAT
  144.  
  145. # What if we want these in alphabetical order? We use sort. sort has special variables $a and $b which you use to specify
  146. # the order you want
  147.  
  148. @animals= sort { $a <=> $b } @animals;
  149. # sorts it in alphabetical order
  150.  
  151. @animals= sort { $b <=> $a } @animals;
  152. # sorts it in reverse alphabetical order
  153.  
  154. =head9
  155.  
  156. Imagine doing all of these operations with foreach and if loops. It would get very, very messy. List operators are very
  157. powerful tools in modern programming languages and you can accomplish complicated tasks by using them in smart ways.
  158.  
  159. Let's combine these list operators to do all of the work we just ^ did in a single line.
  160.  
  161. =cut
  162.  
  163. @animals = ('DOG', 'CAT', 'BAT', 'BUFFALO', 'ELEPHANT', 'TIGER');
  164. @animals = sort { $a <=> $b } map { lc($_) } grep { !/BAT/ } @animals;
  165. # you read list operators from right to left. This first filters out BAT from the array with grep
  166. # then converts all values to lowercase with map
  167. # and then sorts all of the values in alphabetical order
  168.  
  169. =head10
  170.  
  171. map, grep, and sort are extremely important. Your first practice assignment is to write me some code that uses map, grep, and
  172. sort. Master them. Start thinking in terms of list operators instead foreach loops.
  173.  
  174. END OF LESSON 1
  175.  
  176. =cut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement