Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Howto get a part of an Array

Status
Not open for further replies.

Hanzel79

Technical User
Feb 13, 2003
16
NL
Hi all,

I need to get a part of an array, starting at some value and reading till it finds another value.

The array is like this:
[algemeen]
g-winzip
g-msword
g-msexcel
g-msaccess
g-msoutlook

[additioneel]
g-msword
g-winzip
g-msexcel
g-msoutlook
g-msaccess

[misbruik]
g-msoutlook
g-msword
g-msexcel
g-msaccess
g-winzip

I want let it start reading if it meets instr(arrGroups, &quot;additioneel&quot;) and keep on reading till it meets a Left(<stringname>,1) = &quot;[&quot; and then returning the values between [additioneel] and the next &quot;[&quot;.

For Each item In arrGroups
If instr(item,&quot;algemeen&quot;) Then
WScript.Echo item
End If
Next

Is only returning the name &quot;[algemeen]&quot; (as expecteed)
I don't really know how i can accomplish this and even if it can be done.

Any help is welcome

Grtz Hanzel



If smile = vbFalse Then
MsgBox(&quot;Smile... Tomorrow will be worse!&quot;, ,&quot;Smile...&quot;)
End If
 
Hello Hanzel79,

This is one way to do it out of many. It finds the index range where you need the elements of the array.
Code:
sS=&quot;additioneel&quot;	'starting
sE=&quot;[&quot;		        'ending must be at position 1
iFound=-1
For i=0 to ubound(arrGroups)
	If Instr(1, arrGroups(i), sS,1)>0 Then
		iFound=i : i=ubound(arrGroups)
	End If
Next
If iFound<0 Then
	wscript.echo &quot;No &quot; & sS & &quot; is found. Operation aborted.&quot;
	wscript.quit(1)
End If
jFound=-1
For i=iFound+1 to ubound(arrGroups)
	If Instr(1, arrGroups(i), sE, 1)=1 Then
		jFound=i : i=uBound(arrGroups)
	End If
Next
If jFound<0 Then
	wscript.echo &quot;No &quot; & sE & &quot; is found after located &quot; & sS & &quot;. Operation aborted.&quot;
	wscript.quit(2)
End If
wscript.echo &quot;Object found  between the index &quot; & cstr(iFound) & &quot;  and &quot; & cstr(jFound)
regards - tsuji

PS. Instr() with string to search not being a character may on occasions result in unexpected found. Your array element's composition may seem immuned from this aberration. But, just be careful.
 
You can try something like this:
Code:
strGroups=Join(arrGroups,vbLf)
strSearch=&quot;[algemeen]&quot;
strFound=&quot;&quot;
i=InStr(strGroups,strSearch,1)
If i>0 Then
  strFound=Mid(strGroups,i+Len(strSearch))
  i=InStr(strFound,&quot;[&quot;,1)
  If i>0 Then strFound=Left(strFound,i-1)
End If
WScript.Echo strFound
You can create a new array with the results like this:
Code:
arrFound=Split(strFound,vbLf)

Hope This Help
PH.
 
Hi Tsuji,

I want to thank you alot, had a small question about 'what the indexnr was' but with a little trying it works perfectly, added a litte For loop at the end to get only the values between the [<first>] and [<last>]

For Item = cstr(iFound)+1 to cstr(jFound)-1
If arrGroups(item) > &quot;&quot; Then
WScript.Echo arrGroups(item)
End If
Next

Great thanks, i was stuck with my script and now i finally can continu (with great hurry)

Grtz Hanzel

If smile = vbFalse Then
MsgBox(&quot;Smile... Tomorrow will be worse!&quot;, ,&quot;Smile...&quot;)
End If
 
Thanks for the feedback, Hanzel79.

You got the idea all right. Just note that iFound & jFound are integers, hence, your echo for visual inspection lines do better without cstr().

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top