Page 1 of 1

VBScript runtime error in script filter called "field maths"

Posted: Sun Oct 30, 2011 5:24 am
by DFH
In the script filter called "field maths", I get

Code: Select all

[13] Microsoft VBScript runtime error:Type mismatch at line:9, char:2near:
when using the Trial Run data.

Line 9 reads

Code: Select all

  fields = split( line, chr(9) )

Code: Select all

'Perform maths on tab delimited fields

'Called for every line in the file
'EOL contains the end of line characters (Unix, DOS or Mac) that must be
'appended to each line
function processLine(line, EOL)
  'ie a tab
  dim fields(3)
  fields = split( line, chr(9) )
  redim preserve fields(4)
  fields(3) = fields(1) / fields(2)
  processLine = join(fields, chr(9)) & EOL
end function


'Called at the start of a processing job -
'perform one-time initialisation here
sub startJob()
end sub


'Called at the end of a processing job -
'destroy any declared objects here
sub endJob()
  'do nothing
end sub


'Called before each file is opened -
'perform per-file initialisation here
function startFile()
  startFile = ""
end function


'Called before each file is closed -
'flush all pending file output here
function endFile()
  endFile = ""
end function
Please explain why.

If this filter doesn't work, please correct it before the next release of TextPipe.

Re: VBScript runtime error in script filter called "field ma

Posted: Mon Oct 31, 2011 10:18 pm
by DFH
Got it!

The problem is actually in line 8.

Code: Select all

  dim fields(3)
This should read

Code: Select all

  dim fields
Then the filter works, and trial run outputs as follows:

Code: Select all

2009-06-06	61383.9200	10529.8651	5.82950678067091	
2009-06-07	42263.6000	10089.0255	4.18906662491833	
2009-06-08	23442.0400	10904.0565	2.14984579362735	
2009-06-09	22708.4600	6829.3108	3.32514666048	
2009-06-10	.0000	6656.4652	0	
2009-06-11	64457.2700	6503.6794	9.91089290163965	
2009-06-12	65525.3200	7755.4558	8.44893216978943	
2009-06-13	61298.4800	9503.1016	6.45036563641496	
2009-06-14	24457.5700	10175.7505	2.40351510190821	
That's not quite the whole story, because there is an extra tab in the output lines.
This is because line 10 reads

Code: Select all

  redim preserve fields(4)
Changing line 10 to

Code: Select all

  redim preserve fields(3)
fixes this secondary bug.
The trial output then becomes

Code: Select all

2009-06-06	61383.9200	10529.8651	5.82950678067091
2009-06-07	42263.6000	10089.0255	4.18906662491833
2009-06-08	23442.0400	10904.0565	2.14984579362735
2009-06-09	22708.4600	6829.3108	3.32514666048
2009-06-10	.0000	6656.4652	0
2009-06-11	64457.2700	6503.6794	9.91089290163965
2009-06-12	65525.3200	7755.4558	8.44893216978943
2009-06-13	61298.4800	9503.1016	6.45036563641496
2009-06-14	24457.5700	10175.7505	2.40351510190821
without the extra tabs after the calculated field.

David

Re: VBScript runtime error in script filter called "field ma

Posted: Mon Oct 31, 2011 10:21 pm
by DFH
NB. To show the difference between the above outputs, you need to use SELECT ALL in the code section headings.

Re: VBScript runtime error in script filter called "field ma

Posted: Wed Nov 02, 2011 12:17 pm
by DataMystic Support
Thanks David - updated for next release. It looks like the vbscript array is now zero-based not 1-based.

Re: VBScript runtime error in script filter called "field ma

Posted: Fri Nov 04, 2011 11:02 pm
by DFH
I think it always was zero based as regards the indexing, but I guess that the redimensioning statement has been made more consistent for dynamic arrays.

I have the O'Reilly book VBScript in a Nutshell in front of me.

Have you checked whether any other script filters had the same problem?

David

Re: VBScript runtime error in script filter called "field ma

Posted: Mon Nov 07, 2011 7:53 am
by DataMystic Support
I've checked through all the filters in the vbscript folder manually and no other problems were found.