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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array help 2

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
Ok... I have an array, it's nice'n redimmable.

It starts as size.

I then pass it into an array byReference

It comes back, and I do a for each loop on it, msgboxing each value

I've only populated the first 3 values of the array, so I get the 3 values, then 13 more blank message boxes?! Yet ubound of the array still stores 15. Oh well, no big deal there. I couldn't care.

However, if I pull a Redim array(3) within my function, I get 4 messageboxes (still one more, what the heck?... and yes I'm sure it's not an errant messagebox from elsewhere, I have a unique bit of text in each one that's not changing)

But more perturbing, all 4 are blank... that's my big issue, I want to redim it to the size I want, without dropping the values.... any ideas?

-Rob
 
sounds like you jsut didn't ReDim Preserve the array

if you do not preserve the array or redim for a smaller numeric value then the number of elements the arrays contents will be lost.

O' and the 3 toltalling 4 is correct. remember vbscript and vb count array(0) as a element.

the 13 thing I'm not sure of

example of preserve
<script language=&quot;vbscript&quot;>
dim MyArray()
for x = 0 to 15
redim preserve MyArray(x)
MyArray(x) = &quot;testing&quot; & cStr(x)
alert(MyArray(x))
next

for y = 0 to UBound(MyArray)
alert MyArray(y)
next
</script>

run this first then run it again taking out the preserve

_________________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
[/sub]
 
Redim preserve... wonderful, that's it!

And about the 3->4, 15->16 thing...

I realize that the arrays are zero indexed, but it's seeming to me that the behavior of redim ArrayName(someNum), meant you'd have someNum + 1 elements, the max at index someNum... but now I do! Thanks for all the fish.

-Rob
 
[smile]

Thanks skiflyer

_________________________________________________________
$str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
$Nstr = ereg_replace(&quot;sleep&quot;,&quot;coffee&quot;,$str); echo $Nstr;

onpnt2.gif
[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top