Rafaela
Rafaela

Reputation: 449

How to resolve the error: ITEM expected input to be a number but got the string "12" instead in NetLogo?

I have a list that I'd like to strip out the spaces and square brackets and turn into a number to use elsewhere in the code.

turtle-profiles-habitat[ [1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] ]...

turtle-profiles-habitat-code [ 1 2 3 4 5 12 13 14 15 23 ]...

However, the following error appears: ITEM expected input to be a number but got the string "12" instead.

Can I do a string to number transformation?

Thanks in advance

The code below

globals [ ValidHabs ]
turtles-own [ turtle-profiles-habitat-code turtle-profiles-habitat t-p-h-item-ValidHabs my-patches ]
patches-own [ habitatcover ]

to setup
  ca
  set ValidHabs [[1] [2] [3] [4] [5] [1 2] [1 3] [1 4] [1 5] [2 3] [2 4] [2 5] [3 4] [3 5] [4 5] [1 2 3] [1 2 4] [1 2 5] [1 3 4] [1 3 5] [1 4 5] [2 3 4] [2 3 5] [2 4 5] [3 4 5] [1 2 3 4] [1 3 4 5] [1 2 4 5] [1 2 3 5] [2 3 4 5] [1 2 3 4 5]]
  (
    foreach ValidHabs [
      this-profile ->
      ask one-of patches [
        sprout 1
        [
          set turtle-profiles-habitat this-profile
          read
        ]
      ]
    ]
  )     
end

to read
  ask turtle who [
    set turtle-profiles-habitat-code reduce_list turtle-profiles-habitat
    print ( word "turtle-profiles-habitat-code is: " turtle-profiles-habitat-code )
  ]

  print ( word "turtle-profiles-habitat-code: " turtle-profiles-habitat-code )
  print ( word "ValidHabs is: " ValidHabs )    
end

to-report reduce_list [a_list]
  report reduce word a_list
end

However it is giving the following error:

I made your suggestion and the following error appeared: ITEM expected input to be a number but got the string "12" instead. Is there any way to adjust this? Thanks

Upvotes: 0

Views: 236

Answers (1)

LittleLynx
LittleLynx

Reputation: 1181

This looks like a huge workaround, but it works:

report reduce word a_list only reports a string, if a_list is a list with at least to entries. If it is for example [1], it would only report a number (I didn't know this before)

That's why I make sure that a string is created with word reduce word a_list ""

That string is then reported as a number with read-from-string

to-report reduce_list [a_list]
  report read-from-string word reduce word a_list ""
end

Upvotes: 1

Related Questions