mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2008-11-12, 07:40   #12
Patrick123
 
Patrick123's Avatar
 
Jan 2006
JHB, South Africa

157 Posts
Default

Quote:
Originally Posted by IronBits View Post
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
             UserName = arrMyTeamName(1)
        Else
             UserName = Team_Name
        End If

Regards
Patrick
Patrick123 is offline   Reply With Quote
Old 2008-11-12, 08:35   #13
IronBits
I ♥ BOINC!
 
IronBits's Avatar
 
Oct 2002
Glendale, AZ. (USA)

111310 Posts
Default

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
IronBits is offline   Reply With Quote
Old 2008-11-12, 12:12   #14
Patrick123
 
Patrick123's Avatar
 
Jan 2006
JHB, South Africa

157 Posts
Default

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
Patrick123 is offline   Reply With Quote
Old 2008-11-12, 13:51   #15
axn
 
axn's Avatar
 
Jun 2003

12AC16 Posts
Default

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)
axn is offline   Reply With Quote
Old 2008-11-12, 14:17   #16
Patrick123
 
Patrick123's Avatar
 
Jan 2006
JHB, South Africa

9D16 Posts
Default

Quote:
Originally Posted by axn1 View Post
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
Patrick123 is offline   Reply With Quote
Old 2008-11-12, 14:42   #17
IronBits
I ♥ BOINC!
 
IronBits's Avatar
 
Oct 2002
Glendale, AZ. (USA)

100010110012 Posts
Default

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
IronBits is offline   Reply With Quote
Old 2008-11-12, 15:58   #18
IronBits
I ♥ BOINC!
 
IronBits's Avatar
 
Oct 2002
Glendale, AZ. (USA)

21318 Posts
Default

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
IronBits is offline   Reply With Quote
Old 2008-11-27, 03:02   #19
WraithX
 
WraithX's Avatar
 
Mar 2006

23·59 Posts
Default

Quote:
Originally Posted by IronBits View Post
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
    UserName = Team_Name
  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
WraithX is offline   Reply With Quote
Reply

Thread Tools


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

All times are UTC. The time now is 00:52.

Sat Nov 28 00:52:36 UTC 2020 up 78 days, 22:03, 3 users, load averages: 1.41, 1.31, 1.26

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.