Length of vector members

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Length of vector members

L Boyd
When using len() for the length of vector members, a single numeric value returns undef rather than 1.

Examples:
 
 a = [[1,2],3,"string"];
 
 echoing the string part and lengths
 
 ECHO: "a   = ", [[1, 2], 3, "string"], " length = ", 3
 ECHO: "a[0] = ", [1, 2], " length = ", 2
 
 so far so good, but
 
 ECHO: "a[1]   = ", 3, " length = ", undef
 ECHO: "a[0][1] = ", 2, " length = ", undef
 
 no, it is not related to having a mixed vector
 
 b=[5,4,3,1];

 ECHO: "b   = ", [5, 4, 3, 1], " length = ", 4
 ECHO: "b[0] = ", 5, " length = ", undef

 for the string everything behaves great
 
 ECHO: "a[2]   = ", "string", " length = ", 6
 ECHO: "a[2][1]   = ", "t", " length = ", 1
Larry
Reply | Threaded
Open this post in threaded view
|

Re: Length of vector members

nophead
I think that is correct because a single number is not a vector and hence does not have a length. It is different from a vector of length 1.

I also make use of it to test if an arg is a vector, or a scalar.

On 24 September 2015 at 15:29, L Boyd <[hidden email]> wrote:
When using len() for the length of vector members, a single numeric value
returns undef rather than 1.

Examples:

 a = [[1,2],3,"string"];

 echoing the string part and lengths

 ECHO: "a   = ", [[1, 2], 3, "string"], " length = ", 3
 ECHO: "a[0] = ", [1, 2], " length = ", 2

 so far so good, but

 ECHO: "a[1]   = ", 3, " length = ", undef
 ECHO: "a[0][1] = ", 2, " length = ", undef

 no, it is not related to having a mixed vector

 b=[5,4,3,1];

 ECHO: "b   = ", [5, 4, 3, 1], " length = ", 4
 ECHO: "b[0] = ", 5, " length = ", undef

 for the string everything behaves great

 ECHO: "a[2]   = ", "string", " length = ", 6
 ECHO: "a[2][1]   = ", "t", " length = ", 1




--
View this message in context: http://forum.openscad.org/Length-of-vector-members-tp13971.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Length of vector members

doug.moen
In reply to this post by L Boyd
The len() function is only defined for vectors and strings. It returns undef for any other type of argument. Lots of code relies on this behaviour so don't expect it to change.

On Thursday, 24 September 2015, L Boyd <[hidden email]> wrote:
When using len() for the length of vector members, a single numeric value
returns undef rather than 1.

Examples:

 a = [[1,2],3,"string"];

 echoing the string part and lengths

 ECHO: "a   = ", [[1, 2], 3, "string"], " length = ", 3
 ECHO: "a[0] = ", [1, 2], " length = ", 2

 so far so good, but

 ECHO: "a[1]   = ", 3, " length = ", undef
 ECHO: "a[0][1] = ", 2, " length = ", undef

 no, it is not related to having a mixed vector

 b=[5,4,3,1];

 ECHO: "b   = ", [5, 4, 3, 1], " length = ", 4
 ECHO: "b[0] = ", 5, " length = ", undef

 for the string everything behaves great

 ECHO: "a[2]   = ", "string", " length = ", 6
 ECHO: "a[2][1]   = ", "t", " length = ", 1




--
View this message in context: http://forum.openscad.org/Length-of-vector-members-tp13971.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;Discuss@lists.openscad.org&#39;)">Discuss@...
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Length of vector members

L Boyd
In reply to this post by nophead
Ok, so it is intentional for a reason, not a bug.
Larry
Reply | Threaded
Open this post in threaded view
|

Re: Length of vector members

L Boyd
I meant to say:
It is intentional for a valid reason.
Larry
Reply | Threaded
Open this post in threaded view
|

Re: Length of vector members

jon_bondy
In reply to this post by L Boyd
Would it make sense to include examples such as this in the
documentation in order to prevent similar questions in the future?

On 9/24/2015 10:51 AM, L Boyd wrote:

> Ok, so it is intentional for a reason, not a bug.
>
>
>
> --
> View this message in context: http://forum.openscad.org/Length-of-vector-members-tp13971p13974.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 2015.0.6140 / Virus Database: 4431/10692 - Release Date: 09/24/15
>
>


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Length of vector members

L Boyd
In reply to this post by doug.moen
I forgot about [1] and [] being valid vectors
Larry
Reply | Threaded
Open this post in threaded view
|

Re: Length of vector members

L Boyd
In reply to this post by jon_bondy
I think examples of this in the manual would be useful. That was actually what I was working on when I discovered this unexpected behaviour.
Larry