• Aucun résultat trouvé

Creating a Table

Dans le document This book is dedicated to its readers. (Page 136-142)

Listing05_09 puts the formatting specifiers you have examined in previous sections to work to create a table.

#Listing05_09

# Creating a table using specifiers and flags

#1 Definitions of identifiers

$PlRadiusOfEarth = 6357; #kilometers

$EqRadiusOfEarth = 6378; #kilometers

$EarthToMoon = 3.84401e6; #kilometers

$EarthToSun = 149597870.7; #kilometers

$AgeOfSolarSystem = 4.6e9; #years

$AgeOfKnownUniverse = 16.5 * (10 ** 9); #years

$SpeedOfLight = 299792458; #meters per second

$LightYear = 9.6 * (10 ** 12); #kilometers

$DToProximaCentauri = 4.26; #light years

$FarthestGalaxy = 15 * (1.0e9); #light years

$MilkyWayDiameter = 1000000; #light years

Figure 5.9

Specifiers and flags allow you to control the appear-ance of the text.

Q Creating a Table

$MilkyWayThickness = 1000; #light years

$StarsInMilkyWay = 1.0 * (10 ** 11);

$KmspeedOfLight = $SpeedOfLight/1000; #kilometers

$RatioOfSStoKU = $AgeOfKnownUniverse/$AgeOfSolarSystem;

$LY = "lys"; #light years

$KM = "km"; #kilometers

$EY = "years"; #years

$NL = "\n";

#2 Construction of a table print "$NL $NL $NL";

print("$NL---Astronomy Data Table".

"---");

printf("%-36s %20d %12s", $NL . "Equatorial radius of earth", $EqRadiusOfEarth, $KM);

printf("%-36s %20d %12s", $NL . "Polar radius of earth", $PlRadiusOfEarth, $KM);

printf("%-36s %20d %12s", $NL . "Distance of Earth to Sun", $EarthToSun, $KM);

printf("%-36s %20d %12s", $NL . "Distance of Earth to Moon", $EarthToMoon, $KM);

printf("%-36s %20.0f %12s", $NL . "Age of the Earth/Solar System", $AgeOfSolarSystem, $EY);

printf("%-36s %20.0f %12s", $NL . "Age of the known universe", $AgeOfKnownUniverse, $EY);

printf("%-36s %20.2f %12s", $NL . "Ratio of earth / universe age", $RatioOfSStoKU, "");

printf("%-36s %20.0f %12s", $NL . "Speed of light", $SpeedOfLight, "m sec");

printf("%-36s %20.2f %12s", $NL . "Speed of light", $KmspeedOfLight, "$KM"." per s");

printf("%-36s %20.2e %12s", $NL . "Light year (si)" , $LightYear, $KM );

printf("%-36s %20.2f %12s", $NL . "Light year", $LightYear, $KM);

printf("%-36s %20.2f %12s", $NL . "Distance to Proxima Centauri", $DToProximaCentauri, $LY);

CHAPTER 5

}

Scalars and Formatting

116

Q Q Q TEAM LinG

printf("%-36s %20.0f %12s", $NL . "Distance to the farthest Galaxy", $FarthestGalaxy, $LY);

printf("%-36s %20.0f %12s", $NL . "Diameter of the Milky Way" , $MilkyWayDiameter, $LY);

printf("%-36s %20.0f %12s", $NL . "Thickness of the Milky Way", $MilkyWayThickness, $LY);

printf("%-36s %20.0f %12s", $NL . "Number of stars in the Milky Way", $StarsInMilkyWay, "$LY");

print "$NL $NL $NL";

The code accompanying comment #1 in Listing05_09 defines a set of values for the table. With the code associated with comment #2, you begin working through the list of data to create the table using the printf() function. In each use of the printf() function you create three columns. The first column presents a string. For the string, you concatenate a scalar identifier containing a new line escape sequence with literal strings that introduce the astronomical data the table presents. Each use of the print() function creates a row in your table.

You set the field width for the first column of the table to be 36 characters wide, justified to the left, and of the string type. In this respect, all the rows are the same. You make the third column of the table consist of fields 12 characters wide and of the string type. You allow the default right justification to apply to these fields.

The middle or second column of the table sports three types of fields. The fields are all 20 characters wide, but in some cases they display decimal values. In other cases, they display float values. In one field you use the e specifier to force the number to display in scientific notation.

This value pertains to the speed of light in kilometers. To show a comparison between scientific notation and a standard decimal number, you also use the f formatting specifier.

Figure 5.10 illustrates the appearance of the table. The conversion specifiers for the data you employ in the table considerably improve the appearance of the data over previous displays. The numbers show no decimal places when it does not seem reasonable to display them. On the other hand, they can be shown (in the instance of the distance to Proxima Centauri) when the decimal places make sense.

Variations

Listing05_10 provides a few examples of common formatting operations. As previous sections have emphasized, you can use a minus sign to left justify a column. You can employ such capabilities in combination with type specifiers to create specific results, such as fillers. Such techniques can come in handy for creating headers. You can also prefix a zero to a specifier to fill a column with zeros. You might use this formatting procedure to generate inventory or other Q Creating a Table

numbers that must possess a uniform number of characters. Appending a plus sign to a number is at times handy, also.

#Listing05_10

# Variations

$PlRadiusOfEarth = 6357; #kilometers

$NL = "\n";

#1. #Prefixing a minus sign aligns the field to the left.

# No minus sign renders right justification.

printf ("%-1s %-10s %1s", "1|", "="x10, "|1");

print $NL;

#2. Append a period after the field width specifier and

# define the number of visible decimal places

# The field width is 9; the decimal places is 3 printf ("|%9.3f|", 123.1234567);

print $NL;

#3. Prefix a plus sign to show the sign

printf "%-20s %+15.2f","Polar radius of earth (km)", $PlRadiusOfEarth;

Figure 5.11 illustrates the output of Listing05_10.

Figure 5.10

Specifiers and flags allow you to create table columns.

CHAPTER 5

}

Scalars and Formatting

118

Q Q Q TEAM LinG

Figure 5.11

Some variations on for-matting can come in handy.

Investigating ASCII Conversions

Figure 5.12 provides a truncated version of a table displaying ASCII codes. ASCII stands for American Standard Code for Information Interchange, and you can find such tables on the In-ternet (www.lookuptables.com, for example). The ASCII code for a given character is its numerical representation. As Figure 5.12 indicates, 060 represents the less than (<) character, while 062 represents the greater than (>) character.

Figure 5.12

A table of ASCII values allows you to see the nu-meric face of characters.

Listing05_11 provides examples of some of the operations you can perform to manipulate data using the ASCII designations.

#Listing05_11

#convert ASCII, hexadecimal, and octal values

Q Creating a Table

$NL = "\n ";

#Decimal Octal Hex Character

#60 074 03C <

062 076 03E >

# 1. Print the character that a given ASCII value designates printf("$NL The character for %d is %c", 60, 60);

printf("$NL The character for %d is %c", 62, 62);

#2 Use hex() to return the decimal value of a hexadecimal printf($NL x 2 . "An ASCII value of %d renders an %c", 60, 60);

print("$NL Hex: 03C");

#convert to decimal

$HEXA = hex("03C");

printf("$NL The character is %c", $HEXA);

#3 Use oct() to return the decimal value of an octal

printf($NL x 2 . "An ASCII value of %d renders an %c", 60, 60);

print("$NL Octal: 074");

#convert to decimal

$OCTA = oct("074");

printf("$NL The character is %c", $OCTA);

#4 Use ord() to extract the ASCII value from a character print($NL x 2 . "$NL For \'<\' ord() returns " . ord('<'));

printf("$NL The number for \'%s\' is %d", 'c', ord('<'));

At comment #1 in Listing05_11, you employ the printf() function to display a literal number and to convert a number using the c specifier to a character. At the start of the program, columns from an ASCII table allow you to view a few numerical values of characters. When you use the c specifier (%c), you convert an integer value to a character. You can convert numbers between 0 and 255 using this approach. As Figure 5.13 illustrates, Listing05_11 converts 60 to < and 62 to >.

The lines trailing comments #2 and #3 venture into exploring hexadecimal and octal values.

To employ such values, you use the hex() and oct() functions. Both of these functions return decimal values.

In the code affiliated with comment #4, you make use of the ord() function to convert a character to a number. This is the reverse of the action you perform with the c specifier.

CHAPTER 5

}

Scalars and Formatting

120

Q Q Q TEAM LinG

Figure 5.13

Use the print functions to convert all types of data.

Dans le document This book is dedicated to its readers. (Page 136-142)

Documents relatifs