Core Function PrintfC

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
Line 131: Line 131:
 
$arr = array("fmtSeparator" => " ", 10, 11, 12);
 
$arr = array("fmtSeparator" => " ", 10, 11, 12);
 
PrintfC('%x', $arr); # 'a b c'
 
PrintfC('%x', $arr); # 'a b c'
 +
 +
say "";
 +
 +
$arr = array("fmtSeparator" => " ", 65, 66, 67);
 +
PrintfC('%c', $arr); # 'a b c'
  
 
say "";
 
say "";

Revision as of 08:10, 30 August 2013

PrintfC( <format control>, <params> )

Contents

Description

Prints a formatted string (similar to the C sprintf() function).

Parameters

format control

See Fmt( <format control>, <params> ) for format control.

params

See Fmt( <format control>, <params> ) for params.

Return Value

None.

Remarks

This is basically a wrapper for the Fmt( <format control>, <params> ) function so it will do exactly what that function does but print the result instantly and return no string.

See Fmt( <format control>, <params> ) for remarks.

Example

Large example

$n = 43951789;
$u = -43951789;
 
// notice the double %%, this prints a literal '%' character
PrintfC("%%d = '%d'\n", $n);             //'43951789'          standard integer representation
PrintfC("%%e = '%e'\n", $n);             //'4.395179e+007'     scientific notation
PrintfC("%%u = '%u'\n", $n);             //'43951789'          unsigned integer representation of a positive integer
PrintfC("%%u <0 = '%u'\n", $u);          //'4251015507'        unsigned integer representation of a negative integer
PrintfC("%%f = '%f'\n", $n);             //'43951789.000000'   floating point representation
PrintfC("%%.2f = '%.2f'\n", $n);         //'43951789.00'       floating point representation 2 digits after the decimal point
PrintfC("%%o = '%o'\n", $n);             //'247523255'         octal representation
PrintfC("%%s = '%s'\n", $n);             //'43951789'          string representation
PrintfC("%%x = '%x'\n", $n);             //'29ea6ad'           hexadecimal representation (lower-case)
PrintfC("%%X = '%X'\n", $n);             //'29EA6AD'           hexadecimal representation (upper-case)
 
PrintfC("%%+d = '%+d'\n", $n);           //'+43951789'         sign specifier on a positive integer
PrintfC("%%+d <0= '%+d'\n", $u);         //'-43951789'         sign specifier on a negative integer
 
 
$s = 'monkey';
$t = 'many monkeys';
 
PrintfC("%%s = [%s]\n", $s);             //[monkey]            standard string output
PrintfC("%%10s = [%10s]\n", $s);         //[    monkey]        right-justification with spaces
PrintfC("%%-10s = [%-10s]\n", $s);       //[monkey    ]        left-justification with spaces
PrintfC("%%010s = [%010s]\n", $s);       //[0000monkey]        zero-padding works on strings too
PrintfC("%%10.10s = [%10.10s]\n", $t);   //[many monke]        left-justification but with a cutoff of 10 characters
 
PrintfC("%04d-%02d-%02d\n", 2008, 4, 1);

If you are wondering how the hell to do a 64-bit signed integer

PrintfC("Int64 value is: '%I64d' ok\n", @Int64_Max);

If you are wondering how the hell to do a 64-bit unsigned integer

PrintfC("Int64 value is: '%llu' ok\n", @UInt64_Max);

Example of using arrays

$n = 777;
$t = array(100, 200, 300, 400);
PrintfC("Single value is '%d' but lets do an array of values '%d' cool huh?\n", $n, $t);

Example of using arrays with seperator

$n = 777;
$t = array("fmtSeparator" => "-", 100, 200, 300, 400);
PrintfC("Single value is '%d' but lets do an array of values '%d' cool huh?\n", $n, $t);

Large example with arrays

$ints = array("fmtSeparator" => "-", 100, 200, 300, 400);
$floats = array("fmtSeparator" => "#", 100.2, 777.42, 133.77, 88.1);
$strings = array("fmtSeparator" => "#", "One", "Two", "Three");
PrintfC("Ints '%d' \nFloats '%g' \nStrings '%s'\n", $ints, $floats, $strings);

Example using "fmtSeparator", "fmtBegin" and "fmtEnd"

$arr = array("fmtSeparator" => "\n", "fmtBegin" => "0x", "fmtEnd" => "<-- Hex yup", 0..50);
PrintfC("Hex of the array\n%x\n", $arr);

More examples

PrintfC(  '%+d\n', 42   ); # '+42'
PrintfC(  '%4d\n', 42   ); # '  42'
PrintfC(  '%04d\n', 42   ); # '0042'
PrintfC(  '%X\n', 0x1337f00d   ); # '1337F00D'

Using an array as the format string

$arr = array("huey", "dewey", "louie");
PrintfC($arr); # 'huey dewey louie'
 
say "";
 
$arr = array(10, 11, 12);
PrintfC('%x', $arr); # 'abc'
 
say "";
 
$arr = array("fmtSeparator" => " ", 10, 11, 12);
PrintfC('%x', $arr); # 'a b c'
 
say "";
 
$arr = array("fmtSeparator" => " ", 65, 66, 67);
PrintfC('%c', $arr); # 'a b c'
 
say "";
 
$arr = array("fmtSeparator" => '; ', 1, 2, 3);
PrintfC('%02d', $arr); # '01; 02; 03'

Using an array (as a dictionary) as the format string

$arr = array("foo" => 1, "bar" => 2);
PrintfC($arr); # 'foo     1
               #  bar     2'
say "";
 
$arr = array("foo" => 1, "bar" => 2, "testingFooBar" => 3);
PrintfC($arr); # 'foo               1
               #  bar               2
               #  testingFooBar     3'
say "";

Using an array (as a dictionary) as the only param

$arr = array("Apples" => 5, "Oranges" => 10);
PrintfC('%s cost %d euros', $arr);
             # 'Apples cost 5 euros
             #  Oranges cost 10 euros'
say "";
 
// Of course you can still use the fmtSeparator
$arr = array("fmtSeparator" => ";", "Apples" => 5, "Oranges" => 10);
PrintfC('%s cost %d euros', $arr);
             # 'Apples cost 5 euros;Oranges cost 10 euros'
say "";
 
// Of course fmtBegin and fmtEnd are useable too
$arr = array("fmtBegin" => ">>>", "fmtEnd" => "<<<", "Apples" => 5, "Oranges" => 10);
PrintfC('%s cost %d euros', $arr);
             # '>>>Apples cost 5 euros<<<
             #  >>>Oranges cost 10 euros<<<'
say "";
 
// Can use them all
$arr = array("fmtSeparator" => ";", "fmtBegin" => ">>>", "fmtEnd" => "<<<", "Apples" => 5, "Oranges" => 10);
PrintfC('%s cost %d euros', $arr);
             # '>>>Apples cost 5 euros<<<;>>>Oranges cost 10 euros<<<'
say "";
 
$arr = array("fmtSeparator" => " -- ", "huey" => 1, "dewey" => 2, "louie" => 3);
PrintfC('%s', $arr);
             # 'huey -- dewey -- louie'
say "";
 
$arr = array("fmtSeparator" => " -- ", "huey" => 1, "dewey" => 2, "louie" => 3);
PrintfC('%s(%d)', $arr);
             # 'huey(1) -- dewey(2) -- louie(3)'
say "";
 
PrintfC("%s is %.3f AU away from the sun", "Earth", 1);
             # Prints "Earth is 1.000 AU away from the sun"
say "";
 
PrintfC("%s is %.3f AU away from the sun", array("Earth" => 1));
             # Prints "Earth is 1.000 AU away from the sun"
say "";
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox