您的位置:程序门 -> vb -> 基础类



vb对txt文件内容操作并纪录到数组问题~


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vb对txt文件内容操作并纪录到数组问题~
发表于:2007-02-10 01:02:39 楼主
我现在有个txt文件,格式为:

10000,10
10001,20
10002,30
10000,20
10001,40
10000,10

要把这些数据,前一项相同的则后一项累计相加,再把结果纪录到二维数组。上面数据操作后的结果应该为[10000,40],[10001,60],[10003,30]。该如何实现呢?

发表于:2007-02-10 08:44:531楼 得分:0
'加个模块  
'定义
option   explicit

type   typtemp
        key   as   string
        dblvalue   as   double
end   type
public   temp()   as   typtemp
'画面
private   sub   command1_click()
        dim   strline   as   string
        dim   i   as   integer
        dim   iidx   as   integer
        i   =   0
        open   "test.txt "   for   input   as   #1
        do   while   not   eof(1)
                line   input   #1,   strline
                if   trim(strline)   <>   " "   then
                      iidx   =   getindex(split(strline,   "; ")(0))
                      temp(iidx).key   =   split(strline,   "; ")(0)
                      temp(iidx).dblvalue   =   temp(iidx).dblvalue   +   split(strline,   "; ")(1)
                end   if
        loop
        close   #1
end   sub
private   function   getindex(strkey   as   string)
        dim   i   as   integer
        dim   imax   as   integer
on   error   goto   iniarray
        imax   =   ubound(temp)
        for   i   =   0   to   imax
                if   temp(i).key   =   strkey   then
                        getindex   =   i
                        exit   function
                end   if
        next
        redim   preserve   temp(imax   +   1)
        getindex   =   imax   +   1
        exit   function
iniarray:
        redim   temp(0)
        getindex   =   0
end   function
发表于:2007-02-11 10:57:242楼 得分:0
判断   chr(13)
发表于:2007-02-11 13:22:473楼 得分:0
试试这个行不行:

        dim   col   as   new   collection
        dim   stemp   as   string
        dim   arr()   as   string
        dim   i   as   integer
        dim   b   as   boolean
       
        b   =   false
        open   app.path   &   "\t.txt "   for   input   as   #1
                while   not   eof(1)
                        line   input   #1,   stemp
                        if   trim(stemp)   <>   vbnullstring   then
                                stemp   =   strconv(stemp,   vbnarrow)
                                for   i   =   1   to   col.count
                                        if   trim(split(col.item(i),   ", ")(0))   =   trim(split(stemp,   ", ")(0))   then
                                                stemp   =   trim(split(stemp,   ", ")(0))   &   ", "   _
                                                        &   (val(split(col.item(i),   ", ")(1))   +   val(split(stemp,   ", ")(1)))
                                                col.remove   i
                                                col.add   stemp
                                                b   =   true
                                                exit   for
                                        end   if
                                next
                                if   b   =   false   then
                                        col.add   stemp
                                end   if
                                b   =   false
                        end   if
                wend
        close   #1
       
        redim   arr(col.count)
        for   i   =   1   to   col.count
                arr(i)   =   col.item(i)
                debug.print   arr(i)
        next

发表于:2007-02-11 13:29:034楼 得分:0
哦还要加一句
set   col=nothing
发表于:2007-04-10 21:22:035楼 得分:0
vbman2003(家人)   给的代码测试好用
beal_p()   的测试出错,可能是我太菜
发表于:2007-04-13 08:28:276楼 得分:0
哦,   split(strline,   "; ")(0)
这边   "; "改为你文本里的分割符,   1楼所示为   ", "


快速检索

最新资讯
热门点击