mersenneforum.org vbscript help
 User Name Remember Me? Password
 Register FAQ Search Today's Posts Mark Forums Read

2008-11-12, 07:40   #12
Patrick123

Jan 2006
JHB, South Africa

157 Posts

Quote:
 Originally Posted by IronBits All my filenames that I parse end with .org.txt so I'm making copies of the original files to create the .txt and .csv files. I suppose if someone passed the filename Results.txt as an argument, this would fail...
Yes, I see that very likely you will end up opening your input file for output. The best may be to check if the output file exists first or if the two files are the same & give an error message.

The other piece of code:
Code:
        Team_Name = MyArray(1)
arrMyTeamName = Split(Team_Name,"_",-1,1)
if arrMyTeamName(0) = "gd" Then UserName = Team_Name End If
If arrMyTeamName(0) = "Free-DC" Then UserName = arrMyTeamName(1) End If
If arrMyTeamName(0) = "AMDUsers" Then UserName = arrMyTeamName(1) End If
If arrMyTeamName(0) = "ArsTechnica" Then UserName = arrMyTeamName(1) End If
If arrMyTeamName(0) = "XtremeSystems" Then UserName = arrMyTeamName(1) End If
If arrMyTeamName(0) = "Team_Italia" Then UserName = arrMyTeamName(1) End If
If arrMyTeamName(0) = "XGrubbersKickAss" Then UserName = arrMyTeamName(1) End If
If arrMyTeamName(0) = "Ukraine" Then UserName = arrMyTeamName(1) End If
If arrMyTeamName(0) = "Singapura" Then UserName = arrMyTeamName(1) End If
If arrMyTeamName(0) = "RaidersOfTheLostPrime" Then UserName = arrMyTeamName(1) End If
My suggestion is to try not to hardcode data in you program at all if you can help it, rather use something like this:
Code:
        Team_Name = MyArray(1)
arrMyTeamName = Split(Team_Name,"_",-1,1)
If UBound(arrMyTeamName) > 0 then
Else
End If

Regards
Patrick

 2008-11-12, 08:35 #13 IronBits I ♥ BOINC!     Oct 2002 Glendale, AZ. (USA) 3×7×53 Posts Thank you for your suggestions and ideas Patrick. I had to add another line to that hardcoded team mess, on the last line, so that UserNames that don't have Team_UserName will get captured. Code: Team_Name = MyArray(1) arrMyTeamName = Split(Team_Name,"_",-1,1) if arrMyTeamName(0) = "gd" Then UserName = Team_Name End If If arrMyTeamName(0) = "Free-DC" Then UserName = arrMyTeamName(1) End If If arrMyTeamName(0) = "AMDUsers" Then UserName = arrMyTeamName(1) End If If arrMyTeamName(0) = "ArsTechnica" Then UserName = arrMyTeamName(1) End If If arrMyTeamName(0) = "XtremeSystems" Then UserName = arrMyTeamName(1) End If If arrMyTeamName(0) = "Team_Italia" Then UserName = arrMyTeamName(1) End If If arrMyTeamName(0) = "XGrubbersKickAss" Then UserName = arrMyTeamName(1) End If If arrMyTeamName(0) = "Ukraine" Then UserName = arrMyTeamName(1) End If If arrMyTeamName(0) = "Singapura" Then UserName = arrMyTeamName(1) End If If arrMyTeamName(0) = "RaidersOfTheLostPrime" Then UserName = arrMyTeamName(1) End If UserName = Team_Name Can we open another file called Teams.dat perhaps, load that into an array, close file. Then use the arrTeams to see if any match arrMyTeamName(0)? I can't remember the code to easily do that efficiently and cleanly. I'll do a better job of file handling to ensure we don't get input/output file name collisions. I ran a test run with what I have, and it completes over 40,000 lines in under a second, so I'm very pleased with the performance, just trying to be a better programmer and learn some tips and tricks along the way. BTW, this is my FIRST useful vbscript I've written. Thank you again for your time! :cheers: Last fiddled with by IronBits on 2008-11-12 at 08:40
 2008-11-12, 12:12 #14 Patrick123     Jan 2006 JHB, South Africa 2358 Posts It's only a pleasure IronBits. I notice that the extra line that you inserted: Code: UserName = Team_Name This should actually be placed before the conditionals as it overrides all you conditionals. Now instead of using an array and simply looping through it until the team name is found, we apply a very simplified hashing technique. What we do is to load the team.dat file & get the maximum occurrence of the first letter in Uppercase, as you can see, there are 2 teams that start with 'X' and another 2 with 'A' In this case, we then create a 2 dimensional array (26,2) and slot the teams into this array based on their starting letter e.g. Code: t(0,0) = "AMDUsers" t(0,1) = "ArsTechnica" ... t(23,0) = "XtremeSystems" t(23,1) = "XGrubbersKickAss" The array will have a lot of blank or unused slots, but where we lose on the swings, we make up on the merry-go-round i.e. memory vs speed Now it will be a simple matter of coding: Code: arrMyTeamName = Split(Team_Name,"_",-1,1) UserName = Team_Name ' no need to include 'gd' as this will take care of him. If UBound(arrMyTeamName) > 0 then idx := Asc(arrMyTeamName(0)) - 65 ' this will make 'A' = 0, 'B' = 1, etc. For col = 0 to UBound(t(idx)) If t(idx,col) = arrMyTeamName(0) then UserName = arrMyTeamName(1) Exit For ' break out, we don't need to check further. End If Next Col End If Regards Patrick
 2008-11-12, 13:51 #15 axn     Jun 2003 23·19·31 Posts There is a faster trick than the above hash (only faster in a scripting language) Code: C = ",AMDUsers,ArsTechnica,...,XtremeSystems,XGrubbersKickAss," If InStr("," & arrMyTeamName(0) & ",", C) > 0 Then ... End If C should ideally be declared as a Const at the top of the program. Easy to use and maintain. Should be fast. (PS:- Check the documentation for the correct usage of InStr)
2008-11-12, 14:17   #16
Patrick123

Jan 2006
JHB, South Africa

157 Posts

Quote:
 Originally Posted by axn1 There is a faster trick than the above hash (only faster in a scripting language) ...
Thanks axn1, I agree, for something of this caliber, that is by far the best way to handle it.

Regards
Patrick

 2008-11-12, 14:42 #17 IronBits I ♥ BOINC!     Oct 2002 Glendale, AZ. (USA) 45916 Posts More hot tips! Ok, here is what I did in those sections Const C = ",Free-DC,AMDUsers,ArsTechnica,..... if Left(str_CurrentLine,5) = "user=" Then MyArray = Split(str_CurrentLine,"=",-1,1) Team_Name = MyArray(1) UserName = Team_Name arrMyTeamName = Split(Team_Name,"_",-1,1) If InStr("," & arrMyTeamName(0) & ",", C) > 0 Then UserName = arrMyTeamName(1) End If But it doesn't strip the Team_ from UserName
 2008-11-12, 15:58 #18 IronBits I ♥ BOINC!     Oct 2002 Glendale, AZ. (USA) 3×7×53 Posts Ok, so this works Const strTeams = "",Free-DC,AMDUsers,ArsTechnica,..... if Left(str_CurrentLine,5) = "user=" Then MyArray = Split(str_CurrentLine,"=",-1,1) Team_Name = MyArray(1) arrMyTeamName = Split(Team_Name,"_",-1,1) StringPos = InStr(1, strTeams, arrMyTeamName(0), vbTextCompare) if StringPOS > 0 Then UserName = arrMyTeamName(1) Else UserName = Team_Name End If Maybe one of you can find a cleaner way to achieve the same thing? Last fiddled with by IronBits on 2008-11-12 at 16:17
2008-11-27, 03:02   #19
WraithX

Mar 2006

23×59 Posts

Quote:
 Originally Posted by IronBits Ok, so this works Const strTeams = "",Free-DC,AMDUsers,ArsTechnica,..... if Left(str_CurrentLine,5) = "user=" Then MyArray = Split(str_CurrentLine,"=",-1,1) Team_Name = MyArray(1) arrMyTeamName = Split(Team_Name,"_",-1,1) StringPos = InStr(1, strTeams, arrMyTeamName(0), vbTextCompare) if StringPOS > 0 Then UserName = arrMyTeamName(1) Else UserName = Team_Name End If Maybe one of you can find a cleaner way to achieve the same thing?
Well, If I understand what you are trying to do, then maybe the following would work for you:

Code:
If Left(str_CurrentLine,5) = "user=" Then
MyArray = Split(str_CurrentLine,"=",-1,1)
Team_Name = MyArray(1)

If InStr(1, Team_Name, "Team_", 1) > 0 Then
UserName = Right(Team_Name, Len(Team_Name)-5)
Else
End If
End If
One place I've found to be a valuable resource for vbscript is here:
http://www.w3schools.com/VBscript/vb..._functions.asp

 Similar Threads Thread Thread Starter Forum Replies Last Post IronBits Programming 0 2008-12-06 09:20

All times are UTC. The time now is 03:57.

Sat Oct 24 03:57:17 UTC 2020 up 44 days, 1:08, 1 user, load averages: 1.49, 1.29, 1.27