Core Function Scanf

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(Example)
Line 31: Line 31:
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 
my $RET = Scanf("X123 Y456", "X%d Y%d");
 
my $RET = Scanf("X123 Y456", "X%d Y%d");
 +
printr($RET);
 +
 +
my $RET = Scanf("Copyright 2009-2011 CompanyName (Multi-Word Message)", "Copyright %d-%d %s (%[^)]");
 
printr($RET);
 
printr($RET);
 +
</syntaxhighlight>
 +
 +
Example #1 sscanf() Example
 +
<syntaxhighlight lang="sputnik">
 +
// getting the serial number
 +
list($serial) = Scanf("SN/2350001", "SN/%d");
 +
// and the date of manufacturing
 +
$mandate = "January 01 2000";
 +
list($month, $day, $year) = Scanf($mandate, "%s %d %d");
 +
println("Item $serial was manufactured on: $year-" . substr($month, 0, 3) . "-$day");
 +
</syntaxhighlight>
 +
 +
Example #2 sscanf() - using optional parameters
 +
If optional parameters are passed, the function will return the number of assigned values.
 +
<syntaxhighlight lang="sputnik">
 +
// get author info and generate DocBook entry
 +
$auth = "24\tLewis Carroll";
 +
$n = Scanf($auth, "%d\t%s %s", $id, $first, $last);
 +
print("<author id='$id'>
 +
    <firstname>$first</firstname>
 +
    <surname>$last</surname>
 +
</author>\n");
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Revision as of 16:31, 22 January 2013

Scanf( <expression>, <def> )

Contents

Description

Parses input from a string according to a format.

Parameters

expression

The string to evaluate.

def

The formation string containing the definition of how to parse the string.

Return Value

Success: Returns array of all captured objects from the parsed string.

Failure: Returns empty array.

Remarks

None.

Example

my $RET = Scanf("X123 Y456", "X%d Y%d");
printr($RET);
 
my $RET = Scanf("Copyright 2009-2011 CompanyName (Multi-Word Message)", "Copyright %d-%d %s (%[^)]");
printr($RET);

Example #1 sscanf() Example

// getting the serial number
list($serial) = Scanf("SN/2350001", "SN/%d");
// and the date of manufacturing
$mandate = "January 01 2000";
list($month, $day, $year) = Scanf($mandate, "%s %d %d");
println("Item $serial was manufactured on: $year-" . substr($month, 0, 3) . "-$day");

Example #2 sscanf() - using optional parameters If optional parameters are passed, the function will return the number of assigned values.

// get author info and generate DocBook entry
$auth = "24\tLewis Carroll";
$n = Scanf($auth, "%d\t%s %s", $id, $first, $last);
print("<author id='$id'>
    <firstname>$first</firstname>
    <surname>$last</surname>
</author>\n");
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox