[Gambas-user] A random sort of listview

jbskaggs jbskaggs at ...1871...
Tue Apr 7 16:48:06 CEST 2009


When I ran the code it ran fine- why is it important to have it  count
backwards in the random swap?

ie for i =199 to 0 step -1 instead of for i=0 to 199 step 1?

JB SKaggs


Simonart Dominique wrote:
> 
> Hi,
> 
> Hmm, did you run exactly this code? If so, there is 
> something wrong with the second FOR NEXT loop
> 
> jbskaggs a écrit :
>> using your suggestions and the swap command vs manual swapping this is my
>> code:
>> 
>> public b as string  'optional string used to hold the array data for file
>> or
>> split later
>> 
>> PUBLIC SUB button4_click() ' initialize array
>> DIM myArray AS Integer[200]
>> DIM a AS Integer
>> DIM i AS Integer
>> 
>> FOR i = 0 TO 199 STEP 1 'add array items 
>>  myArray[i] = i 
>> NEXT 
>> 
>> FOR i = 0 TO 199 STEP 1 ' random swap array items
>> a = Int(Rnd(i + 1))
>> SWAP myArray[i], myArray[a]
>> NEXT 
>> 
> 
> the right code is:
> 
> FOR i = 199 TO 0 STEP -1
>     ...
> NEXT
> 
>> 
>> FOR i = 0 TO 199 STEP 1 ' write items in listview2
>> listview1.MoveTo(myArray[i])
>> c = listview1.Item.Key
>> listview2.add(c, listview1.item.text) 
>> NEXT 
>> 
>> 
>> 
>> FOR i = 0 TO 199 STEP 1 'optional step to write array as a string for
>> file
>> or whatever use
>> b &= "slot" & myArray[i] & ","
>> NEXT 
>> PRINT b
>> END
>> 
>> PUBLIC SUB Button1_Click() 'an optional way to load the array values from
>> the variable b and / or a file if b was file loaded
>> DIM egg AS String[]
>> DIM c AS String
>>  listview1.Clear
>>  egg = Split(b, ",")
>>  FOR EACH c IN egg 
>> TRY listview1.Add(c, c) 
>> IF ERROR THEN RETURN 
>> NEXT 
>> IF listview1.Count >= 200 THEN RETURN 
>> END
>> 
>> JB Skaggs
>> 
> 
> Dominique Simonart
> 
> 
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> High Quality Requirements in a Collaborative Environment.
> Download a free trial of Rational Requirements Composer Now!
> http://p.sf.net/sfu/www-ibm-com
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
> 
> 

-- 
View this message in context: http://www.nabble.com/A-random-sort-of-listview-tp22919766p22930824.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list