Gabriel Fair
Gabriel Fair

Reputation: 4264

Trouble with advanced netlogo code involving a question mark

I am using Netlogo v6.0.4 and I'm getting the following error message when I try to run sample code from an answer I found here on Stack Overflow.

Nothing named ? has been defined

In this answer the following netlogo code is proposed as an answer:

to-report split [ string delim ]
  report reduce [
    ifelse-value (?2 = delim)
      [ lput "" ?1 ]
      [ lput word last ?1 ?2 but-last ?1 ]
  ] fput [""] n-values (length string) [ substring string ? (? + 1) ]
end

The specific ? it does like is the first one in this section substring string ? (? + 1) .

When this answer was written in 2014, Netlogo v5 was in active use and it had a feature called tasks that were lambda methods. But in v6 tasks were replaced by anonymous-procedures.

Is that what the ? are here? How can I fix this error?

Upvotes: 0

Views: 1015

Answers (1)

Luke C
Luke C

Reputation: 10301

You got it in one- the ? in the versions were essentially placeholders for whatever variable was being passed to the task. The 5.3 dictionary entry for foreach has a good example:

foreach [1.1 2.2 2.6] [ show (word ? " -> " round ?) ]
=> 1.1 -> 1
=> 2.2 -> 2
=> 2.6 -> 3

In that case, foreach was taking the input list of [1.1 2.2 2.6] and iterating over it, where the ? takes the place in the command block of the current item being processed. As I understand it, the main syntactical difference in 6.X is that now you explicitly state what that placeholder is, by using the -> operator. So, the exact same idea as above, translated to 6.0 in the foreach example in the 6.0 dictionary entry, looks like this:

foreach [1.1 2.2 2.6] [ x -> show (word x " -> " round x) ]
=> 1.1 -> 1
=> 2.2 -> 2
=> 2.6 -> 3

There, you can see that the x is explicitly being defined as the placeholder. This really improves the clarity of code- you can define the placeholder however you like to be as clear and explicit as you'd like- this (over the top) example works just as well:

foreach [ 1.1 2.2 2.6 ] [ round_me -> show (word round_me " -> " round round_me) ]

If you're using multiple lists, do note that you have to surround the anonymous procedure with ( ) and your placeholder declaration with [ ]- for example:

( foreach [ 1 2 3 ] [ 10 20 30 ] [ [ a b ] -> print a * b ] )

If you're translating your code example, then, you can just focus on explicitly stating the placeholders. It also might help to break it down into the component parts to clarify- more detail in comments:

to-report split2 [ string delim ]
  ; split the string up into individual characters
  let characters fput [""] n-values ( length string ) [ a -> substring string a ( a + 1 ) ]

  ;  build words, cutting where the delimiter occurs
  let output reduce [ [ b c ] -> 
    ifelse-value ( c = delim ) 
    [ lput "" b ] 
    [ lput word last b c but-last b ]
  ] characters

  report output
end

Now, to follow Nicolas' example from your linked answer, you can call that to-report to split up your text:

to read-example
  let line "Example\tof\tsome\ttext"

  show split2 line "\t"
end

Gives you:

observer: ["Example" "of" "some" "text"]

Hope that is helpful!

Upvotes: 3

Related Questions