21天入门Perl系列,DAY03

Understanding Scalar Values

Today we will learn as follow:
1.Scalar values
2.How integers are represented lFloating-point values
3.The octal and hexadecimal notations
4.Character strings, and using the double-quote and single-quote characters to enclose them
5.Escape sequences
6.The interchangeability of character strings and numeric values

what is a scalar value?
Basically, a scalar value is one unit of data. This unit of data can be either a number or a chunk of text.

Integer Scalar Values
The most common scalar values in Perl programs are integer scalar values, also known as integer constants or integer literals.

Here are a few examples:
14
10000000000
-27
1_000_000

Integer Scalar Value Limitations
In Perl, there is a limit on the size of integers included in a program.

Listing 3.1. A program that displays integers and illustrates their size limitations.

#!/usr/bin/perl
$value = 1234567890;
print (“first value is “, $value,”\n”);
$value = 1234567890123456;
print (“second value is “, $value,”\n”);
$value = 12345678901234567890;
print (“third value is “, $value,”\n”);

On most machines, floating-point registers can store approximately 16 digits berore running out of space.

Floating-Point Scalar Values

In Perl, a floating-point scalar value consists of all of the following:
An optional minus sign (-)
A sequence of digits, optionally containing a decimal point
An optional exponent

Here are some simple examples of floating-point scalar values:
11.4
-275
-0.3
3.
.3

Listing 3.2. A program that displays various floating-point scalar values.

#!/usr/bin/perl

$value = 34.0;
print (“first value is”, $value,”\n”);
$value = 114.6e-01;
print (“second value is”, $value,”\n”);
$value = 178.263e+19;
print (“third value is “, $value, “\n”);
$value = 123456789000000000000000000000;
print (“fourth value is “, $value, “\n”);
$value = 1.23e+999;
print (“fifth value is “, $value, “\n”);
$value = 1.23e-999;
print (“sixth value is “, $value, “\n”);

Listing 3.3. A program that illustrates round-off error problems in floating-point arithmetic.

#!/usr/local/bin/perl
$value = 9.01e+21 + 0.01 – 9.01e+21;
print (“first value is “, $value, “\n”);
$value = 9.01e+21 – 9.01e+21 + 0.01;
print (“second value is “, $value, “\n”);
Using Double-Quoted Strings
Perl supports scalar variable substitution in character strings enclosed by double quotation-mark characters. For example, consider the following assignments:

$number = 11;
$text = “This text contains the number $number.”;
print (“The final result is “, $result, “\n”);
print (“The final result is $result\n”);\

Escape Sequences
\a Bell (beep)
\b Backspace
\cn The Ctrl+n character
\e Escape
\E Ends the effect of \L, \U or \Q
\f Form feed
\l Forces the next letter into lowercase
\L All following letters are lowercase
\n Newline
\r Carriage return
\Q Do not look for special pattern characters
\t Tab
\u Force next letter into uppercase
\U All following letters are uppercase
\v Vertical tab
Listing 3.4. A case-conversion program.

#!/usr/local/bin/perl
print (“Enter a line of input:\n”);
$inputline = <STDIN>;
print (“uppercase: \U$inputline\E\n”);
print (“lowercase: \L$inputline\E\n”);

print (“as a sentence: \L\u$inputline\E\n”);$ program3_4

Single-Quoted Strings
Perl also enables you to enclose strings using the ‘ (single quotation mark) character:
$text = ‘This is a string in single quotes’;

he first difference between double-quoted strings and single-quoted strings is that scalar variables are replaced by their values in double-quoted strings but not in single-quoted strings.

$string = “a string”;
$text = “This is $string”; # becomes “This is a string”
$text = ‘This is $string’; # remains ‘This is $string’

Interchangeability of Strings and Numeric Values

$string = “43”;
$number = 28;
$result = $string + $number;
$number = <STDIN>;
chop ($number);
$result = $number + 1;

$result = “hello” * 5;
# this assigns 0 to $result, since “hello” becomes 0

Initial Values of Scalar Variables
In Perl, all scalar variables have an initial value of the null string, “”. This means that you do not need to define a value for a scalar variable.

#!/usr/local/bin/perl
$result = $undefined + 2; # $undefined is not defined
print (“The value of \$result is $result.\n”); # The value of \$result is 2.

Summary
Perl supports three kinds of scalar values: integers, floating-point numbers, and character strings.

Integers can be in three notations: standard (decimal) notation, octal notation, and hexadecimal notation. Octal notation is indicated by a leading 0, and hexadecimal notation is indicated by a leading 0x. Integers are stored as floating-point values and can be as long as the machine’s floating-point precision (usually 16 digits or so).

Floating-point numbers can consist of a string of digits that contain a decimal point and an optional exponent. The exponent’s range can be anywhere from about e-309 to e+308. (This value might be different on some machines.) When possible, floating-point numbers are displayed without the exponent; failing that, they are displayed in scientific notation (one digit before the decimal point).

When you use floating-point arithmetic, be alert for round-off errors. Performing arithmetic operations in the proper order-operating on large numbers first-might yield better results.

You can enclose character strings in either double quotes (“) or single quotes (‘). If a scalar variable name appears in a character string enclosed in double quotes, the value of the variable is substituted for its name. Escape characters are recognized in strings enclosed in double quotes; these characters are indicated by a backslash (\).

Character strings in single quotes do not support escape characters, with the exception of \\and \’. Scalar variable names are not replaced by their values.

Strings and integers are freely interchangeable in Perl whenever it is logically possible to do so.

Q&A

Q:If Perl character strings are not terminated by null characters, how does the Perl interpreter know the length of a string?

A:The Perl interpreter keeps track of the length of a string as well as its contents. In Perl, you do not need to use a null character to indicate “end of string.”

Q:Why does Perl use floating-point registers for floating-point arithmetic even though they cause round-off errors?

A:Basically, it’s a performance issue. It’s possible to write routines that store floating-point numbers as strings and convert parts of these strings to numbers as necessary; however, you often don’t need more than 16 or so digits of precision anyway.Applications that need to do high-speed arithmetic calculations of great precision usually run on special computers designed for that purpose.

Q:What happens if I forget to call chop when reading a number from the standard input file?

A:As it happens, nothing. Perl is smart enough to ignore white space at the end of a line that consists only of a number. However, it’s a good idea to get into the habit of using chop to get rid of a trailing newline at all times, because the trailing newline becomes significant when you start doing string comparisons. (You’ll learn about string comparisons on Day 4, “More Operators.”)

Quiz

1.Define the following terms:
a round-off error
b octal notation
c precision
d scientific notation

2.Convert the following numbers from octal notation to decimal:
a 0377
b 06
c 01131

3.Convert the following numbers from hexadecimal notation to decimal notation:
a 0xff
b 0x11
c 0xbead
4.What does the following line print?
print (“I am bored\b\b\b\b\bhappy!\n”);

5.Suppose the value of $num is 21. What string is assigned to $text in each of the following cases?
a $text = “This string contains $num.”;
b $text = “\\$num is my favorite number.”;
c $text = ‘Assign \$num to this string.’;

6. Convert the following numbers to scientific notation:
a 43.71
b 0.000006e-02
c 3
d -1.04

Exercises
1. Write a program that prints every number from 0 to 1 that has a single digit after the decimal place (that is, 0.1, 0.2, and so on).

2. Write a program that reads a line of input and prints out the following: m1 if the line consists of a non-zero integer m0 if the line consists of 0 or a string (Hint: Remember that character strings are converted to 0 when they are converted to integers.)

3. Write a program that asks for a number and keeps trying until you enter the number 47. At that point, it prints Correct! and rings a bell.

4. BUG BUSTER: What is wrong with the following program? #!/usr/local/bin/perl$inputline = <STDIN>;print (‘here is the value of \$inputline\’, “: $inputline”);

5. BUG BUSTER: What is wrong with the following code fragment?
$num1 = 6.02e+23;
$num2 = 11.4;
$num3 = 5.171e+22;
$num4 = -2.5;
$result = $num1 + $num2 – $num3 + $num4;

6. BUG BUSTER: What is wrong with the following statement?
$result = “26” + “0xce” + “1”;

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s