JP081X
JP081X

Reputation: 33

Combining and formatting lists in NetLogo

I've got a list of point pairs (in NetLogo coordinates) called "coords" that looks like this:

[[[255 97] [256 97]] 
 [[-131 408] [-129 407]]
 [[-125 406] [-122 405]] 
 [[-84 188]] [-83 188]]
 [[-303 200] [-304 203]]
 [[25 414] [19 415]]
 [[-128 256] [-125 254]]
 .......................................]

Each point pair has associated with it a classification variable, which takes one of the values "I", "U", or "S". I have a separate list of these classification variables, called "coord_class" the same length and in the same order as the point pairs listed above.

["S" "S" "U" "I" "S" "I" "U" ...]

What I would like to do is append the corresponding classification variable to the point pairs, in a way that looks like this:

[[[255 97 "S"] [256 97 "S"]] 
 [[-131 408 "S"] [-129 407 "S"]]
 [[-125 406 "U"] [-122 405 "U"]] 
 [[-84 188 "I"]] [-83 188 "I"]]
 [[-303 200 "S"] [-304 203 "S"]]
 [[25 414 "I"] [19 415 "I"]]
 [[-128 256 "U"] [-125 254"U"]]
 .......................................]

Note that both points in a given pair take the same classification variable value.

I've attempted to do this using the map reporter:

set coords (map [list ?1 ?2] coords coord_class)

which gives an output that looks like this:

[[[[255 97] [256 97]] "S"] 
 [[[-131 408] [-129 407]] "S"]
 [[[-125 406] [-122 405]] "U"] 
 [[[-84 188]] [-83 188]] "I"]
 [[[-303 200] [-304 203]] "S"]
 [[[25 414] [19 415]] "I"]
 [[[-128 256] [-125 254]] "U"]
 .......................................]

This isn't structured properly for other functions in the code. Any advice to help me obtain the desired output would be appreciated. Thanks!

Upvotes: 2

Views: 409

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

You have a list of lists of lists. Doing what you want will require two separate map operations. The easiest way to do this is probably to split the task between two small reporters:

to-report add-classes [ coords classes ]
  report (map add-class coords classes)  
end

to-report add-class [ lists class ]
  report map [ lput class ? ] lists
end

You can then use it like this:

to go
  let coords [
    [ [ 255  97] [ 256  97] ]
    [ [-131 408] [-129 407] ]
    [ [-125 406] [-122 405] ]
    [ [ -84 188] [ -83 188] ]
    [ [-303 200] [-304 203] ]
    [ [  25 414] [  19 415] ]
    [ [-128 256] [-125 254] ]
  ]
  let coord_class ["S" "S" "U" "I" "S" "I" "U"]
  show add-classes coords coord_class
end

Which will print the desired output:

[[[255 97 "S"] [256 97 "S"]] [[-131 408 "S"] [-129 407 "S"]] [[-125 406 "U"] [-122 405 "U"]] [[-84 188 "I"] [-83 188 "I"]] [[-303 200 "S"] [-304 203 "S"]] [[25 414 "I"] [19 415 "I"]] [[-128 256 "U"] [-125 254 "U"]]]

Upvotes: 2

Related Questions