Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


how to split

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,48195
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> how to split
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 how to split - 6/12/2007 6:12:00 AM   
  sheepz


Posts: 247
Score: 2
Joined: 3/17/2006
From: Riverside the 909s
Status: offline
i have make/model names for computers that i'm trying to extract. 

what i have to work with:  HP Compaq dc510 Small Form Factor
what i want is to extract or split and grab from that: HP dc510

anyone have examples?  thanks!
 
 
Post #: 1
 
 RE: how to split - 6/12/2007 6:15:44 AM   
  ebgreen


Posts: 4946
Score: 31
Joined: 7/12/2005
Status: online
If the format of the string is always the same (6 words sperated by spaces) and the part that you want is always the same (the first andthird parts) then this will work:

strTest = "HP Compaq dc510 Small Form Factor"
arrParts = Split(strTest, " ")
WScript.Echo arrParts(0) & " " & arrParts(2)

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to sheepz)
 
 
Post #: 2
 
 RE: how to split - 6/12/2007 6:51:04 AM   
  sheepz


Posts: 247
Score: 2
Joined: 3/17/2006
From: Riverside the 909s
Status: offline
thanks ebgreen... i didn't know i could split with:  arrParts = Split(strTest, " ")   i thought i had to have a delimiter of some sort.  thank you again.  =)

(in reply to sheepz)
 
 
Post #: 3
 
 RE: how to split - 6/12/2007 6:59:54 AM   
  ebgreen


Posts: 4946
Score: 31
Joined: 7/12/2005
Status: online
You do have a delimiter. Space. " "

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to sheepz)
 
 
Post #: 4
 
 RE: how to split - 6/12/2007 9:44:04 AM   
  sheepz


Posts: 247
Score: 2
Joined: 3/17/2006
From: Riverside the 909s
Status: offline
i didn't know a space was a valid delimiter, thanks.  sorry another question.  for the make/models they come up like so:

Comp_Model = "HP d530 SV(79285)"
Comp_Model = "HP Compaq dc7600 Small Form Factor"

so i want to differenciate from these two types using Select but having problems.  i'm trying to use InStr but it keeps using the 2nd case statement even tho theres a "d5" in the string... 
                    
Select Case TypeModel
          Case InStr(Comp_Model, "d5")
                   arrParts = Split(Comp_Model, " ")  
                   Comp_Model_Split = arrParts(0) & "-" & arrParts(1)
          Case InStr(Comp_Model, "7")
                   arrParts = Split(Comp_Model, " ")  
                   Comp_Model_Split = arrParts(0) & ":" & arrParts(2)
          Case Else 
                   arrParts = Split(Comp_Model, " ")  
                   Comp_Model_Split = arrParts(0) & "=" & arrParts(1) & _ 
                   arrParts(2)
          End Select
                     
WScript.Echo comp_model_split

i'm using the select statement incorrectly.  it works if i revise to if statement

< Message edited by sheepz -- 6/12/2007 9:50:17 AM >

(in reply to ebgreen)
 
 
Post #: 5
 
 RE: how to split - 6/12/2007 10:01:14 AM   
  Skie

 

Posts: 58
Score: 0
Joined: 3/2/2006
Status: offline
You were hitting Case 2 because
TypeModal = 0
InStr(CompModel,"d5") was > 0
InStr(CompModel,"7") = 0


      

(in reply to sheepz)
 
 
Post #: 6
 
 RE: how to split - 6/12/2007 10:21:02 AM   
  sheepz


Posts: 247
Score: 2
Joined: 3/17/2006
From: Riverside the 909s
Status: offline
how does it figure that
InStr(CompModel,"d5") was > 0
how is this less than 0?

and
InStr(CompModel,"7") = 0
how is this equal to 0?

whats the logic?  does select automatically take the first case1 as >0 and the case2 =0.   would case3 be <0

do these lines:  InStr(CompModel, "7")  have values?  i would like a select statement that does not contain >0

(in reply to Skie)
 
 
Post #: 7
 
 RE: how to split - 6/12/2007 10:26:08 AM   
  ehvbs

 

Posts: 2169
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi sheepz,

perhaps looking at

  http://www.visualbasicscript.com/m_48170/tm.htm

will help you to understand "Select Case" better.

Good luck!

ehvbs

(in reply to sheepz)
 
 
Post #: 8
 
 RE: how to split - 6/12/2007 11:06:20 AM   
  sheepz


Posts: 247
Score: 2
Joined: 3/17/2006
From: Riverside the 909s
Status: offline
i used that as an example but still unsure how it works... i understand that Select Case tests each case, once a true case is found it executes, and the rest of the case statements are ignored... when skie showed me what i was doing wrong, i understood why i was getting the select case2.  what i'm a little fuzzy on is how does it know the value of this:

Case InStr(Comp_Model, "7") > 0

how is Case InStr(Comp_Model, "7") greater than 0?



2nd question:  i thought the name after the select statement is just the name of the statement, meaningless. 
Select Case TypeModel  
is the same as putting
Select Case TypeModel = 0     ?

i'm trying to learn how the Select case works by converting this if statement into select:

                    If InStr(Comp_Model, "5") Then
                       arrParts = Split(Comp_Model, " ") 
                       Comp_Model_Split = arrParts(0) & "-" & arrParts(1)
                      ElseIf InStr(Comp_Model, "dc7") Then
                       arrParts = Split(Comp_Model, " ") 
                       Comp_Model_Split = arrParts(0) & ":" & arrParts(2)
                      Else
                       arrParts = Split(Comp_Model, " ") 
                       Comp_Model_Split = arrParts(0) & "=" & arrParts(1) & _
                       arrParts(2)
                    End If
                   
      WScript.Echo Comp_Model_Split  

what would be the correct conversion? am i converting it correctly?

< Message edited by sheepz -- 6/12/2007 11:18:48 AM >

(in reply to ehvbs)
 
 
Post #: 9
 
 RE: how to split - 6/12/2007 1:15:37 PM   
  dm_4ever


Posts: 2635
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Maybe with a regular expression...


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to sheepz)
 
 
Post #: 10
 
 RE: how to split - 6/13/2007 1:30:21 AM   
  ebgreen


Posts: 4946
Score: 31
Joined: 7/12/2005
Status: online
If you really want to use Select Case, then you need to use something of a trick:

Select Case True
   Case InStr(Comp_Model, "5")
       'Do things here
   Case InStr(Comp_Model, "dc7")
       'Do things here
   Case Else
       'Do things here
End Select

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to dm_4ever)
 
 
Post #: 11
 
 RE: how to split - 6/13/2007 5:00:39 AM   
  Skie

 

Posts: 58
Score: 0
Joined: 3/2/2006
Status: offline
ebgreen, I don't think this would work:

      

True = -1 and InStr never returns -1 or true.  It will only return 0 for the string not found or and integer for the location within the string where the substring is found.

So, you need to add the > 0 for the first two cases:

      

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: how to split - 6/13/2007 5:12:43 AM   
  Skie

 

Posts: 58
Score: 0
Joined: 3/2/2006
Status: offline
quote:

ORIGINAL: sheepz

how is Case InStr(Comp_Model, "7") greater than 0?


InStr doesn't return true of false it returns 0 or the location of "7" within the string Comp_Model.

      

The difference between If InStr(sString,"a") and If InStr(sString,"a') = True is that the first statement looks for not false where false = 0 and the second statement looks for true where true = -1.


quote:

ORIGINAL: sheepz

2nd question:  i thought the name after the select statement is just the name of the statement, meaningless.


The variable after the select statement is the case you are looking for.  Since TypeModel is undeclared as anything else, it's 0.  Your code is basically saying when the following isn't in the string then run the case.  If you change it to Select Case True you're looking for a true statement.  Since we know that InStr does not return true, we need to create a statement for the InStr to generate a True.  We know if the InStr is greater than 0 then the string we are looking for is found.  Thus (InStr(Comp_Model,"5") > 0 ) = True.

Another option with the select case is we can do:

      
In this situation when Comp_Model = "HP Compaq dc7600 Small Form Factor" then the first case runs.  If it's anything else, then the second case runs.

I hope this helps to explain things.

(in reply to sheepz)
 
 
Post #: 13
 
 RE: how to split - 6/13/2007 5:31:34 AM   
  ebgreen


Posts: 4946
Score: 31
Joined: 7/12/2005
Status: online
You are correct Skie. That is what I get for just winging it intead of thinking it through and testing it. 

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Skie)
 
 
Post #: 14
 
 RE: how to split - 6/14/2007 4:41:30 AM   
  sheepz


Posts: 247
Score: 2
Joined: 3/17/2006
From: Riverside the 909s
Status: offline
Skie, thank you i really appreciate the detailed explanation... its much more clearier with the examples given, thanks. 

dm_4ever and ebgreen thank you for additional information, still trying to grasp expressions. thanks guys!


(in reply to ebgreen)
 
 
Post #: 15
 
 RE: how to split - 7/1/2007 6:55:40 PM   
  sheepz


Posts: 247
Score: 2
Joined: 3/17/2006
From: Riverside the 909s
Status: offline
i'm sorry another question about split.  lets say i have a string but i dont want the first array(0) because its always the Username, the rest is group membership which i do need.  is this the best way to ignore/bypass the username in this string and retrieve only the membership?

strTemp = "JSmith;Accounting;Purchasing;Approver"
arrTemp = Split(strTemp, ";")
For i = LBound(arrTemp) To UBound(arrTemp) - 1
   WScript.Echo arrTemp(i + 1)
Next


Is works but is this the actual way to go about it?

(in reply to sheepz)
 
 
Post #: 16
 
 RE: how to split - 7/2/2007 12:53:07 AM   
  dm_4ever


Posts: 2635
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
You could also do it like this...it is up to you...


strTemp = "JSmith;Accounting;Purchasing;Approver"
arrTemp = Split(strTemp, ";")
For i = 1 To UBound(arrTemp)
  WScript.Echo arrTemp(i)
Next


or backwards


strTemp = "JSmith;Accounting;Purchasing;Approver"
arrTemp = Split(strTemp, ";")
For i = UBound(arrTemp) To 1 Step - 1
  WScript.Echo arrTemp(i)
Next

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to sheepz)
 
 
Post #: 17
 
 RE: how to split - 7/2/2007 3:30:01 AM   
  sheepz


Posts: 247
Score: 2
Joined: 3/17/2006
From: Riverside the 909s
Status: offline
awesome dm, didn't know i could do that, much simpler, thanks.   =)

(in reply to dm_4ever)
 
 
Post #: 18
 
 RE: how to split - 7/2/2007 3:44:00 AM   
  ebgreen


Posts: 4946
Score: 31
Joined: 7/12/2005
Status: online
Or if you want the last part and you want it formatted as it originally was:

strTemp = "JSmith;Accounting;Purchasing;Approver"
arrTemp = Split(strTemp, ";")
arrTemp(0) = ""
WScript.Echo Mid(Join(arrTemp, ";"), 2)

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to sheepz)
 
 
Post #: 19
 
 RE: how to split - 7/2/2007 9:59:59 AM   
  sheepz


Posts: 247
Score: 2
Joined: 3/17/2006
From: Riverside the 909s
Status: offline
hi eb, thats a good way too.  i'm sorry to ask but could you explain this line:

Mid(Join(arrTemp, ";"), 2)

i'm not sure what Mid is for and the 2.  as for the join, it means to join the rest of arrTemp with ; after each element?

(in reply to ebgreen)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> how to split Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts