|
NetLogo /
HandyCodeSnippets1. Testing code in the Observer area of the interface can be very frustrating because if there is an error, it eats the code instead of leaving it up on the screen. The better way is to create a test procedure, and a test button on the interface. to test let x ; [code here] show x end From the Code Tab: Type your code, try to go to the Interface. If there is an error, NetLogo will flag it and stay on the Code tab. It it doesn't find an error, click the "test" button you created. Even if NetLogo accepts it, it may not produce the results you expected. In that case, you can go back to the Code tab and refine it. 2. Create a sequential list ;; a useful tool to create a sequence ;; example: u-sequence 3 7 ;; output: [3 4 5 6 7] ;; example: reverse u-sequence 3 7 ;; output: [ 7 6 5 4 3] to-report u-sequence [ m n ] let res [] let i m while [i <= n] [ set res (lput i res) set i (i + 1) ] report res end 3. pick a number in a range to-report pick-a-number [low high] ;;validate low <= high before running the report let x high - low let y random x let z y - high report z end 4. to reverse the order of a csv data file requires CSV extension type 'u-reverse-csv' on the observer line overwrites 'reversed.csv' if it exists rename the file after it runs to u-reverse-csv set data read-data user-file ;;remove the header and put it at the end let header first data set data butfirst data set data lput header data set data reverse data ;; Now save the data. Manually rename the file to your choice when done csv:to-file "reversed.csv" data set data [] print "done - changes saved to reversed.csv. Rename the file now." end |