Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


InStr() - Help needed pulling additional info....

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> InStr() - Help needed pulling additional info....
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 InStr() - Help needed pulling additional info.... - 5/24/2007 4:49:34 AM   
  Spooner

 

Posts: 41
Score: 0
Joined: 4/16/2007
From: Blighty
Status: offline
Hi there,

I have the following piece of code -

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
   sLinkName = objRecordSet.Fields("cn").value
   sCost = objRecordSet.Fields("cost").value
   sFilter = right(sLinkName,2)
   If Instr(1,Lcase(sFilter),"-1") <> 0 Then
   For Each sConnects in objRecordSet.Fields("siteList").value
   oOutput.writeline sLinkName & ";" & sCost
 Next
 End If
objRecordSet.MoveNext

Loop

If for instance sLinkName contains "FRED-1", my script above can pick out the "-1" part but I'd also like it to be able to pick out the "FR" part of "FRED-1".

How can I do that?  The records in sLinkName will contain "JUNE-1","JANE-1" etc...it's just the "FR" and the "-1" I need to be returned so I can write it to the output file.

Any ideas anyone?

Thanks
Spooner
 
 
Post #: 1
 
 RE: InStr() - Help needed pulling additional info.... - 5/24/2007 4:53:47 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
I'm a little confused. Do you only want to get the first two letters of FRED, or the first two letters of every string? Either way, to get the first two letters:

strFirstTwo = Left(sLinkName, 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 Spooner)
 
 
Post #: 2
 
 RE: InStr() - Help needed pulling additional info.... - 5/24/2007 4:56:17 AM   
  Country73


Posts: 735
Score: 10
Status: offline
You should be able to get what you want by adding another INSTR function with AND -

IF INSTR(LCASE(sFilter),"-1") AND INSTR(LCASE(sFilter),"fr")THEN

INSTR FUNCTION -
   INSTR([start (optional),] string1 (required), string2 (required)[, compare (optional)])

or better yet, use what ebgreen posted

IF LEFT(LCASE(sFilter),2) = "fr" AND INSTR(sFilter,"-1") THEN

< Message edited by Country73 -- 5/24/2007 4:58:04 AM >

(in reply to Spooner)
 
 
Post #: 3
 
 RE: InStr() - Help needed pulling additional info.... - 5/24/2007 5:12:52 AM   
  ehvbs

 

Posts: 2223
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Spooner,

here is another approach for another interpretation of your specs:


      

Good luck!

ehvbs

(in reply to Country73)
 
 
Post #: 4
 
 RE: InStr() - Help needed pulling additional info.... - 5/24/2007 6:51:16 AM   
  Spooner

 

Posts: 41
Score: 0
Joined: 4/16/2007
From: Blighty
Status: offline
Hi All,

Many thanks for the replies  -excellent.

What I'd also like to be able to do is be able to pull out the characters "FR" and "-1" if the string looks like this "MAN-FRED-1" ... can you do this using InSt ?

Thanks
Spooner

(in reply to ehvbs)
 
 
Post #: 5
 
 RE: InStr() - Help needed pulling additional info.... - 5/24/2007 7:21:48 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
quote:

if the string looks like this "MAN-FRED-1"


How much will it look like that? Will just the number change? If so, how much? At a guess:

arrParts = Split(sLinkName, "-")
strFirstTwoLetters = Left(arrParts(1), 2)
strNumber = "-" & 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 Spooner)
 
 
Post #: 6
 
 RE: InStr() - Help needed pulling additional info.... - 5/24/2007 7:48:05 AM   
  ehvbs

 

Posts: 2223
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
While there is nothing wrong with ebgreen's solution and it certainly has the
big pro of using easily to understand standard VBScript functions, a RegExp
approach may be easier to tweak, if the specifications are changing. Compare
this


      

to my last example and you'll see I changed the pattern only and didn't have
to come up with a new algorithm. Of course, you'll have to invest some learning
time and effort into RegExps, but in the long run it will pay.

(in reply to ebgreen)
 
 
Post #: 7
 
 RE: InStr() - Help needed pulling additional info.... - 5/24/2007 8:05:12 AM   
  ebgreen


Posts: 5250
Score: 31
Joined: 7/12/2005
Status: offline
Ehvbs is very correct about the payoff for learning Regexs.

_____________________________

"... 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 ehvbs)
 
 
Post #: 8
 
 RE: InStr() - Help needed pulling additional info.... - 5/24/2007 8:18:18 AM   
  ehvbs

 

Posts: 2223
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
And you can use mikeock's RegExps Tester

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

to add a bit of fun to the learning!

(in reply to ebgreen)
 
 
Post #: 9
 
 RE: InStr() - Help needed pulling additional info.... - 5/24/2007 6:59:54 PM   
  Spooner

 

Posts: 41
Score: 0
Joined: 4/16/2007
From: Blighty
Status: offline
Hi Ebgreen,

Sorry about the confusion.

All the strings being read in will be in this format "MAN-xxxx-yyy-z"

MAN is always present
xxxx will be any name but I'm only interested in these four - FRED, JANE, DIRK or GAIL
yyy will be any three  characters e.g ABC
z will either be -1 or -2


So , if the first record read in from sLinkName contains  "MAN-FRED-ABC-1 the second record contains "MAN-JANE-ABC-1" and the third record contains "MAN-BERT-ABC-1"  and the fourth contains "MAN-KOFI-ABC-1" how do I pull out just the first two records as they contain what I'm looking for, namely 'FRED' and ' JANE'  and '-1' ?

To recall, the code I have at present is -

have the following piece of code -

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
  sLinkName = objRecordSet.Fields("cn").value
  sCost = objRecordSet.Fields("cost").value
  sFilter = right(sLinkName,2)
  If Instr(1,Lcase(sFilter),"-1") <> 0 Then
  For Each sConnects in objRecordSet.Fields("siteList").value
  oOutput.writeline sLinkName & ";" & sCost
Next
End If
objRecordSet.MoveNext

Loop

(in reply to ebgreen)
 
 
Post #: 10
 
 
 
  

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 >> InStr() - Help needed pulling additional info.... Page: [1]
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