190 likes | 298 Views
Perl Day 2. By Gabe and Ted. Review. Integers $tickets= 34; Strings $name= “Bob”; Arrays @array = (8, 2, 13, 9, 27); Hashes %Dogs = (Tiger => “Border Collie”, Bart => “Pit bull” , Bozo => “Chow” , Missie => “Golden Retriever”);. Review. For input use <STDIN>.
E N D
Perl Day 2 By Gabe and Ted
Review • Integers • $tickets= 34; • Strings • $name= “Bob”; Arrays • @array = (8, 2, 13, 9, 27); Hashes • %Dogs = (Tiger => “Border Collie”, Bart => “Pit bull” , Bozo => “Chow” , Missie => “Golden Retriever”);
Review • For input use <STDIN>. • Chomp removes last character if is it a new line character. • Chop removes last character regardless of what character it is. • Do while, do until • Test is done after the action.
Characteristics • Simplicity- Easy to learn and use. Syntax is like the human language. • Portability- Very Portable. It is available for Linux, Mac OS X, Windows, Unix. • Writability- Easy for humans to write and read. It is similar to English. • Orthogonality- Designed to be a non-orthogonal language. • Reliability- Perl 5 had some problems and was not very reliable but with the new version of Perl they are fixing these errors. • Abstraction- Perl is seen as a high-level language and does not worry about low-level details
my() Function • We use the use strict statement to check that we have declared all our variables in a function. • If the use strict statement is used then my function must be used. • The my() function is used to create local variables in a function. It is used for scalars, arrays, and hashes.
Arrays/Array Functions • Adding elements to an array @array1 = (5, 6, 7); @array2; @array2 = (@array1, 1, 2, 3, 4); print “@array2\n”; • reverse()-Returns an array in reverse order. @names = qw(Alex Doug Nick Joe); @names = reverse(@names); print"(@names) \n"; = (Joe Nick Doug Alex) • push()- Adds a single or list of elements to the end of an array. @sports = qw(football baseball soccer basketball); push @sports, hockey; print "(@sports)\n"; = (football baseball soccer basketball hockey)
Array Functions Cont • pop()- Removes the top element which is the element with the highest index. @grades = qw(A B C D F); pop @grades; print “(@grades)\n”= (A B C D) • shift ()-Removes first element at the beginning of the array. • unshift()-Moves an element to the beginning of the array. @store = (); unshift @store, "Macys"; print "(@store)\n"; = (Macys) unshift @store,"Target","Kohls","Champs"; print "(@store)\n"; = (Target Kohls Champs Macys) shift @store; print "(@store)\n"; = (Kohls Champs Macys) • sort()- Takes a list of only strings or letters and returns a sorted version of this array. @unsorted = qw(Winter Summer Fall Spring); @sorted = sort @unsorted; print "Sorted: @sorted\n";
Subroutines/Functions • Subroutines are defined with the Sub Keyword and invoked by naming the function along with parentheses. • Arguments go inside the parentheses and they may be scalars, lists, or hashes. If the function defined before it is invoked no parentheses are needed. • Arguments in functions are passed by reference not by value. This means that we declare the function parameters as references rather than normal variables. @product = product(1, 2, 3, 4, 5); sub product { $product = 1; $product *= $_ foreach @_; print $product; } product (); or sub product;
Scope • Perl has global variables and lexical variables. Perl function variables are global variables by default. • Global variables- Can be accessed anywhere in the program $a= 13; fun_1(); sub fun_1 { print “value of \$a is: $a\n”; } • Lexical Variables/Local Variables- must be declared with my ( ). These variables have local scope which means they exist from the point where they were declared until the end of the enclosing block. $a = 13; fun_2(); sub fun_2{ my $a=31; print "The value of the local variable is: $a \n"; } print "The value of the global variable is: $a \n";
Regular Expressions • Regular expressions are used to search text easily and quickly. Regular expressions always take the form of /foo/. • Substitutions- substitute words • $test =~ s/foo/bar/; • Translations- substitute letters • $string =~ tr/[a-z]/[A-Z]/; • Matching- true/false • $string =~ m/foobar/;
Regular Expressions Cont • Split() function- Breaks up a string into a list of words • ($os1,$os2,$os3) = split(/,/, “windows,linux,mac”); • Join() function- Opposite of split, takes the elements of an array and puts the first part between the array elements. • @array = (“perl”,”java”,”C++”); • print join(“\n”,@array);
References • In Perl, a reference is always a scalar but can talk about the data stored in an array or hash. • Unlike C++, a reference in Perl only stores memory locations for specific, clearly defined data structures. • A backslash in front of the variable is all you need to create a reference if you already have the data in an existing variable. • @array =(5, 4, 3, 2, 1); • $array_r = \@array; • $number = 32; • $number_r = \$number;
References Cont • Anonymous Arrays • $array_r = [1, 2, 3, 4, 5]; • To dereference data put the reference in curly braces. • $array_r = [1, 2, 3, 4, 5]; • @array2 = @{$array_r};
String Functions • Length() function- Determines the length of a string. • length(string) $name= ‘Daniel’; print ‘length of $name is: ‘, length ($name), “\n”; Index() function- Locates substrings in strings. • index(string, substring); $my = index('Merry Christmas','Christmas'); print "the substring was found at index: $my\n"; $this index(‘Happy Hanukkah’, ‘Ha’, 5); print “the substring was found at index: $this\n”; rindex() function- Similar to index(), it searches the string from right to left. substr() function- Extracts fields out of a string if a string follows a specific column layout. • Substr(string, starting_index, length)
Object oriented Perl • Attribute- Something we know about an object such as a name or birthday. • Methods- Anything you can tell the object to do. They are subroutines but also need the arrow operator to call methods. • Constructors- A class method that all classes should possess. It constructs and returns a new object. • Destructors- When an object is no longer in use, Perl automatically destroys it. Perl will attempt to call a method name DESTROY(). • DESTROY() function- will destroy data and object when called. • Bless() function- takes a reference and turns it into an object
OOP Example • Attribute • My $person = { lastname => “Smith”, firstname => “John”, address => “123 Main St”, }; • Method • sub returnname { • print lastname; • }
Calling an object’s method • To call an object’s method you use an arrow “->”. • $person->returnname;
Practice Problems • Write a function that takes has an argument and prints out the sum of the argument +2. • Create an array with a list of strings and print out the array in reverse order.
Homework Problems • Sort this list (“Ted”, “Drew”, “Krista”, “Gabe”, “Forrest”, “Michael”, “Brett”, “Matt”) into alphabetical order. • Write a function that takes in input and prints out the prime factors of the input. • Write a function that takes in a string and a substring as input and determine if the substring can be found in the function. If it is found print out the index where the substring starts and if not found print that it was not found.