1、6) Hashes Ma* Webster IMBIM, BMC ma*hew.webster@imbim.uu.se Reminder: scalars !•variables in perl (e.g.$example) can be numbers or strings !•perl converts variable between the two depending on the operaDon •backslash used to indicate special characters like newline and tab t!•c
2、ompare numbers or strings with comparison operators andif!• gets input from the keyboard •chompremoves!•whilerepeats a chunk of code while an expression is true Reminder: arrays $array[0]!•first element of @array$#array!•is the final index of@array!qw//!•quoted by whitespace p
3、ushpop!•add or remove elements from the end shiftunshift!•add or remove elements from the start splice!•add, remove, replace elements from the middle foreach!•cycle through every element sortreverse!•change order can also be read into an array reminder: subrouDnes •subrouDnes
4、–called by &sub_example or sub_example()!–defined using sub{}!–use return or it will return last evaluated value –arguments passed within: @_!$_[0],$_[1],$_[2] .... •my is used to restrict a variable to a block of code •usestrict means all variables must be declared using my!reminder:
5、 input and output while(){}!•to read from standard input <>!•to process invocaDon commands and read in files @ARGV!•contains the invocaDon commands (useful) printf!•formats scalars (e.g. decimal points on numbers) openFH,"input.txt";!•opens a filehandle for reading openFH,">outp
6、ut.txt";!•opens a filehandle to write (>> to append) while(){}!•reads each line of a filehandledie!•your script dies! scalar variables, arrays and hashes •A scalar variable contains a single string or number: $month="October";!$month=10;!scalar variables, arrays and hashes •An arra
7、y contains a list of values with a numbered index @students=qw/DoreenOskarElinSangeetMalin/;!$students[4]Malin!$students[3]Sangeet!$students[2]Elin!$students[1]Oskar!$students[0]Doreen!@students!scalar variables, arrays and hashes •An hash contains a set of values with an index of na