michaelslamet Posted March 16, 2011 Posted March 16, 2011 (edited) Hi, Let say I have a array. I want only array content that begin with XX and length less than 6. Example: current array is: $current_array[0] = "AB1234" $current_array[1] = "XX11" $current_array[2] = "XX1234567" $current_array[3] = "XX123" $current_array[4] = "XA1234" New array should be: $new_array[0] = "XX11" $new_array[1] = "XX123" which is begin with XX and less than 6 in length. Any build-in function to do this? Thanks a lot Edited March 17, 2011 by michaelslamet
hannes08 Posted March 16, 2011 Posted March 16, 2011 Hi michaelslamet, I don't know any builtin function, you'll need to do it this way: $new_array = $current_array For $i = Ubound($new_array) -1 To 0 Step -1 If Stringleft($new_array[$i],2) <> "XX" Or StringLen($new_array[$i]) >= 6 Then _ArrayDelete($new_array,$i) EndIf Next Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
michaelslamet Posted March 16, 2011 Author Posted March 16, 2011 Thanks Hannes Why we should use descending for? Just wonder, instead of For $i = Ubound($new_array) -1 To 0 Step -1 can we use For $i = 1 to Ubound($new_array) - 1 ?
hannes08 Posted March 16, 2011 Posted March 16, 2011 Hi michaelslamet, if you use _ArrayDelete() it decreases the indexes: $a[0] = 1 $a[1] = 2 $a[2] = 3 _ArrayDelete($a, 1) $a[0] = 1 $a[1] = 3 If you use this in a loop you will throw an exception like array out of bounds. That's why going backwards. Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
michaelslamet Posted March 17, 2011 Author Posted March 17, 2011 ok, understand. Implement this into my code successfully. Thanks a lot, Hannes
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now