Core Function PrintfC
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
// Recommended way PrintfC("Int64 value is: '%D' ok\n", @Int64_Max); // Alternative PrintfC("Int64 value is: '%I64d' ok\n", @Int64_Max);
If you are wondering how the hell to do a 64-bit unsigned integer
// Recommended way PrintfC("UInt64 value is: '%U' ok\n", @UInt64_Max); // Alternative PrintfC("UInt64 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 "";
Example of using %V
// The %V will use whatever Sputnik data type is given and translate it into // the appropriate fmt() type so a $var containing an Int32 using %V becomes %dsay "Now singles"; PrintfC("Int64 value is: '%V' ok\n", (char)65); PrintfC("Int64 value is: '%V' ok\n", null); PrintfC("Int64 value is: '%V' ok\n", true); PrintfC("Int64 value is: '%V' ok\n", false); PrintfC("Int64 value is: '%V' ok\n", (sbyte)77); PrintfC("Int64 value is: '%V' ok\n", (byte)100); PrintfC("Int64 value is: '%V' ok\n", (int16)777); PrintfC("Int64 value is: '%V' ok\n", (int32)777); PrintfC("Int64 value is: '%V' ok\n", (int64)777); PrintfC("Int64 value is: '%V' ok\n", (uint16)777); PrintfC("Int64 value is: '%V' ok\n", (uint32)777); PrintfC("Int64 value is: '%V' ok\n", (uint64)777); PrintfC("Int64 value is: '%V' ok\n", (float)777.42); PrintfC("Int64 value is: '%V' ok\n", (double)777.77); PrintfC("Int64 value is: '%V' ok\n", "Hello"); say "Now arrays"; PrintfC("Int64 value is: '%V' ok\n", array((char)65, (char)'a')); PrintfC("Int64 value is: '%V' ok\n", array(null, null, null)); PrintfC("Int64 value is: '%V' ok\n", array(true, false, (bool)1, (bool)0)); PrintfC("Int64 value is: '%V' ok\n", array((sbyte)77, (sbyte)11, (sbyte)22)); PrintfC("Int64 value is: '%V' ok\n", array((byte)100, (byte)200, (byte)202)); PrintfC("Int64 value is: '%V' ok\n", array((int16)777, (int16)778, (int16)778)); PrintfC("Int64 value is: '%V' ok\n", array((int32)1337, (int32)7331, (int32)1373)); PrintfC("Int64 value is: '%V' ok\n", array((int64)131, (int64)121, (int64)141)); PrintfC("Int64 value is: '%V' ok\n", array((uint16)1, (uint16)2, (uint16)3)); PrintfC("Int64 value is: '%V' ok\n", array((uint32)111, (uint32)222, (uint32)333)); PrintfC("Int64 value is: '%V' ok\n", array((uint64)1110, (uint64)2220, (uint64)3330)); PrintfC("Int64 value is: '%V' ok\n", array((float)777.77, (float)777.11, (float)777.22)); PrintfC("Int64 value is: '%V' ok\n", array((float)42.77, (float)42.11, (float)42.22)); PrintfC("Int64 value is: '%V' ok\n", array("Hello", "World", "ok"));