ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Whenever I try to run a for loop on it, the error says "Custom_Message object is not iterable.

Plain message objects are indeed not iterable.

Also I can not use any split or iter stuff on it since its not iterable.

Again, expected.

Any idea how to iterate through a custom message.

My Custom_Message file is like this:

string[] custom_msg

The iterable here is custom_msg, not the message object itself (it's unfortunate you've given your message type the same name as the only field it contains, that makes this less obvious).

So to iterate over the list contained in custom_msg (note: the field, not the message object itself), you'd do something like this (in a callback called my_callback(..) and the only argument it gets is a Custom_Message instance called msg):

def my_callback(msg):
    for the_msg in msg.custom_msg:
        # do something with 'the_msg' here

Whenever I try to run a for loop on it, the error says "Custom_Message object is not iterable.

Plain message objects are indeed not iterable.

Also I can not use any split or iter stuff on it since its not iterable.

Again, expected.

Any idea how to iterate through a custom message.

My Custom_Message file is like this:

string[] custom_msg

The iterable here is custom_msg, not the message object itself (it's unfortunate you've given your message type the same name as the only field it contains, that makes this less obvious).

So to iterate over the list contained in custom_msg (note: the field, not the message object itself), you'd do something like this (in a callback called my_callback(..) and the only argument it gets is a Custom_Message instance called msg):

def my_callback(msg):
    for the_msg in msg.custom_msg:
        # do something with 'the_msg' here

Finally: I'd recommend given a better name to your message type. Custom_Message does not convey any information as to what sort of information it contains, and also does not give any clues as to how to interprete the data in it. custom_msg as a field name is of course also not really a good idea.

Try to use the field name to give information on what sort of messages are in there.

Whenever I try to run a for loop on it, the error says "Custom_Message object is not iterable.

Plain message objects are indeed not iterable.

Also I can not use any split or iter stuff on it since its not iterable.

Again, expected.

Any idea how to iterate through a custom message.

My Custom_Message file is like this:

string[] custom_msg

The iterable here is custom_msg, not the message object itself (it's unfortunate you've given your message type the same name as the only field it contains, that makes this less obvious).

So to iterate over the list contained in custom_msg (note: the field, not the message object itself), you'd do something like this (in a callback called my_callback(..) and the only argument it gets is a Custom_Message instance called msg):

def my_callback(msg):
    for the_msg in msg.custom_msg:
        # do something with 'the_msg' here

Finally: I'd recommend given giving a better name to your message type. Custom_Message does not convey any information as to what sort of information it contains, and also does not give any clues as to how to interprete the data in it. custom_msg as a field name is of course also not really a good idea.

Try to use the field name to give information on what sort of messages are in there.