HI,

I have the following code which works fine except for when a value(s) is
null at end of record when it is read. For example, when a csv has 7
possible fields. But most of the records don't have the 7th field. My
function will place a comma at end of record, but if it isn't the last field
as noted by header I get a subscript error.

The first two work fine because you see it has a comma when the field was
blank or it has all 7 fields. But the second and third don't.

Example records from csv:
header1,header2,header3,header4,header5,header6,header7
testa,8,"testa,testa",01.66,deptname0,6,Y
testt,1,"tester,test",01.55,deptname1,5,
testm,2,"mine, test",01.56,deptname2,68
testy,,,TERMINATED,

code:
do until SourceFile.AtEndOfStream
SourceFields = SplitCSV(SourceFile.ReadLine)
wscript.stdout.write SourceFields(0) & " " & SourceFields(1) & " " &
SourceFields(2) & " " & SourceFields(3) & " " & SourceFields(4) & " " &
SourceFields(5) & " " & SourceFields(6) &vbcrlf '''''remove
loop

function SplitCSV(ByRef strLine)
const Quote = """"
const Quote2 = """"""
const Comma = ","

dim arrFields, blnIgnore, intFieldCount, intCursor, intStart, strChar,
strValue

if (len(trim(strLine)) = 0) then
SplitCSV = array()
exit function
end if

blnIgnore = false
intFieldCount = 0
intStart = 1
arrFields = array()

strLine = strLine & ","

for intCursor = 1 to len(strLine)
strChar = mid(strLine,intCursor,1)
select case strChar
case QUOTE
blnIgnore = not blnIgnore
case Comma
if (not blnIgnore) then
reDim preserve arrFields(intFieldCount)
if (intCursor - intStart > 0) then
strValue = mid(strLine,intStart,intCursor - intStart)
if (left(strValue, 1) = Quote) then
arrFields(intFieldCount) =
replace(mid(strValue,2,len(strValue)-2),Quote2,Quote)
else
arrFields(intFieldCount) = strValue
end if
else
arrFields(intFieldCount) = ""
end if
intFieldCount = intFieldCount + 1
intStart = intCursor + 1
end if
end select
next

SplitCSV = arrFields
end function



If a value read is not in csv as it should be based on the header I would
like to just add "" to the field. Where would I make this change in my
function?

Thank you in advance.

Re: Insert "" in array element when none in other array by Ayush

Ayush
Thu Mar 01 18:57:37 CST 2007

RdS wrote ::
> HI,

> I have the following code which works fine except for when a value(s) is
> null at end of record when it is read. For example, when a csv has 7
> possible fields. But most of the records don't have the 7th field. My
> function will place a comma at end of record, but if it isn't the last field
> as noted by header I get a subscript error.

> The first two work fine because you see it has a comma when the field was
> blank or it has all 7 fields. But the second and third don't.


do until SourceFile.AtEndOfStream
SourceFields = SplitCSV(SourceFile.ReadLine)
Flds=""
For Each fld in SourceFields : Flds=Fld & fld & " " : Next
Wscript.StdOut.Write Flds
loop

function SplitCSV(ByRef strLine)
...
end function





Good Luck, Ayush.
--
Scripting Home : http://snipurl.com/Scripting_Home

Re: Insert "" in array element when none in other array by RdS

RdS
Thu Mar 01 19:15:05 CST 2007

Thank you again Ayush. I'll give it a try.

"Ayush" <"ayushmaan.j[aatt]gmail.com" wrote:

> RdS wrote ::
> > HI,
>
> > I have the following code which works fine except for when a value(s) is
> > null at end of record when it is read. For example, when a csv has 7
> > possible fields. But most of the records don't have the 7th field. My
> > function will place a comma at end of record, but if it isn't the last field
> > as noted by header I get a subscript error.
>
> > The first two work fine because you see it has a comma when the field was
> > blank or it has all 7 fields. But the second and third don't.
>
>
> do until SourceFile.AtEndOfStream
> SourceFields = SplitCSV(SourceFile.ReadLine)
> Flds=""
> For Each fld in SourceFields : Flds=Fld & fld & " " : Next
> Wscript.StdOut.Write Flds
> loop
>
> function SplitCSV(ByRef strLine)
> ...
> end function
>
>
>
>
>
> Good Luck, Ayush.
> --
> Scripting Home : http://snipurl.com/Scripting_Home
>

Re: Insert "" in array element when none in other array by RdS

RdS
Thu Mar 01 19:55:05 CST 2007

hello,

tried what you listed but it returns no results. Also, is the array being
updated because I still need to use the array in rest of code.

thanks.

"Ayush" <"ayushmaan.j[aatt]gmail.com" wrote:

> RdS wrote ::
> > HI,
>
> > I have the following code which works fine except for when a value(s) is
> > null at end of record when it is read. For example, when a csv has 7
> > possible fields. But most of the records don't have the 7th field. My
> > function will place a comma at end of record, but if it isn't the last field
> > as noted by header I get a subscript error.
>
> > The first two work fine because you see it has a comma when the field was
> > blank or it has all 7 fields. But the second and third don't.
>
>
> do until SourceFile.AtEndOfStream
> SourceFields = SplitCSV(SourceFile.ReadLine)
> Flds=""
> For Each fld in SourceFields : Flds=Fld & fld & " " : Next
> Wscript.StdOut.Write Flds
> loop
>
> function SplitCSV(ByRef strLine)
> ...
> end function
>
>
>
>
>
> Good Luck, Ayush.
> --
> Scripting Home : http://snipurl.com/Scripting_Home
>

Re: Insert "" in array element when none in other array by Ayush

Ayush
Thu Mar 01 21:17:16 CST 2007

RdS wrote ::
> hello,

> tried what you listed but it returns no results.

There was a typo.. here is the correct script :

do until SourceFile.AtEndOfStream
SourceFields = SplitCSV(SourceFile.ReadLine)
Flds=""
For Each fld in SourceFields : Flds=Flds & fld & " " : Next
Wscript.StdOut.Write Flds & vbCrLf
loop

> Also, is the array being
> updated because I still need to use the array in rest of code.

No, only the variable Flds.


Good Luck, Ayush.
--
Script Center : http://microsoft.com/technet/scriptcenter/default.mspx

Re: Insert "" in array element when none in other array by RdS

RdS
Fri Mar 02 08:41:18 CST 2007

ah. I need to have the array with proper values because the array is used
all over the program for other things. :) With a single field I am sort of
back to where I started. :)

Is there no way to add "" to an element if it is null based on what is read?

Thanks again.

"Ayush" <"ayushmaan.j[aatt]gmail.com" wrote:

> RdS wrote ::
> > hello,
>
> > tried what you listed but it returns no results.
>
> There was a typo.. here is the correct script :
>
> do until SourceFile.AtEndOfStream
> SourceFields = SplitCSV(SourceFile.ReadLine)
> Flds=""
> For Each fld in SourceFields : Flds=Flds & fld & " " : Next
> Wscript.StdOut.Write Flds & vbCrLf
> loop
>
> > Also, is the array being
> > updated because I still need to use the array in rest of code.
>
> No, only the variable Flds.
>
>
> Good Luck, Ayush.
> --
> Script Center : http://microsoft.com/technet/scriptcenter/default.mspx
>

Re: Insert "" in array element when none in other array by RdS

RdS
Fri Mar 02 10:13:30 CST 2007

hi.

I found a solution that works perfectly. Thanks again. I added the below
to the function and a condition to loop.

if (bFirstRecordRead and (ubound(arrFields) < intNumHeaderFields -1)) then
redim preserve arrFields(intNumHeaderFields-1)


"RdS" wrote:

> ah. I need to have the array with proper values because the array is used
> all over the program for other things. :) With a single field I am sort of
> back to where I started. :)
>
> Is there no way to add "" to an element if it is null based on what is read?
>
> Thanks again.
>
> "Ayush" <"ayushmaan.j[aatt]gmail.com" wrote:
>
> > RdS wrote ::
> > > hello,
> >
> > > tried what you listed but it returns no results.
> >
> > There was a typo.. here is the correct script :
> >
> > do until SourceFile.AtEndOfStream
> > SourceFields = SplitCSV(SourceFile.ReadLine)
> > Flds=""
> > For Each fld in SourceFields : Flds=Flds & fld & " " : Next
> > Wscript.StdOut.Write Flds & vbCrLf
> > loop
> >
> > > Also, is the array being
> > > updated because I still need to use the array in rest of code.
> >
> > No, only the variable Flds.
> >
> >
> > Good Luck, Ayush.
> > --
> > Script Center : http://microsoft.com/technet/scriptcenter/default.mspx
> >