Core Function VSPrintf
From Sputnik Wiki
(Difference between revisions)
(Created page with "<pre> VSPrintf( <format control>, <params>... ) </pre> === Description === Returns a formatted string (similar to the C sprintf() function) but accepts arrays as the arguments....") |
m (4 revisions) |
||
(9 intermediate revisions by one user not shown) | |||
Line 33: | Line 33: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
print vsprintf("%04d-%02d-%02d", split('1988-8-1', '-')); // 1988-08-01 | print vsprintf("%04d-%02d-%02d", split('1988-8-1', '-')); // 1988-08-01 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | $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. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | $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. | ||
+ | */ | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | 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' | ||
+ | */ | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 12:37, 14 June 2015
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' */