Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use strict;
- use warnings;
- =head1
- Lesson 1
- This stuff contained between =head and =cut is called POD (Plain Old Documentation) and it's used for extended documentation inside of a script.
- It is ignored during compile/run time.
- The first thing you need to learn is about strings, lists, arrays, and scalars.
- =cut
- my $string = 'Michael Fontanesi';
- =head2
- This is a simple string. $string is a variable that contains Michael Fontanesi. You can print this variable.
- =cut
- print $string;
- # outputs Michael Fontanesi
- =head3
- You can change the value of a string very easily.
- =cut
- $string = 'Connor Yates';
- print $string;
- # outputs Connor Yates. Rememeber, it originally was Michael Fontanesi.
- =head4
- You can also combine strings very easily. This is called "concatentation"
- =cut
- my $first = 'Michael';
- my $last = 'Fontanesi';
- my $full = "$first $last";
- print $full;
- # outputs Michael Fontanesi
- $full = $first . $last;
- # periods are frequnetly used in concatentation. It combines $first and $last with no space.
- # outputs MichaelFontanesi
- =head5
- The next thing you need to learn about is lists. It's exactly how it sounds, it's a list of things.
- The important part of lists is that they ARE NOT NAMED.
- =cut
- # ( 'Michael', 'Fontanesi', 'Connor', 'Yates', );
- # this would cause an error if it wasn't commented out. You will see how lists are used later.
- =head6
- The way that lists are normally used is in array context. This is so we have a named variable to we can call.
- =cut
- my @array = ('Michael', 'Fontanesi', 'Connor', 'Yates');
- =head7
- The order of the values in the array is preserved. Arrays have "indexes" so you can refer to specific values within
- the array. This is called an array slice. Since array slices refer to a single value, it is a string. In computer languages,
- the first value is 0 instead of 1
- =cut
- my $array_slice = $array[0];
- # outputs Michael
- $array_slice = $array[1];
- # outputs Fontanesi
- $array_slice = $array[2];
- # outputs Connor
- $array_slice = $array[3];
- # outputs Yates
- =head8
- Arrays are important. Let's do stuff with them. To iterate through each value in an array (OR LIST),
- you use a foreach (or for) loop.
- =cut
- my @names = ('Michael, 'Connor', 'Joey', 'Barton');
- foreach my $name (@names) {
- print "Fuck you $name\n";
- }
- # outputs Fuck you Michael
- # Fuck you Connor
- # Fuck you Joey
- # Fuck you Barton
- # what if we only want to say Fuck you to Barton?
- my @names = ('Michael, 'Connor', 'Joey', 'Barton');
- foreach my $name (@names) {
- print "Fuck you $name\n" if $name eq 'Barton';
- }
- # outputs Fuck you Barton
- =head9
- So that's great and all. In older languages like C, programs are absolutely riddled with foreach and if loops and it gets
- very hard to read. Modern languages make working with lists and arrays easier with "list operators". The most important ones
- are map, grep, and sort.
- map iterates through each value of an array or list and does whatever operation you tell it to.
- grep is the comparison operator and will filter a list or array with your given parameters.
- sort does exactly what you think. You can specify how you want things sorted (i.e. ascending, descending).
- Let's use these
- =cut
- my @animals = ('DOG', 'CAT', 'BAT', 'BUFFALO', 'ELEPHANT', 'TIGER');
- # let's make all of these lowercase
- my @lowercase = map { lc($_) } @animals;
- # @lowercase now contains dog, cat, bat, buffalo, elephant, tiger
- # the $_ is a special variable in perl meaning "the current value" so we don't have to make a separate string variable
- # like we did with foreach loops
- # You also don't need to make a new array variable! You can modify the values in the original array
- @animals = map { lc($_) } @animals;
- # Now, what if we don't want all the values in this array? Let's filter it with grep.
- @animals = ('DOG', 'CAT', 'BAT', 'BUFFALO', 'ELEPHANT', 'TIGER');
- @animals = grep { /BAT/ } @animals;
- # @animals only consists of the value BAT now. We can also do
- @animals = grep { !/BAT/ } @animals;
- # ! is negation, or "not" like in "not equal to", so now @animals contains everything except BAT
- # What if we want these in alphabetical order? We use sort. sort has special variables $a and $b which you use to specify
- # the order you want
- @animals= sort { $a <=> $b } @animals;
- # sorts it in alphabetical order
- @animals= sort { $b <=> $a } @animals;
- # sorts it in reverse alphabetical order
- =head9
- Imagine doing all of these operations with foreach and if loops. It would get very, very messy. List operators are very
- powerful tools in modern programming languages and you can accomplish complicated tasks by using them in smart ways.
- Let's combine these list operators to do all of the work we just ^ did in a single line.
- =cut
- @animals = ('DOG', 'CAT', 'BAT', 'BUFFALO', 'ELEPHANT', 'TIGER');
- @animals = sort { $a <=> $b } map { lc($_) } grep { !/BAT/ } @animals;
- # you read list operators from right to left. This first filters out BAT from the array with grep
- # then converts all values to lowercase with map
- # and then sorts all of the values in alphabetical order
- =head10
- map, grep, and sort are extremely important. Your first practice assignment is to write me some code that uses map, grep, and
- sort. Master them. Start thinking in terms of list operators instead foreach loops.
- END OF LESSON 1
- =cut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement