preface 
 
  Look_up Engine  
 
  email how to  
 
  counter  
 
  regex  
 
  search engine  
 
  home  
 
  ask us  
 

 

Perl scripting


Applications


OO Perl


Perl & Databases


Search a word:
   



© The scientific sentence. 2010

Perl format write ()
Perl printf & format



With Perl, we can format a number, character, string or text. We 
use line format for the text and printf format specifiers  
for the others. These specifiers are similar to those we use wiht C 
and Fortran languages.

1. The printf format

1.1. The specifiers

%c character %d decimal (integer) number (base 10) %e exponential floating-point number %f floating-point number %i integer (base 10) %o octal number (base 8) %s a string of characters %u unsigned decimal (integer) number %x number in hexadecimal (base 16) %% (or \% ) print a percent sign

1.1. The width of a number

The statement: printf("%x,ya", N); means outputs the number N of thype "a" with "x+y" spaces and with "y" decimals Example1: Floating-point number #!C:/Perl/bin/perl use strict; my $number = 12345.6789; printf ("%5.3f",$number);# width of 8 with 3 decimals printf ("\n"); printf ("'%14.5f'",$number); # quoted: 19 spaces with 5 decimals printf ("\n"); printf ("%14.5f\n", - $number); # negative: width of 19 spaces with 5 decimals The output is: 12345.679 ' 12345.67890' -12345.67890 By default, these ouputs are right-justified. To left-justify those previous printf examples, just add a minus sign (-) after the % symbol: Example: #!C:/Perl/bin/perl use strict; my $number = 12345.6789; printf ("%25.2f",$number);# width of 25 spaces and 2 decimals printf ("\n"); printf ("%-25.2f",$number);# width of 25 spaces and 2 decimals printf ("\n"); That output is: 12345.68 12345.68 Example2: integer number #!C:/Perl/bin/perl use strict; my $number = 6789; printf ("%3d",$number);# infeger: width of 3 spaces printf ("\n"); printf ("%6d",$number);# integer: width of 6 spaces printf ("\n"); # zero-fill option printf ("%06d",$number);# integer: width of 6 spaces, zero-fill option printf ("\n"); printf("'%-+5d'", $number); #quoted integer: width of 5 spaces That output is: 6789 6789 006789 '+6789' Example3: Perl printf - printing strings #!C:/Perl/bin/perl use strict; my $sentence = 'Astronauts walked on the moon in 1969.'; printf ("%s\n",$sentence);# as it is with new-line printf ("'%s'",$sentence); # quoted printf ("\n"); printf ("%44s",$sentence); # right-justified printf ("\n"); printf ("'%- 44s'\n",$sentence); # quoted and left-justified with new-line That output is: Astronauts walked on the moon in 1969. 'Astronauts walked on the moon in 1969.' Astronauts walked on the moon in 1969. 'Astronauts walked on the moon in 1969. '

2. Format and write

2.1. Using format and printf

With the acronym PERL(Practical Extraction and Reporting Language), Perl is then associated with parsing and manipulation files and data. Actually, reporting data outputs the formatted results to a file or STDOUT for examination. Here are two ways to report results: Example: #!C:/Perl/bin/perl.exe -w @Books= ( "Physics:Waves:Michael Farady:324", "Mathematics:Trigonometry:Max Planck:453", "Chemistry:Molecules:Louis de Broglie:214", "Geography:North America:Max Born:565", ); printf "%-12s %-20s %-20s %3s\n", "Discipline", "Field", "Author", "Pages"; printf "%-12s %-20s %-20s %3s\n", "----------", "-----", "------", "-----"; foreach (@Books) { ($discipline, $field, $author, $pages) = split ":"; $format = "%-12s %-20s %-20s %3d\n"; printf $format, $discipline, $field, $author, $pages; } That output is:

2.2. Using format and write

Example: #!C:/Perl/bin/perl.exe -w @Books= ( "Physics:Waves:Michael Farady:324", "Mathematics:Trigonometry:Max Planck:453", "Chemistry:Molecules:Louis de Broglie:214", "Geography:North America:Max Born:565", ); foreach (@Books) { ($discipline, $field, $author, $pages) = split ":"; write; } # The headrer is written as: format STDOUT_TOP = Discipline Field Author Pages . format STDOUT = @<<<<<<<<<<< @<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<< @>> $discipline, $field, $author, $pages . # The dot is very important. That output is:
  
Google
Web
ScientificSentence
 




chimie labs
|
scientific sentence
|
java
|
php
|
green cat
|
contact
|


© Scientificsentence 2007. All rights reserved.