CUNY CSCI 132 - Perl Data Types and Variables

Unformatted text preview:

Perl Data Types and VariablesData Types in PerlTypelessnessThree Data ClassesLiteralsNumeric Literals in PerlInternal Representation of NumbersInaccuracy of NumbersGaps in Numeric RepresentationString Literals: Double-quoted StringsCase ConversionBackslash to Escape CharactersVariables in Double-Quoted StringsSingle-quoted StringsString Literals: Single-quoted StringsScalar VariablesThe Scoop on my DeclarationsVariables, Names, and StorageThe Assignment OperatorAssignment SemanticsRules to RememberInput ExpressionsMore ExamplesParenthesesString ExpressionsRepetitionMixed Types Mixed Types (2)Type ConversionType Conversion (2)Shorthand AssignmentsList DataLists Inside ListsLists Formed from RangesPrinting ListsQuoteWord qw()Array VariablesAccessing the ElementsSubscript OperatorArray AssignmentThe Importance of ArraysArray SlicesExamples of Array SlicesArray Functionsshift() and unshift() FunctionsShiftingUnshiftingExamples of shift() and unshift()The push() FunctionMore Examples The pop() FunctionThe pop() Function The Concept of a HashA Hash is a Finite FunctionDefining a HashHash LiteralsHash VariablesExampleExample ContinuedExample Continued FurtherSome Reminders About HashesContextScalar ContextList ContextDetermining ContextDetermining Context (2)Scalar Value of a List LiteralReferencesVariables, Names, and AddressesVariables ExposedBackslash OperatorReference Variables Store AddressesDereferencing ReferencesExamplesReferences to ArraysExample SummaryCopyright 2006 Stewart WeissCopyright 2009 Stewart WeissPerl Data Types and VariablesPerl Data Types and VariablesData, variables, expressions, and much more2 CSci 132 Practical UNIX with PerlData types in PerlPerl is unlike most high-level languages in that it does not make a formal distinction between numeric data and character data, nor between whole numbers and numbers with fractional parts. Most modern languages invented before Perl ask you to declare in advance whether a variable will store a character, a whole number, a floating point number, or something else. Not Perl.3 CSci 132 Practical UNIX with PerlTypelessness in PerlTo illustrate, if we declare a variable named anything using the statement my anything; then all of the following assignment statements are valid: anything = "Now I am a string"; anything = 10; anything = 3.141592;In short, in Perl, variables are untyped.4 CSci 132 Practical UNIX with PerlThree data classesHowever, Perl does distinguish the class of data, i.e., whether it is primitive or structured. Scalars are primitive and lists and hashes are structured: scalar data a single data item list data a sequence or ordered list of scalars hash data an unordered collection of (key,value) pairsScalars may be numbers such as 12 or 44.3 or strings like "the swarthy toads did gyre and gimble in the wabe". There are other kinds of scalars as well, as you will now see.5 CSci 132 Practical UNIX with PerlLiteralsA literal is a value explicitly represented in a program. For example, in print 56;the numeral 56 is a numeric literal, and in print "This is a string literal.";the string "This is a string literal." is a string literal.Some people call literals constants. It is more accurate to call them literals.6 CSci 132 Practical UNIX with PerlNumeric literals in PerlIntegers: -52 6994 6_994 #Note that _ can be used in place of ','Fixed decimal: 3.1415 -2.635 1.00Floating point (scientific notation): 7.25e45 # 7.25 x 10^45-12e-48 # -12.0 x 10^(-48)1.000E-5 # 1.000 x 10^(-5)Octal: 0377 # octal, starts with 0Hexadecimal: 0x34Fb # hexadecimal, case insensitive7 CSci 132 Practical UNIX with PerlInternal representation of numbersInternally, all numbers are stored and computed on as floating point values. Floating-point representation is like scientific notation; a number is stored as a mantissa and an exponent, either of which may be positive or negative or zero.The advantage of floating-point is that they can represent fractions, extremely large magnitudes such as the number of stars in the universe, and extremely small magnitudes such as the distance from a nucleus to an electron in meters.The disadvantage is that not all numbers can be represented accurately. This is not only because of scientific notation, but because of the nature of digital computers.8 CSci 132 Practical UNIX with PerlInaccuracy of numbersNot all numbers can be represented exactly in a digital computer, because computers can only represent numbers as sums of powers of 2. Try writing 1/3 as a sum of powers of 2 and you will see it cannot be. How about 1/5th? Not that either. Not 1/10th either. In fact, most numbers are represented with some small inaccuracy.9 CSci 132 Practical UNIX with PerlGaps in numeric representationImagine that the black rectangles below represent numbers that can be represented in floating point and blue are those that cannot. The rectangle below is like a piece of the number line showing that there are big gaps between representable numbers.The inaccuracies and gaps can lead to large errors in calculations. Numerical Methods is a branch of computer science concerned with methods of computation that do not produce large errors.10 CSci 132 Practical UNIX with PerlString literals: Double-quoted stringsDouble-quoted strings can contain special characters, such as:\n, \t, \r, \177 (octal 177) You have already seen that \n is a newline character. \t is the tab. You can see a more complete list of these special characters in the textbook. \c is the prefix for inserting control characters into strings, as in:\cC \cD control-C and control-D11 CSci 132 Practical UNIX with PerlString literals: Case conversionOther useful special characters are \L, \U, and \E. The pair \Ltext\E converts text to lowercase \Utext\E converts text to uppercase: "\LABc\E" # is abc "\LaBc\E" # is ABC "\L123\E" # is 12312 CSci 132 Practical UNIX with PerlBackslash escape characterIf you want to insert a character that has special meaning, you use the backslash to "escape" the special meaning. Since " is a special character, to insert a " into a string, write \"To insert a backslash, use \\. Example:print "Use \\ before \" to put a \" in a string.";This will print: Use \ before " to put a " in a string.13 CSci 132 Practical UNIX with PerlVariables in double-quoted stringsYou can put a variable in a double-quoted string. When the string is evaluated, the variable's value will be substituted


View Full Document

CUNY CSCI 132 - Perl Data Types and Variables

Download Perl Data Types and Variables
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Perl Data Types and Variables and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Perl Data Types and Variables 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?