JenB
JenB

Reputation: 17678

netlogo convert string to numbers within nested lists

I am transferring between NetLogo and igraph (in R). Some of the information returned from igraph is 2-level nested lists of strings. Typical example looks like:

[ ["1" "2" "3"] ["4"] ]

I want to convert the internal strings into numbers, while retaining the list structure. So the example would become:

[ [1 2 3] [4] ]

I am guessing I need a combination of map and read-from-string (and perhaps other list manipulation like lput and foreach due to the nesting), but I just can't make it work.

Any ideas?

Upvotes: 5

Views: 1710

Answers (2)

Nicolas Payette
Nicolas Payette

Reputation: 14972

Just for fun, here is a version that can convert an arbitrary number of nested levels:

to-report read-from-list [ x ]
  report ifelse-value is-list? x
    [ map read-from-list x ] 
    [ read-from-string x ]
end

Example:

observer> print read-from-list [ ["1" "2" "3" ] ["4" [ "5" "6" ] ] ]
[[1 2 3] [4 [5 6]]]

Upvotes: 5

mattsap
mattsap

Reputation: 3806

Essentially, map each list to a mapped list with only int values. Try the following:

show map [ map [ read-from-string ? ] ?] [ ["1" "2" "3"] ["4"] ]

Upvotes: 6

Related Questions