Documente Academic
Documente Profesional
Documente Cultură
edu>
LEARNINGPERL
Numbers
Strings
Strings
Autoconversion:Numbers&Strings
Variables
BinaryAssignmentOperators
OutputUsingprint
Interpolation
DelimitingtheVariableName
ComparisonOperators
Numericcomparisonoperators ==,!=,<,>,<=,>= Examples: 50==100/2; #true 100/3!=33.3 #true Stringcomparisonoperators eq,ne,lt,gt,le,ge 'pedro'lt'jose' #false 'jose'eqjose #true ''gt'' #true
BooleanValues
undef,numberzero(0),stringzero('0'),the emptystring(''),areallfalse.Undefdesignates avariablewithnovalueassignedyet. nonzeronumbers(like1)andnonempty strings(except'0')arealltrue. Examples $bool1='Fred'lt'fred'; $bool2='fred'lt'Fred'; print$bool1;#prints1fortrue print$bool2;#emptystringforfalse
IfControlStructure
ReadingOneLinefromStdin
Use<STDIN>toreadonelinefromstandard input,usuallytheconsolekeyboard Examples: printEnterfirstname:; $fname=<STDIN>; printEnterlastname:; $lname=<STDIN>; chomp($fname); chomp($lname); printYourname:$fname$lname\n;
Thechomp()Function
WhileControlStructure
UNDEF
Thedefined()Function
Exercises
WriteaPerlprogramthatreadslinesofinput from<STDIN>,andprintseachlineread.Stop whenthelinethatisreadis'Done'(withoutthe quotes). WriteaPerlprogramthatreadsthevaluesof threevariables$num1,$oper,and$num2from <STDIN>.Ifthevalueof$operisoneofthe strings'plus','minus','times',or'over',the programshouldcarryouttheindicated operationon$num1and$num2.
Lists&Arrays
InitializingArrayswithLiteralValues
Anarraymaybeinitializedwithvaluesin parentheses().Example: @propty=('Pablo',62,'male', undef); Here,thearrayis@propty,andthevaluesin thelistare: $propty[0]is'Pablo' $propty[1]is62 $propty[2]is'male' $propty[3]isundef#civilstatus
ValuesMayAllBeSameType
Alllistvaluesmaybethesametype @friends=('Pablo','Jose', 'Juan','Mario','David'); Here,thearrayis@friends,andthevaluesin thelistare: $friends[0]is'Pablo' $friends[1]is'Jose' $friends[2]is'Juan' $friends[3]is'Mario' $friends[4]is'David'
ValuesofArrayIndices
Anyvalue,variable,orexpression,whosevalueis integerorcanbeconvertedtointegercanbeusedas index. Example: $ndx=2.5; $friends[$ndx+1]is$friends[3] $#friendsisthevalueofthelastindexofarray @friends,whichis4. $friends[$#friends+10]='Carlos'; addselement'Carlos'atindex14,the15thelement. Valuesatindex5to13willbeundef.
InitializingArraywithLiteralValues
InterpolateArrays/ValuesinStrings
If@arrisanarray,thenarray@arrandlist value$arr[k]willbeinterpolated(evaluated) whenplacedinsidedoublequotedstrings Exampleinterpolatingarrays @arr=(5..7); printFour@arreight\n; #willprintFour567eight Exampleinterpolatinglistvalues @toy=('toycar','toyrobot', 'toygun'); printIhavea$toy[2]athome\n;
pop()Function
push()Function
shift()andunshift()
foreachControlStructure
Perl'sDefaultVariable:$_
reverse()andsort()
reverse(@arr)reversestheorderofvaluesin thelist @fruits=qw/mangopapayachico/; @revfr=reverse(@fruits); @fruits=reverse(@fruits); sort(@arr)sortsthevaluesinthelistin increasinglexicographicorder,orstringorder, notnumericorder @fruits=qw/mangopapayachico/; @sfruits=sort(@fruits); @rfruits=reversesort@fruits;
Forcingscalar()Context
<STDIN>asListorScalar
SortingLinesfrom<STDIN>
Exercises
Writeaprogramthatreadsfrom<STDIN>aset ofnumericvalues,oneperline,andcomputes themeanandvarianceofthesevalues.IfNis thenumberofvalues,then mean=(sumofallvalues)/N; variance= (sumsquare(eachvaluemean))/N; Writeaprogramthatreadslinesfrom<STDIN>, sortstheselinesinreversealphabeticalorder, printsthelines,andprintsthetotalnumberof lines.
Hashes
Ahashisalistofkeyvaluepairs.Thevariable namestartswith% %age=(Pablo,62,Karen,23, Paul,33); HerethekeyPablohasvalue62,thekey Karenhasvalue23,andthekeyPaulhas value33. Accessingahashbykey $age{Paul}gives33 $age{Karen}gives23
Hashes:BigArrowNotation
UsingaHash
Keys&Values
each()&exists()
HashElementInterpolation
Exercises
Subroutines
ExampleFunction
Usingthefunction: &greet;
PassingArguments
LocalVariables;ReturningValues
Exercises