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

Need more array help

Status
Not open for further replies.

MrMoocow

Programmer
May 19, 2001
380
US
Heres the code I have
For i = 1 To 25
test = test + 1
MsgBox test
i = i + 1
Next i

I want it to literate through each I 52 times and then stop, I could use a do loop but I want to be better at these things.
 
Do you want to loop 52 times or 25 times? In any case, your code is close:

For i = 1 To 25
test = test + 1
MsgBox test
Next i

Best Regards and many Thanks!
Michael G. Bronner X-)

"Who cares how time advances? I am drinking [beer] today." Edgar Allan Poe
 
Okay heres what I have:

I have card(1 to 52)
then I have a string with 52 numbers seperated by strings, So I use this:

do until i = 25
card(i) = left(deck,dl)


dl=dl+3
i=i+1
loop

but it gives me errors, Can u help me seperate 52 numbers from one string to each card(1 to 52)?,
 
Did you initialize the variable i? If not, i will be set to zero at the start of your loop. You then will address card(0), which does not exist, as you declared the array from 1 to 52.

With all the above assumptions, a correct piece of code could be:

Dim card(1 to 52) As String
Dim i As Integer
Dim deck As String

For i = 1 to 52
card(i) = Left$(deck,dl)
dl = dl + 3
next i


However this assumes that the string starts with 1 and that all numbers are left aligned, without leading zeroes and regularly spaced.

A better approach would be to use the Split function, which does the job in a single statement.Assuming that the numbers are separated by spaces, code looks as follows

Dim card() As String
Dim deck as String
deck - Split(deck)


Card will be returned as a ZERO based array, meaning that the indices run from 0 to Ubound(card) (which should be 51, if all things run smoothly.






_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
rvBasic has the approach, but a few details are awry. Hence, ny entry:

Code:
Public Function basShuffle() As Variant

    Dim Card As Variant
    Dim Deck As String

    For Idx = 1 To 51
        Deck = Trim(str(Idx)) & ","
    Next Idx
    Deck = Deck & "52"

    Card = Split(Deck, ",")

End Function

You would 'call' this function:

[tab]MyCards = basShuffle

It would return a ZERO based array of integers, 1 to 52 in MyCards(0) to MyCards(51).

Obviously, this is only a small piece of the issue. If wer'e usin "Cards", they need to be seperated into Suite and randomized. for any real card "game". Since this functionality was not specified, it was NOT considered. One assumes the poster will either have the other parts, or will return, asking additional questions.

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
thanks for your help, and about the suites and what not, I have that all worked out.
 

Michael: indeed the "details" were terribly awry! Sorry for that. You must Trim$ the string to remove the irregular spaces and replace them with a regular single character pattern to effectively use the Split function. Glad you spotted that. _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
I'm sorry but I need more help,
Here is the numbers I have to work with:
47, 43, 31, 52, 42, 33, 05, 40, 39, 27, 13, 11, 23, 37, 19, 36, 22, 26, 38, 32, 50, 18, 12, 14, 29, 44, 15, 24, 34, 06, 41, 09, 28, 01, 51, 04, 20, 48, 03, 46, 35, 45, 16, 49, 21, 02, 17, 08, 30, 25, 10, 07

that is exactly how they appear, I need to get each one number into an array.
 
In which case rvBasic's split solution is ideal:

dim card() as string
dim deck as string

deck="47, 43, 31, 52, 42, 33, 05, 40, 39, 27, 13, 11, 23, 37, 19, 36, 22, 26, 38, 32, 50, 18, 12, 14, 29, 44, 15, 24, 34, 06, 41, 09, 28, 01, 51, 04, 20, 48, 03, 46, 35, 45, 16, 49, 21, 02, 17, 08, 30, 25, 10, 07"

card=split(deck,", ")
 
John's remark reads: You're getting an error at compile time because there is a blank after the comma in the separator field. There shouldn't be a blank: the separator field is a single character. Therefore you should write:
Split(deck,",")

Note that the resulting data in the card array will start with a space character, except for the first element. If that space is to be elimimated then the code will look as follows:

Dim card() As String
Dim deck As String
card = Split(deck,",")

Dim i as Integer
For i = 0 to UBound(card)
card(i) = LTrim$(card(i))
Next i

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
There is no issue with the seperator field being more than one character. The blank is there deliberately.

 
just be CAREFUL which "Split" soloution you use. rvBasic's has some details which are not correct.

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
I should have been less terse (more verbose:)). I knew the space was deliberate. I was just pointing out a fact because the space in the ", " could be missed easily. I didn't thimk it was an error.
 
strongm is right when he claims that the separator field is not limited to a single character, as i claimed erroneously.

I was lead to believe this, because the language reference (vol1 p994) talks about a string character not a character string, which makes all the difference.

You opened new perpectives.

Robert _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top