Core Function VSPrintf

From Sputnik Wiki
Jump to: navigation, search
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");
[Args("true")] // Allow the @args variable to be made (we will use it)
function vprintf_arrays($format)
{
	my $ret = "";
	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] : '';
		$ret .= vsprintf($format, $pfargs);
	}
	return $ret;
}
 
my $ret = vprintf_arrays("ID '%s' Month '%s'\n", keys($months), values($months));
echo $ret;
/*
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'
*/
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox