Sign in to follow this  
Followers 0
emmanuel

A little help with Arrays?

8 posts in this topic

here's my attempt at creating an array, I figured I'd create a string of all the die results, then stringsplit that into an array... but the array seems to be empty, or I'm not doing the split right?

$XD4 = 5;how many times to roll
Dim $D4ROLLS[$XD4]; define array
MsgBox(0, "test", "5d4 test: " & _dieroller(4, $XD4, $D4ROLLS[1]))

; $D is the die to roll, $X is the number of times to roll it, $total is the value to return to.
Func _dieroller($D, $X, $TOTAL) 
   Do 
      $ROLL = Int(Random($D) + 1)
      $RESULT = String($ROLL & "/")
      $REPS = $REPS + 1
   Until $REPS = $X
   $TOTAL = StringSplit($RESULT, "/")
   Return $TOTAL
EndFunc  ;==>_dieroller

"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites



Untested, but should be close enough.

Func _dieroller($D, $X, $TOTAL)
  Local $res = ""
  Do 
     $ROLL = Int(Random($D) + 1)
     $REPS = $REPS + 1
     $res = $res & ";" & $ROLL
  Until $REPS = $X
  $res = StringTrimLeft($res, 1); Leading semi-colon
  Local $a = StringSplit($res, ";")
  Local $i, $t = 0
  For $i = 1 To $a[0]
     $t = $t + Number($a[$i])
  Next
  $a[0] = $t
  Return $a
EndFunc ;==>_dieroller

Share this post


Link to post
Share on other sites

You can't display an array in a message box since it is not text. Try either returning a string and parsing that how you want, or using some function to display each element of the array (prehaps seperate with a comma-space pair?) Use something like

Func DisplayArray($array) ;returns a string representation of the array
  $return = $array[0]
  For $i =1 To UBound($array) - 1
    $return = $return & ", " & $array[$i]
  Next
  Return $return
EndFunc

Then just concatanate that with what you want in the message box.


[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Share this post


Link to post
Share on other sites

my eventual goal with this is to write a GUI die roller, and I figured first step is to setup the function to do the work... I'd like to be able to display two results when someone clicks roll, the individual rolls and the total of those rolls...

I thought that $array[0] would return the value as the # of lines in the array (why wouldn't that show in a msgbox?), but that doesn't seem to be working for me...

Valik, what does your function return? I'm trying to pick it appart line by line, but I don't think I'm getting it, could you comment it up?


"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

I thought that $array[0] would return the value as the # of lines in the array (why wouldn't that show in a msgbox?), but that doesn't seem to be working for me...

$array[0] will (from the StringSplit() function) but in your original code you were trying to display "5d4 test: " & _dieroller(4, $XD4, $D4ROLLS[1])

The part in italics (your function call) returns an array, which you cannot put into a message box.


[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Share this post


Link to post
Share on other sites

pekster is right.

Here's your code the right way:

$XD4 = 5;how many times to roll
Dim $D4ROLLS[$XD4]; define array
MsgBox(0, "test", "5d4 test: " & _dieroller(4, $XD4, $D4ROLLS[1]))

; $D is the die to roll, $X is the number of times to roll it, $total is the value to return to.
Func _dieroller($D, $X, $TOTAL) 
  Do 
     $ROLL = Int(Random($D) + 1)
     $RESULT = String($ROLL & "/")
     $REPS = $REPS + 1
  Until $REPS = $X
  $TOTAL = StringSplit($RESULT, "/")
  Return $TOTAL[0]
EndFunc ;==>_dieroller

Share this post


Link to post
Share on other sites

The function:

Func _dieroller($D, $X)
    Local $res = "", $REPS, $ROLL, $a, $i, $t = 0   ; Declare variables
    Do 
        $ROLL = Int(Random($D) + 1)
        $REPS = $REPS + 1
        $res = $res & ";" & $ROLL   ; Append to string
    Until $REPS = $X
    $res = StringTrimLeft($res, 1); Strip leading semi-colon
    $a = StringSplit($res, ";")   ; Split into an array
    For $i = 1 To $a[0]   ; Loop through the array adding the numbers
        $t = $t + Number($a[$i])
    Next
    $a[0] = $t   ; Replace element 0 with the total of all the rolls
    Return $a   ; Return the array
EndFunc;==>_dieroller

The test example:

Local $res = _dieroller(6, 10)
MsgBox(4096, "", "Total=" & $res[0]); Display the total
Local $i
For $i = 1 To UBound($res) -1; Loop through and display the rest
    MsgBox(4096, "", "Roll " & $i & "=" & $res[$i])
Next

Share this post


Link to post
Share on other sites
:D Thank you all very much, I didn't intend to ask for it to be done for me, but now that it is, I really appreciate it... I'll keep reading it over and over till I understand all the little parts :huh2:

"I'm not even supposed to be here today!" -Dante (Hicks)

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0