#!/usr/bin/perl ####################################### # This document explains decelration # of new variables/arrays and # simple operators/controls in # Perl # AAK: Last modification: # Tue Sep 11 21:51:16 PDT 2012 # ##################################### #this is how you define new variables, all variables have $ in front #both when declared or used: $stringvar = "Hello World"; $intnum = 3; $realnum = -3.42e-5; # \n is the new line character print $stringvar, ".\n"; #Perl realizes which type of variable you are using ##### Strings: ########## #single quotation is more restricted than double quotation: print 'it\'s cool to be able to write apostrophe', "\n"; # . is an operator that adds two strings together: print 'even cooler is writing backslash : \\' . "\n"; print 'first line without endline.'; print ' new line, you see!' . "\n"; print "tab \t \t is also cool. \n"; print "look you can even type double quote: \" \n"; print "\Uthis is gonna change to upper case \E . end of upper case \n"; #perl accepts + - / * **(power) arithmetic operators print '3 + 5 = ', 3 + 5, "\n"; $str2 = "!"; #Adding strings using '.' operator: $str3 = $stringvar . $str2; print $str3, "\n"; print $nosuchvardefined , "\n"; print "in previous line there was no such variabled defined, so it print nothing \n"; #you can multiply a string by an integer print "three copy of : ", $str3 x 3 , "\n"; print "perl takes care of conversion between number and string \n"; print "for example: ", $stringvar . $intnum, "works \n"; print "as well as: ", "5 x 3 = ", "5" * 3, "\n"; #guess what this one does? $intnum += 5; print "new intnum is: ", $intnum, "\n"; print "Oh! I forgot about commenting in perl \n"; #this is a comment! $what = "dish"; print "there are five $whates on the table \n"; #it didn't work! we need to somehow isolate #the name of the variable from the strings around #{} isolates the name of variable: print "there are five ${what}es on the table \n"; #exponentiation: print " 2 ^ 10 = ", 2**10, "\n"; print " 2 ^ 0.7 = " , 2**0.7, "\n"; #comparison and if statement: if ( 5 > 3.23e-1 ) { print "this is example of if statement \n"; } #perl compares strings using ASCII ordering: if ( 'bad' lt 'good' ) { print "you can also compare strings using gt ne lt le ge \n"; } # gt = greater than | le = less equal and so on...! #perl accepts < > <= >= != == as comparison operators: $logicalvar = (2 != 2); if ($logicalvar) { print "this is not gonna be printed \n"} if (whatever) {print "any non empty string is considerd as true \n";} if (3.42) {print "any nonzero number is considered as true \n";} if (5) { print "example of nested if \n"; if (0) {print "this is not gonna be printed";} } # '!' is the not operator if ( !(2 > 3) ) {print " ! means not operator \n";} print "should we keep going (y/n)? \n"; #perl can read from standard input using: $swkg = <STDIN>; print "your answer was: $swkg"; print "did you noticed the newline in swkg variable?\n"; print "OK lets get rid of that new line character other wise the following is not gonna work.\n"; #chomp gets rid of end of line character \n chomp($swkg); print "your answer without newline is: $swkg" . "chomp worked! \n"; #perl accepts OR logical operator by ||: if ( ($swkg eq 'y') || ($swkg eq 'n') ) { if ( $swkg eq 'y') { print "Ok lets keep going!\n"; } else { print "exiting...\n"; exit 0; #exits the program with status 0 } } else { print "You OK man? \n"; print "BTW: it was a example of \"or\" operator \n"; } $count = 1; # while loop is as following: while ($count <= 10) { print "count is now $count \n"; $count += 1; } print "perl handles undefined variables like this: ", $undefintvar * 5, "\n"; print "and like this: ", $undefintvar . "something", "\n"; $n=1; $facn=1; # *= is similar to +=, but multiplies the left side by the right side # and puts the value on left side variable: while ($n <= 9) { $facn *= $n; $n += 1; } print "9! = ", $facn, "\n"; print "give me sth: \n"; $inp = <STDIN>; if ( defined($inp) ){ print "useful for end of file detection \n"; #at the end of file, $inp will be undef, not an empty string. } ################################################################ # Defining arrays in Perl: # ############################################################## $nl = "\n"; $r[0] = "Hello"; $r[1] = 1.24e-87; # $r[i] refers to the i'th element of r ($fa, $fb) = ("Hello", 2.3e3); print $fa, $fb, $nl; ($fa, $fb) = ($fb, $fa); print "swapping two variables:", ($fa, $fb), $nl; @lff = ("apple ", "orange ", "strawberry "); # @lff refers to entire array print $lff[0], $lff[1] , $lff[2], $nl; @lff2 = qw( apple orange strawberry ); print @lff2, $nl ; #notice what qw did? #you don't need to type all the " and , #if you use qw #defining new array with extending the previously #defined array: @lff3 = (@lff, "melon"); print $lff3[3], $nl; # .. is the range operator: @lofn = 1..10; #lofn gets all the values: 1 2 3 4 5 6 7 8 9 10 print @lofn, $nl; $x[0] = 0; $x[1] = 1; $x[20] = 20; print @x, $nl; #value of 8th element is undef $udfv = $x[8]; print $udfv, $nl; #it will print an empty line print "length of x is = ", $#x, $nl; @nr1 = 5..9; #pop truncate the last element (say x) of an array and returns x: $nrw = pop (@nr1 ); print $nrw, $nl; print @nr1, $nl ; #if list is empty pop returns undef #push adds an element: push (@nr1, 0); print @nr1, $nl; push (@nr1, 100..114); print @nr1, $nl; #shift and unshift does exactly what pop and push does but #for the begining of the array print shift(@nr1), $nl; print shift(@nr1), $nl; unshift(@nr1, "NEITB "); print $nr1[0], $nl; #array can be converted into strings using double quotation #after conversion , there is a space between elements. #which makes it nice!! print "@nr1", $nl; #do loop: foreach $i (1..10) { print "i = $i", $nl; } foreach $j (@lff2) { print $j, $nl; } $fcl=1; foreach (1..10) { #perls default for loop control variable is '$_' $fcl *= $_; } print "10 ! = $fcl \n"; @lst = (1..10); #reverse returns the list in opposite order @lst2 = reverse(@lst); print "@lst2 \n"; #note that reverse doesn't affect the array, but returns it in reverse. #using sort you can sort the array @lst3=(1.23,0.74,1.2,0.5,1.2e-3); @lst3s = sort(@lst3); print "@lst3s \n"; #sort can also sort the strings using ASCII ordering standards #note that in perl @sth can return the number of elemnts depending on the contect: print 45 + @lst3s, $nl; #will return 45 + number of elements in lst3s #in perl depending on the context, different things can happen: $df = something; #considers it as a variable @ps = something ; #considers something as an array/list ($ws, $yt) = something ; #returns list content, therefore nothing assigned for $yt print $ws, $yt, $nl; #defining an empty one element array (undef) is as following: @plsj = (); #there is a way to force perl to interpret the list context as scalar: #look at the output for the difference: print "lff has ", @lff, " fruits in it \n"; print "lff has ", scalar (@lff), " fruits in it \n"; # STDIN can also be affected by context: print "Start writing, when finished type Ctrl + D \n"; @lines = <STDIN>; #reads standard input into list #it will read each line into one element #to stop it you need to use EOF control if reading from standard input (Ctrl + D) #in linux, if reading is from file (by redirection operator <) #then it will read till end of file print "*****This is what you typed:***************\n"; print @lines; print "*****End*********\n"; #you can get rid of new line character at the end of lines by chomping them: #chomp if operated on the list, will chomp the end of line character from all the elements: chomp (@lines); print "@lines \n";