Core Function VSPrintf
From Sputnik Wiki
(Difference between revisions)
(→Example) |
|||
Line 68: | Line 68: | ||
function vprintf_arrays($format) | function vprintf_arrays($format) | ||
{ | { | ||
− | |||
my $args = @args; | my $args = @args; | ||
shift($args); | shift($args); | ||
Line 77: | Line 76: | ||
foreach($args as $arr) | foreach($args as $arr) | ||
$pfargs[] = ($arr is array && $arr[$i]) ? $arr[$i] : ''; | $pfargs[] = ($arr is array && $arr[$i]) ? $arr[$i] : ''; | ||
− | + | vprintf($format, $pfargs); | |
} | } | ||
− | |||
} | } | ||
− | + | vprintf_arrays("ID '%s' Month '%s'\n", keys($months), values($months)); | |
− | + | ||
/* | /* | ||
ID '0' Month 'January' | ID '0' Month 'January' |
Revision as of 14:38, 9 August 2014
VSPrintf( <format control>, <params>... )
Contents |
Description
Returns a formatted string (similar to the C sprintf() function) but accepts arrays as the arguments.
Parameters
format control
See SPrintf( <format control>, <params> ) for format control.
params
One or more arrays to pass as the arguments to VPrintf()
Return Value
None.
Remarks
This is basically a wrapper for the SPrintf( <format control>, <params> ) but accepts arrays of arguments and does them in sequence.
See SPrintf( <format control>, <params> ) for remarks.
Example
See SPrintf( <format control>, <params> ) for example.
print vsprintf("%04d-%02d-%02d", split('1988-8-1', '-')); // 1988-08-01
$fruits = array(1, 'banana',1, 'apples', 3, 'oranges', 2, 'peaches'); print vsprintf("I have %d %s, %d %s, %d %s and %d %s.", $fruits); //I have 1 banana, 1 apples, 3 oranges and 2 peaches.
$format = "A %s %s %s.\n"; $array1 = Array("monkey", "cow", "rooster"); $array2 = Array("eats", "goes", "crows"); $array3 = Array("bananas", "moo", "in the morning"); $ret = vsprintf($format, $array1, $array2, $array3); echo $ret; /* A monkey cow rooster. A eats goes crows. A bananas moo in the morning. */
my $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novemeber", "December"); function vprintf_arrays($format) { my $args = @args; shift($args); my $count = count($args[0]); for($i = 0; $i < $count; $i++) @{ my $pfargs = Array(); foreach($args as $arr) $pfargs[] = ($arr is array && $arr[$i]) ? $arr[$i] : ''; vprintf($format, $pfargs); } } vprintf_arrays("ID '%s' Month '%s'\n", keys($months), values($months)); /* ID '0' Month 'January' ID '1' Month 'February' ID '2' Month 'March' ID '3' Month 'April' ID '4' Month 'May' ID '5' Month 'June' ID '6' Month 'July' ID '7' Month 'August' ID '8' Month 'September' ID '9' Month 'October' ID '10' Month 'Novemeber' ID '11' Month 'December' */