Difference between revisions of "ReNamer:Pascal Script:Basic Conversion Routines"

From den4b Wiki
Jump to: navigation, search
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:Cleanup}}
+
{{Up|ReNamer:Pascal Script}}
  
 
==Basic Conversion Routines or How to serialize files==
 
==Basic Conversion Routines or How to serialize files==
  
In this chapter we will take a look on serialization. All we need to do that is an integer variable and a way to convert the number into string. This type of convertion is done by IntToStr function. Convertion is necessary as FileName is a string and anything we want to add to a FileName must be also a string.
+
In this chapter we will take a look on serialization.  
  
In the following script we will add a serialized number in front of the filename.
+
====Converting Integer to the String====
 +
All we need to serialize filenames is integer variable and a way to convert the number into a string. This type of convertion is done by '''IntToStr''' function. Convertion is necessary as FileName is a string and anything we want to add to a FileName must be also a string.
  
<pre><nowiki>
+
In the following script we will add serialized number in front of the FileName.
 +
<syntaxhighlight lang="pascal">
 
var
 
var
i : Integer;
+
  i: Integer;
 
 
 
begin
 
begin
FileName:=IntToStr(i)+'_'+FileName;
+
  FileName := IntToStr(i) + '_' + FileName;
i:=i+1;
+
  i := i + 1;
 
end.
 
end.
</nowiki></pre>
+
</syntaxhighlight>
 
+
This script always serialize from 0 to "infinity". It's because all the variables are set to their default values when you press Preview. And the default value for integers is 0. Would you mind trying to rewrite this script so it would start to serialize from 1? It's enough to swap the main two lines of code.
 
 
This script always serialize from 0 to "infinity". It<nowiki>’</nowiki>s because all the variables are set to their default values when you press Preview. And the default value of integers is 0.
 
 
 
Would you mind trying to rewrite this script so it would start to serialize from 1?
 
 
 
It<nowiki>’</nowiki>s enough to swap the main two lines of code.
 
 
 
<pre><nowiki>
 
i:=i+1;
 
FileName:=IntToStr(i)+’_’+FileName;
 
</nowiki></pre>
 
 
 
Now '''i''' is incremented from default 0 to 1 before the first filename is changed.
 
  
For now we can serialize from 0 or 1. How to start from a different number will be covered in the next chapter.
+
<syntaxhighlight lang="pascal">
 +
  i := i + 1;
 +
  FileName := IntToStr(i) + '_' + FileName;
 +
</syntaxhighlight>
  
And what if we have filenames that starts from two-digit random number and we want to increase these numbers eg. by 2?
+
Now '''i''' is incremented from default 0 to 1 before the first filename is changed. For now we can serialize from 0 or 1. How to start from a different number will be covered in the next chapter.
  
We need to extract the number from the FileName string, convert it to integer, increase by 2 and then convert back to string. It means we would need the StrToInt function which is the complete opposition to already known IntToStr.
+
====Converting String to the Integer====
 +
And what if we have filenames that already start from a two-digit number and we want to increase these numbers by 2?
  
<pre><nowiki>
+
We need to extract the number from the FileName string, convert it to the integer, increase by 2 and then convert back to the string. It means we would need the '''StrToInt''' function which is the complete opposition to already known '''IntToStr'''.
 +
<syntaxhighlight lang="pascal">
 
var
 
var
i : Integer;
+
  i: Integer;
Number : String;
+
  Number: String;
 
 
 
begin
 
begin
Number:=Copy(FileName, 1, 2);
+
  Number := Copy(FileName, 1, 2);
i:=StrToInt(Number);
+
  i := StrToInt(Number);
if i >= 0 then
+
   i := i + 2;
   begin
+
  FileName := IntToStr(i) + WideCopy(FileName, 3, Length(FileName)-2);
    i:=i+2;
 
    FileName:=IntToStr(i)+WideCopy(FileName, 3, Length(FileName)-2);
 
  end;
 
 
end.
 
end.
</nowiki></pre>
+
</syntaxhighlight>
  
If the string passed to StrToInt function cannot be converted into integer, function will return -1.
+
Beware, if the string passed to '''StrToInt''' function cannot be converted into integer, function will fail and preview procedure will be terminated with an error message.
  
If that doesn<nowiki>’</nowiki>t suit us (cause we want to convert <nowiki>’</nowiki>-1<nowiki>’</nowiki> strings into integers as well and we still want to be warned when the convertion fails) we may use StrToIntDef function that apart from the string takes the default integer value as a parameter and returns that value on failure. So we could use
+
If that doesn't suit us and we want to handle those cases, we can use '''StrToIntDef''' function that apart from the string takes the default integer value as a parameter and returns that value on failure. So we could use it like this:
  
<pre><nowiki>
+
<syntaxhighlight lang="pascal">
i:=StrToIntDef(Number, -123);
+
  i := StrToIntDef(Number, -1);
if i <> -123 then...
+
  if i <> -1 then ...
</nowiki></pre>
+
</syntaxhighlight>
  
in our code.
+
[[Category:ReNamer]]
 +
[[Category:Pascal Script]]

Latest revision as of 15:05, 8 February 2017

Basic Conversion Routines or How to serialize files

In this chapter we will take a look on serialization.

Converting Integer to the String

All we need to serialize filenames is integer variable and a way to convert the number into a string. This type of convertion is done by IntToStr function. Convertion is necessary as FileName is a string and anything we want to add to a FileName must be also a string.

In the following script we will add serialized number in front of the FileName.

var
  i: Integer;
begin
  FileName := IntToStr(i) + '_' + FileName;
  i := i + 1;
end.

This script always serialize from 0 to "infinity". It's because all the variables are set to their default values when you press Preview. And the default value for integers is 0. Would you mind trying to rewrite this script so it would start to serialize from 1? It's enough to swap the main two lines of code.

  i := i + 1;
  FileName := IntToStr(i) + '_' + FileName;

Now i is incremented from default 0 to 1 before the first filename is changed. For now we can serialize from 0 or 1. How to start from a different number will be covered in the next chapter.

Converting String to the Integer

And what if we have filenames that already start from a two-digit number and we want to increase these numbers by 2?

We need to extract the number from the FileName string, convert it to the integer, increase by 2 and then convert back to the string. It means we would need the StrToInt function which is the complete opposition to already known IntToStr.

var
  i: Integer;
  Number: String;
begin
  Number := Copy(FileName, 1, 2);
  i := StrToInt(Number);
  i := i + 2;
  FileName := IntToStr(i) + WideCopy(FileName, 3, Length(FileName)-2);
end.

Beware, if the string passed to StrToInt function cannot be converted into integer, function will fail and preview procedure will be terminated with an error message.

If that doesn't suit us and we want to handle those cases, we can use StrToIntDef function that apart from the string takes the default integer value as a parameter and returns that value on failure. So we could use it like this:

  i := StrToIntDef(Number, -1);
  if i <> -1 then ...