Script Request: Log UserName, IP Address and ShareSize

Which hub software is the best? Where can I find script XXX? Discuss it here...(no, this is not for advertising your hub...)

Moderator: Moderators

Locked
styler
Posts: 6
Joined: 2003-04-16 23:54

Script Request: Log UserName, IP Address and ShareSize

Post by styler » 2003-04-17 00:32

I'd like to see a script along the same lines as the one that ButterflySoul wrote in http://dcplusplus.sourceforge.net/forum ... php?t=1372 to log attempts.

Hope BS (haha) doesn't mind, here's that script's code (v.3 :D ):

Code: Select all

Option Explicit 
Dim LogAtpt, LogSucc 

' --------------------------- 
Sub Main 
' --------------------------- 
   Dim fso 
   Set fso = CreateObject("Scripting.FileSystemObject") 
   If Not fso.FolderExists(".\ConnectLogs") Then fso.CreateFolder(".\ConnectLogs") 
   LogAtpt = 1 ' **** (Connection Attempts Loging / 1=Enable / 0=Disable) 
   LogSucc = 1 ' **** (Connection Successful Loging / 1=Enable / 0=Disable) 
End Sub 

' --------------------------- 
Sub AttemptedConnection (RemoteHostIP) 
' --------------------------- 

   If LogAtpt = 0 Then Exit Sub 
   Call WriteLog("Attempt from " &RemoteHostIP) 

End Sub 

' --------------------------- 
Sub NewUserConnected (curUser) 
' --------------------------- 

   If LogSucc = 0 Then Exit Sub 
   Call WriteLog("Success from " &curUser.IP() &" as user " &curUser.sName) 

End Sub 


' --------------------------- 
Sub OpConnected (curUser) 
' --------------------------- 

' *** This event is fired when a registered user logs in (not necessarily an OP) 
   If LogSucc = 0 Then Exit Sub 
   If Not curUser.bOperator Then Call NewUserConnected(curUser) : Exit Sub 
   Call WriteLog("Success from " &curUser.IP() &" as OP " &curUser.sName) 

End Sub 

' --------------------------- 
Sub WriteLog(sEntry) 
' --------------------------- 

   Dim fso, objLogFile 
   Dim sFileName 
   Set fso = CreateObject("Scripting.FileSystemObject") 

   sFileName = ".\ConnectLogs\" &Year(Date) 
      If Month(Date) > 9 Then 
         sFileName = sFileName &Month(Date) 
      Else 
         sFileName = sFileName &"0" &Month(Date) 
      End If 
      If Day(Date) > 9 Then 
         sFileName = sFileName &Day(Date) &".log" 
      Else 
         sFileName = sFileName &"0" &Day(Date) &".log" 
      End If 

   frmHub.DoEventsForMe 

   Set objLogFile = fso.OpenTextFile(sFileName, 8, True) 
      With objLogFile 
         .WriteLine Time & " : " &sEntry 
         .Close 
      End With 
End Sub
But, not only able to log attempts, but log successfully connected user's UserName, IP Address and ShareSize. The option or ability to store other interesting and available data would be nice too.

Thanks a bunch.

styler :shock:

Marvin
Posts: 147
Joined: 2003-03-06 06:56
Location: France
Contact:

Re: Script Request: Log UserName, IP Address and ShareSize

Post by Marvin » 2003-04-17 06:48

Here come some modifications and addings to perform share size logging.

Code: Select all

[...]

' --------------------------- 
Sub NewUserConnected (curUser) 
' --------------------------- 

   If LogSucc = 0 Then Exit Sub 
   Call WriteLog("Success from " &curUser.IP() &" as user " &curUser.sName&" sharing "&shared(curUser)&" B.") 

End Sub 

' --------------------------- 
Sub OpConnected (curUser) 
' --------------------------- 

' *** This event is fired when a registered user logs in (not necessarily an OP) 
   If LogSucc = 0 Then Exit Sub 
   If Not curUser.bOperator Then Call NewUserConnected(curUser) : Exit Sub 
   Call WriteLog("Success from " &curUser.IP() &" as OP " &curUser.sName&" sharing "&shared(curUser)&" B.") 

End Sub 

Function shared(user)
    shared=afterlast(beforelast(user.sMyInfoString,"$"),"$")
end Function

Function BeforeLast(sFrom, sBeforeLast)    ' by TasMan
    If InStrB(1, sFrom, sBeforeLast) Then
        BeforeLast = Left(sFrom , InStrRev(sFrom, sBeforeLast) - 1)
    Else
        BeforeLast = vbNullString
    End If
End Function

Function AfterLast(sFrom, sAfterLast)        ' by TasMan
    If InStrB(1, sFrom, sAfterLast) Then
        AfterLast = Right(sFrom, Len(sFrom) - InStrRev(sFrom, sAfterLast) - (Len(sAfterLast) - 1))
    Else
        AfterLast = ""
    End If
End Function

[...]
styler wrote: But, not only able to log attempts, but log successfully connected user's UserName, IP Address
This is already done by the script.

Functions BeforeFirst and AfterLast taken from Tasman in this thread

styler
Posts: 6
Joined: 2003-04-16 23:54

Post by styler » 2003-04-17 12:49

i don't know much about vb so bear with me :p

i think there's something wrong with:

Code: Select all

Function shared(user) 
        shared = afterlast(beforelast(user.sMyInfoString,"$"),"$")
end Function 
'cause i get an error 1010: expected Identifier.

and i was finally able to get it to print at least *something* by doing:

Code: Select all

 dim SharedBytes
   SharedBytes = 1000
 '  SharedBytes = &Shared(curUser)
   Call WriteLog("Success from " &curUser.IP() &" as user " &curUser.sName &" sharing " &SharedBytes) 
as you can see above, I'm trying to call the function shared and store it to SharedBytes. I know I can probably pass that in the Call WriteLog().

so, any ideas how to get it to get the user's share saize?

styler :shock:

Marvin
Posts: 147
Joined: 2003-03-06 06:56
Location: France
Contact:

Post by Marvin » 2003-04-17 15:17

Many bugs in my code !!

1°/ The one triggering all the errors : shouldn't have used shared as an identifier (it is probably reserved).

2°/ The variable sMyInfoString is empty when NewUserConnected is fired. So, the script has to be modified to delay the writing of the log or to write a third line with the share size.

Marvin
Posts: 147
Joined: 2003-03-06 06:56
Location: France
Contact:

Post by Marvin » 2003-04-17 15:35

The new version (easy way) :

Code: Select all

Option Explicit
Dim LogAtpt, LogSucc

' ---------------------------
Sub Main
' ---------------------------
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   If Not fso.FolderExists(".\ConnectLogs") Then fso.CreateFolder(".\ConnectLogs")
   LogAtpt = 1 ' **** (Connection Attempts Loging / 1=Enable / 0=Disable)
   LogSucc = 1 ' **** (Connection Successful Loging / 1=Enable / 0=Disable)
End Sub


Sub DataArival (curUser, sCurData)
Dim aCommand
Dim sharedbytes
  
  If beforefirst(sCurData," ") = "$MyINFO" then
     sharedbytes = afterlast(beforelast(sCurData,"$"),"$")
     Call WriteLog("User " &curUser.sName&" is sharing "&sharedbytes&" B.")
  end if
End sub



' ---------------------------
Sub AttemptedConnection (RemoteHostIP)
' ---------------------------

   If LogAtpt = 0 Then Exit Sub
   Call WriteLog("Attempt from " &RemoteHostIP)

End Sub

' ---------------------------
Sub NewUserConnected (curUser)
' ---------------------------

   If LogSucc = 0 Then Exit Sub
   Call WriteLog("Success from " &curUser.IP() &" as user " &curUser.sName)

End Sub


' ---------------------------
Sub OpConnected (curUser)
' ---------------------------

' *** This event is fired when a registered user logs in (not necessarily an OP)
   If LogSucc = 0 Then Exit Sub
   If Not curUser.bOperator Then Call NewUserConnected(curUser) : Exit Sub
   Call WriteLog("Success from " &curUser.IP() &" as user " &curUser.sName)

End Sub

' ---------------------------
Sub WriteLog(sEntry)
' ---------------------------

   Dim fso, objLogFile
   Dim sFileName
   Set fso = CreateObject("Scripting.FileSystemObject")

   sFileName = ".\ConnectLogs\" &Year(Date)
      If Month(Date) > 9 Then
         sFileName = sFileName &Month(Date)
      Else
         sFileName = sFileName &"0" &Month(Date)
      End If
      If Day(Date) > 9 Then
         sFileName = sFileName &Day(Date) &".log"
      Else
         sFileName = sFileName &"0" &Day(Date) &".log"
      End If

   frmHub.DoEventsForMe

   Set objLogFile = fso.OpenTextFile(sFileName, 8, True)
      With objLogFile
         .WriteLine Time & " : " &sEntry
         .Close
      End With
End Sub 


' String functions by TasMan (http://dcplusplus.sourceforge.net/forum/profile.php?mode=viewprofile&u=15)

Function BeforeFirst(sIn, sFirst)
    BeforeFirst = LeftB(sIn, InStrB(1, sIn, sFirst) - 1)
End Function

Function BeforeLast(sFrom, sBeforeLast)
    If InStrB(1, sFrom, sBeforeLast) Then
        BeforeLast = Left(sFrom , InStrRev(sFrom, sBeforeLast) - 1)
    Else
        BeforeLast = vbNullString
    End If
End Function

Function AfterLast(sFrom, sAfterLast)
    If InStrB(1, sFrom, sAfterLast) Then
        AfterLast = Right(sFrom, Len(sFrom) - InStrRev(sFrom, sAfterLast) - (Len(sAfterLast) - 1))
    Else
        AfterLast = ""
    End If
End Function
Problem (is it one ?) is : the log is updated each time a user sends his infostring.

styler
Posts: 6
Joined: 2003-04-16 23:54

Post by styler » 2003-04-17 15:41

bah, i've been trying just about all night, pulling snippets of code from here and there, and i just can't seem to get it to work. i would really appreciate if you, or somebody, who knows this stuff could help me out.

styler :shock:

styler
Posts: 6
Joined: 2003-04-16 23:54

Post by styler » 2003-04-17 15:45

thank you thank you thank you

( i hadn't refreshed the topic by the time you replied )

this is perfect

styler :shock:

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-18 03:26

Ok, since this is originally my script, I thought it would be in order to post a lill modification to make it sound more like mine =p
I never Dim stuff in DataArival, since it's triggered so damn often =)

So the «BS» (!) version of :

Code: Select all

Sub DataArival (curUser, sCurData) 
Dim aCommand 
Dim sharedbytes 
  
  If beforefirst(sCurData," ") = "$MyINFO" then 
     sharedbytes = afterlast(beforelast(sCurData,"$"),"$") 
     Call WriteLog("User " &curUser.sName &" is sharing "&sharedbytes&" B.") 
  end if 
End Sub
would be :

Code: Select all

' --------------------------- 
Sub DataArival (curUser, sCurData) 
' --------------------------- 
If RightB(sCurData, 8) = "$MyI" Then Call WriteLog("User " &curUser.sName&" is sharing " &curUser.iBytesShared &" B.") 
End Sub
It also lets you get rid of the lill afterfirst and beforelast and afterlast functions =)

Let know if you'd like an extra check to write the sharesize in the log only once per user unless their share size changes. It will avoid having several entries per user, since the clients automatically re-send their $MyINFO string every now and then, whether the share size changed or not.

Regards,
Soul (or BS, if you really must =p)
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

styler
Posts: 6
Joined: 2003-04-16 23:54

Post by styler » 2003-04-18 13:22

Cool Soul (sorry.. BS just worked for me at the time)

but curUser.iBytesShared just doesn't seem to ever want to work for me. any ideas why not?

styler :shock:

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-18 14:03

You make me proud ButterflySoul....somebody actually used a parsing byte function besides me. This is great!

curUser.iBytesShared will always equal 0 in the NewUser/OpConnected subs. You must find the $MyINFO string using the DataArival sub.
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

HaArD
Posts: 147
Joined: 2003-01-04 02:20
Location: Canada http://hub-link.sf.net
Contact:

Post by HaArD » 2003-04-18 14:48

Can some one explain why this:

Code: Select all

If RightB(sCurData, 8) = "$MyI" Then Call WriteLog("User " &curUser.sName&" is sharing " &curUser.iBytesShared &" B.") 
isn't this:

Code: Select all

If LeftB(sCurData, 8) = "$MyI" Then Call WriteLog("User " &curUser.sName&" is sharing " &curUser.iBytesShared &" B.") 
I don't fully understand BYTE functions but shouldn't we still be looking at the LEFT of sCurData.

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-18 15:33

Oh well byte functions work basically the same way except for numbering issues.....

Yes it should have been a left, not a right....I never noticed that - I was just too stunned I guess about seeing my suggestions being followed for once.

(btw in the script above uses my string functions - they've been updated (check the link supplied in the script) )
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-18 20:22

Yes, indeed, should have been LeftB *blush* I still get confuzled with where the left and the right are sometimes, eventhough it's usualy when I give directions; not so often in scripting =p
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-18 21:03

Open script collection (any registered SF user can submit scripts)
Since you like Byte functions, I'm sure you'll love this very optimized version of your "Anti-MLDonkey" script :

Code: Select all

Dim aInfo
Sub DataArival(curUser, sCurData)
If RightB(sCurData, 8) = "$MyI" Then
   aInfo = Split(MidB(sCurData, 25),Chr(36))
   If AscW(aInfo(4))=32 Then objUser.Kick()
End If
End Sub
It's a "stand-alone" version of the anti-MLDC code I use in Zeus before analysing the $MyInfo string further; and it's much faster than the one currently in your collection (more reliable too, since it's not based on their name). I'd have posted it myself if I could get my hands on my password again =p
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-18 21:05

Of course, once again, it should LeftB(sCurData, 8) *grumbles* *wishes for an "edit post" button somewhere on these forums* *flails*
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-18 21:10

Hmmmmm I think I've been outmaneuvered....but not quite!

Code: Select all

Dim aInfo 
Sub DataArival(curUser, sCurData) 
If RightB(sCurData, 8) = "$MyI" Then 
   aInfo = Split(MidB(sCurData, 25),Chr(36)) 
   If AscW(aInfo(4))=32 Then objUser.Kick() 
End If 
End Sub
could be improved to this

Code: Select all

Sub DataArival(curUser, sCurData) 
  If LeftB(sCurData, 8) = "$MyI" Then 
    Dim aInfo

    aInfo = Split(MidB(sCurData, 25), "$") 
    If AscW(aInfo(4))=32 Then curUser.Kick
  End If 
End Sub
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-18 21:11

damn I was too slow
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-18 21:21

While a MyInfo string doesn't come too often, DataArival is triggered a lot, so it's usualy a bad idea to Dim stuff (explicitely or implicitely) in DataArival (which is why I had declared it globaly, which is still better than diming and killing it each time a command arrives to the hub). If you want to Dim aInfo only when needed, then

Code: Select all

Option Explicit

' ---------------------------
Sub DataArival(curUser, sCurData)
' ---------------------------
If LeftB(sCurData, 8) = "$MyI" Then Analysis MidB(sCurData, 25)
End Sub

' ---------------------------
Sub Analysis(sInfo)
' ---------------------------
Dim aInfo
aInfo = Split(sInfo,Chr(36)) 
If AscW(aInfo(4))=32 Then objUser.Kick()
End Sub
will be faster =)
(yes, I'm an optimisation freak too =p)
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-18 21:34

But you lose some of the gain by calling the other sub....I hate variants - they suck up so much ram.
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-18 21:48

Yes, they do =/

Btw, in your fix, you replaced the Chr(36) in the Split by a "$"
Coming from you, it caught my attention =p Is it faster to use strings even when they consist of a single character ? I used the Chr function a lot in Zeus precisely because I assumed Chr was faster in that specific case. I guess I was wrong ? =/
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-18 22:33

I just realised that you placed the Dim after the If; instead of its usual position at the begining of the sub :

Code: Select all

Sub DataArival(curUser, sCurData) 
  If LeftB(sCurData, 8) = "$MyI" Then 
    Dim aInfo 
(...)
so my comment about aInfo being dimed each time the sub is triggered was maybe not obvious.

Try this simple script :

Code: Select all

Option Explicit
Sub DataArival(curUser, sCurData) 
Dim x
x = 1
  If x = 2 Then
    Dim y
  Else
    Dim y
  End if
End Sub
and you'll understand (maybe it works in classic VB, but it VBS it should generate an error).
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-18 23:12

If you prefer a real runtime error rather than some message from the syntax checker (or whatever it's called) you can use :

Code: Select all

Option Explicit
Sub DataArival(curUser, sCurData)
On Error Resume Next
Dim x
x = 1
  If x = 1 Then
    y = 1
  End If
ColUsers.ItemByName("TasMan").PrivateMessage "Debug", "y is equal to : " &y
ColUsers.ItemByName("TasMan").PrivateMessage "Debug", vbCrLf &"Error Number : " &Err.Number &vbCrLf &"Error Description : " &Err.Description &vbCrLf &"Error Source : " &Err.Source
End Sub
You will get a PM from debug saying :
<Debug>
Error Number : 500
Error Description : Variable is undefined
Error Source : Microsoft VBScript runtime error

(because one of effects of the Option Explicit statement is that you must explicitely Dim your variables before using them; and y wasn't Dimed)

-----

If you then run :

Code: Select all

Option Explicit
Sub DataArival(curUser, sCurData)
On Error Resume Next
Dim x
x = 1
  If x = 2 Then
    Dim y
  Else
    y = 1
  End If
ColUsers.ItemByName("TasMan").PrivateMessage "Debug", "y is equal to : " &y
ColUsers.ItemByName("TasMan").PrivateMessage "Debug", vbCrLf &"Error Number : " &Err.Number &vbCrLf &"Error Description : " &Err.Description &vbCrLf &"Error Source : " &Err.Source
End Sub
You'll get 2 PMs from debug, saying :
<Debug> y is equal to : 1
<Debug>
Error Number : 0
Error Description :
Error Source :

(Still under an Option Explicit statement, this time there is no error. Eventhough x was never equal to 2, y was still Dimed =)
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-19 04:01

Well the Chr() function just returns a string (well...a variant - who cares), and it's inefficant to call that every time. Strings ARE slower than numbers, but with Chr(), it adds an extra step :P .
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-19 04:07

oh and the comment about dimming.....

I know that will generate an error. You can't have a variable dimmed twice in the same SUB or FUNCTION. However you can reuse it in different subs.

True, this isn't possible

Code: Select all

Sub Me(y)
  If y Then
    Dim x
  Else
    Dim x
  End If
End If
but this is

Code: Select all

Sub Me(y)
  Dim x

  If y Then x = Abs(y)
End If

Sub You(x)
  Dim x

  If Not y Then x = Not y
End Sub
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-19 06:46

*off to replace all his Chr() with strings then* =p

-----

And, yes, you can reuse them in two different subs, of course =p My point was just that

Code: Select all

Dim x
  If <condition> Then
    Dim y
    <do stuff>
  End If
is absolutely the same as

Code: Select all

Dim x, y
  If <condition> Then
  <do stuff>
  End If
Wether the condition is true or not, y will be dimed no matter what, in both cases =) My post above with the runtime error example was probably a bit more representative of this. Essentially, it was to explain why

Code: Select all

If LeftB(sCurData, 8) = "$MyI" Then 
  Dim aInfo
  (...)
End If
in your code example would Dim aInfo no matter what on each execution of DataArival, eventhough the Dim instruction is inside the If statement =)

-----

So let's see... If we swap the Chr for its string value, the super-optimized-best-of-both-scripters-remix-can't-do-faster version of this would be :

Code: Select all

Option Explicit 

' --------------------------- 
Sub DataArival(curUser, sCurData) 
' --------------------------- 
If LeftB(sCurData, 8) = "$MyI" Then Analysis MidB(sCurData, 25) 
End Sub 

' --------------------------- 
Sub Analysis(sInfo) 
' --------------------------- 
Dim aInfo 
aInfo = Split(sInfo,"$") 
If AscW(aInfo(4))=32 Then objUser.Kick() 
End Sub
?
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-19 07:40

Hmmmmm are you sure about it always being dimmed? The variable can't be accessed outside the if statement, and I would think that the VBScript interprets line by line sort of thing (so it wouldn't reach the Dim statement if the If was false)
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-19 07:58

[quote=ButterflySoul]
Wether the condition is true or not, y will be dimed no matter what, in both cases =) My post above with the runtime error example was probably a bit more representative of this. Essentially, it was to explain why

Code: Select all

 
If LeftB(sCurData, 8) = "$MyI" Then 
  Dim aInfo 
  (...) 
End If 
in your code example would Dim aInfo no matter what on each execution of DataArival, eventhough the Dim instruction is inside the If statement =)
[/quote]

Nope, I think wrong wrong. I just did a test in both VB6 and VBScript. It takes longer to execute this code :

Code: Select all

Sub Test()
  Dim i

  For i = 0 To 15000
    Dim a()
    ReDim a(i)
  Next
End Sub
than it does

Code: Select all

Sub Test()
  Dim i, b

  b = False

  For i = 0 To 15000
    If b Then
      Dim a()
      ReDim a(i)
    End If
  Next
End Sub
by quite a few seconds. On the first sub, I could hear my CPU making a funny noise :P . The first takes far too much longer than the second to be some error. The second is much faster because b is false, and the a never gets dimmed.
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-19 08:09

Wait a minute.....if you are right, that means it would declare the a(), but wouldn't execute the ReDim. Damn that was flawed....

Anyways I just did a real test, with 999999 rounds. It 3 seconds to execute both statements (and crashed when I added another 9)...sufice to say, it negliable, no matter who is right.
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-19 08:10

Sigh another monologue that's me....I said "real" test. Ummm that should have been new test. Otherwise it sounds like I didn't do a test beforehand (I wish I could edit my posts....hmmmm perhaps I can somehow get on the mod team :) )
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-19 08:30

Hmmmmm are you sure about it always being dimmed? The variable can't be accessed outside the if statement, and I would think that the VBScript interprets line by line sort of thing (so it wouldn't reach the Dim statement if the If was false)
Well, look back at my first runtime test (maybe you missed it, since I posted it shortly after the "double declaration" example, and you answered shortly after that =)

y is dimed within the If loop, but used outside of it, in both code pieces =)
It even outputs nicely in the PM sent by the script. So yes, the variable can be accessed outside of the If statement =)
And yes, it is always dimed, too, since in the examples I gave, x=1, and put the Dim y under an "If x=2"

I'm pretty sure you missed that specific post, scroll up a bit in the thread =) (or if you didn't miss it, I'm not sure why you'd think it's not valid ? *ponders*)
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-19 09:01

Well I do have a bad habit of glossing over long threads :)

Are you talking about this one?

Code: Select all

Option Explicit 
Sub DataArival(curUser, sCurData) 
On Error Resume Next 
Dim x 
x = 1 
  If x = 2 Then 
    Dim y 
  Else 
    y = 1 
  End If 
ColUsers.ItemByName("TasMan").PrivateMessage "Debug", "y is equal to : " &y 
ColUsers.ItemByName("TasMan").PrivateMessage "Debug", vbCrLf &"Error Number : " &Err.Number &vbCrLf &"Error Description : " &Err.Description &vbCrLf &"Error Source : " &Err.Source 
End Sub
Hmmm well that looks kind of "shady" to me. I believe the interpreter searches for a Dim statement, and if it's found anywhere, then it is tricked into believing it was really dimmed. After the If statement, the values of any variables declared within are erased. If you don't declare your variables, the first time you use it, it get declared, right? Since y was declared in the If statement, the Option Explicit decided it was fine.

I wonder if that makes sense....I'm too afraid to read it.

But after all this....it doesn't really matter! After one million loops of declaring, it took 3 seconds, each one. If the hub that the computer is running on can't handle that, it shouldn't be running a hub at all. The performance gain/loss/difference is basically negliable.
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-19 09:13

Are you talking about this one?
Yes, that post =)


If you don't declare your variables, the first time you use it, it get declared, right?
Yes and no :
- By default, yes, it gets declared. It's called implicit declaration.

- Under an "Option Explicit" statement, no, it doesn't get declared. It generates and error instead (because only explicit declarations are allowed under such a statement. The first script example in the same post was showing this).

It also saves a bunch of resources to run under Option Explicit (according to Microsoft anyway) precisely because it doesn't expect to run into implicitely declared variables (if I understood it properly, on top of all this, the memory use is a bit optimised too, but since the 2 types of variants in VBS use respectively 16 and 22 bytes, you still won't come anywhere close to classic VB "performance").

*ponders* err.. I guess we hijacked the thread big time, didn't we ? =p
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-19 09:25

After the If statement, the values of any variables declared within are erased
Then you wouldn't get a PM from debug telling you :
<Debug> y is equal to : 1

But instead you'd get
<Debug> y is equal to : (0/Null/Blank String/whatever)
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-19 09:26

*ponders* err.. I guess we hijacked the thread big time, didn't we ? =p
Yup we did. I think we'll have many a fond memory together by the time summer arrives :P .

I don't care if I was wrong anymore....it takes too much effort to give a good answer :) . I hate fighting losing battles....and there's no save points in real life.

I never heard the one about Option Explicit.....me thinks I'll have to take a look (but it's a good habit to get into anyways whether or not it does increase performance)[/code]
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

ButterflySoul
Posts: 210
Joined: 2003-01-23 17:24
Location: Nevada
Contact:

Post by ButterflySoul » 2003-04-19 09:52

Awww.. You were not wrong, your way is working fine as well =) It's just that back when Zeus was just 10 lines long, I had written a bunch of different scripts to test the whole "variable declaration inside an If statement" thing in VBS, and after all the optimisations you taught me, I had an oportunity to give you a hand and optimise your code =)

For the Option Explicit statement, think of it like managing a supermarket. If you know in advance how many lettuce you're going to sell next week, you'll order them and manage them in a very different and much more efficient way than if you're trying to make a day-by-day guess at it as the week goes by.
That's exactly what Option Explicit does. It doesn't need to execute the Sub to know how many variables (and memory) it will need along the way, and the memory assignement will work a bit differently -that is, if things do indeed work the way Microsoft says they do; but apparently they do- =)
[CoZ] Children of Zeus
-----
Shadows DC Hub - VBS and JS scripting at their best

TasMan
Posts: 196
Joined: 2003-01-03 08:31
Location: Canada
Contact:

Post by TasMan » 2003-04-19 09:58

The only rule you ever need to know when programming : Never take Microsoft's words at face value. Read the Wheel of Time (An Aes Sedia always tells the truth, but not always the truth you may think it is....).

Where did that come from...? Too much reading lately.....
Shadows Direct Connect Hub - Taking away the light from NMDCH, leaving only shadows.....

Locked