bugGNU Octave - Bugs: bug #48064, for-loop over structure elements...

 
 

bug #48064: for-loop over structure elements behaves differently from document

Submitter:  None
Submitted:  Tue 31 May 2016 09:34:06 AM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Documentation
Status:  Fixed Assigned to:  None
Originator Name:  Wang S Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * 4.0.2
Operating System:  * GNU/Linux Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Fri 17 Jun 2016 01:17:21 AM UTC, comment #10: 

No, it is probably better just to delete it.  Short is good :)

I'll close this as "fixed".

Lachlan Andrew <lachlan>
Thu 16 Jun 2016 05:36:46 PM UTC, comment #9: 

Ok, I deleted the entire paragraph on the stable branch, then came back here and noticed/remembered Lachlan's patch to reword it instead, sorry about that.

http://hg.savannah.gnu.org/hgweb/octave/rev/42929c66846d

If you think it's useful to add this back in to remind people about omitting output arguments for this syntax, we can do that.

Mike Miller <mtmiller>
Group Member
Sat 04 Jun 2016 01:11:54 PM UTC, comment #8: 

I think that it currently does iterate over columns, as shown below.


>> a.foo = 1;
>> a(3,3).bar = 2;
>> for b = a
b
end
b =

  3x1 struct array containing the fields:

    foo
    bar

b =

  3x1 struct array containing the fields:

    foo
    bar

b =

  3x1 struct array containing the fields:

    foo
    bar


It seems to be only the documentation that needs updating.

Lachlan Andrew <lachlan>
Fri 03 Jun 2016 01:59:14 PM UTC, comment #7: 

I'm guessing that the documented behavior was correct at some point, probably before struct arrays had been implemented and structures had to be scalars.  It was also long before the "~ syntax" for omitting results or arguments.

I agree with comment #5.  Iteration should work column-wise, the same as for other loops involving arrays.

John W. Eaton <jwe>
Group administrator
Fri 03 Jun 2016 02:37:29 AM UTC, comment #6: 

The attached patch updates the documentation.  Feel free to suggest better wording.

(file #37364)

Lachlan Andrew <lachlan>
Thu 02 Jun 2016 11:08:52 PM UTC, comment #5: 

For other types of "x", such as a cell or an ordinary matrix of double,int*,char,logical,...,  all of "for val=x", "for [val]=x" and "for [~]=x" iterate over x(:,1),x(:,2),...
Therefore, for a struct "x", all of the above for-loop should also iterate on x(:,1),x(:,2),... consistently.
Only "for [val,key]=x", "for [~,key]=x", "for [val,~]=x" and "for [~,~]=x" should iterate over x.a,x.b,...

So the current behavior does not need to change.
Only the document should be changed.

Anonymous
Thu 02 Jun 2016 01:31:15 PM UTC, comment #4: 

I agree with you all.
a straightforward for loop with a struct array on the right hand side should iterate over the array (e.g. x(1), x(2), x(3), ...), not the struct key (e.g. x.a, x.b, x.c, ...).
Because if not so, there would be no way to distinguish which to iterate over.

Anonymous
Thu 02 Jun 2016 01:12:23 PM UTC, comment #3: 

I agree with you all.
a straightforward for loop with a struct array on the right hand side should iterate over the array (e.g. x(1), x(2), x(3), ...), not the struct key (e.g. x.a, x.b, x.c, ...).
Because if not so, there would be no way to distinguish which to iterate over.

Anonymous
Thu 02 Jun 2016 02:51:20 AM UTC, comment #2: 

I agree with Lachlan, eliminate any mention of the simplifications, except to remind users that an element of the array can be omitted with the ~ placeholder.

This doesn't work in any version of Octave I have access to, back to 3.6.4, not sure when it ever worked, if at all, or why it was documented or implemented as this special case. I think it's clear that a straightforward for loop with a struct array on the right hand side should just iterate over the elements of the struct array.

Mike Miller <mtmiller>
Group Member
Thu 02 Jun 2016 12:32:06 AM UTC, comment #1: 

Matlab (R32013b) doesn't implement this feature, so we are not bound by compatibility issues.

My preference would be to change the documentation to
"Either the key or val variable may be replaced by a tilde."
or perhaps allowing only "[val]", but still making the brackets mandatory.

The alternative (making brackets optional) would require run-time processing to determine whether the RHS is a structure or an array, which would slow down the common case for a minor syntactic advantage in the (very?) rare case.

Lachlan Andrew <lachlan>
Tue 31 May 2016 09:34:06 AM UTC, original submission:  

PDF documentation says that
"The key variable may also be omitted. If it is, the brackets are also optional."
in section 10.5.1 Looping Over Structure Elements, page 168.

But the behavior is as follow

octave:1> clear all; x.a=1; x.b=[1,2;3,4]; x.c="string"; for [val,key]=x, key, val, endfor
key = a
val =  1
key = b
val =

   1   2
   3   4

key = c
val = string

OK

octave:2> clear all; x.a=1; x.b=[1,2;3,4]; x.c="string"; for [~,key]=x, key, endfor
key = a
key = b
key = c

OK

octave:3> clear all; x.a=1; x.b=[1,2;3,4]; x.c="string"; for [val,~]=x, val, endfor
val =  1
val =

   1   2
   3   4

val = string

OK

octave:4> clear all; x.a=1; x.b=[1,2;3,4]; x.c="string"; for [val]=x, val, endfor
val =

  scalar structure containing the fields:

    a =  1
    b =

       1   2
       3   4

    c = string

cannot loop over elements

octave:5> clear all; x.a=1; x.b=[1,2;3,4]; x.c="string"; for val=x, val, endfor
val =

  scalar structure containing the fields:

    a =  1
    b =

       1   2
       3   4

    c = string

cannot loop over elements

Which is correct, the behavior or the document?

System information:

octave:6> ver
----------------------------------------------------------------------
GNU Octave Version: 4.0.2
GNU Octave License: GNU General Public License
Operating System: Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) x86_64

and Debian package status:

ii  liboctave3:amd64      4.0.2-1~bpo8+1 amd64
ii  octave                4.0.2-1~bpo8+1 amd64
ii  octave-common         4.0.2-1~bpo8+1 all
ii  octave-doc            4.0.2-1~bpo8+1 all
ii  octave-info           4.0.2-1~bpo8+1 all


Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files

 

Depends on the following items: None found

Digest:
   bug dependencies.

 

Carbon-Copy List
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by lachlan (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 10 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-06-17 lachlan CategoryNone Interpreter
        Item GroupNone Documentation
        StatusPatch Submitted Fixed
        Open/ClosedOpen Closed
    2016-06-04 lachlan StatusConfirmed Patch Submitted
    2016-06-03 jwe StatusPatch Submitted Confirmed
    2016-06-03 lachlan Attached File- Added bug_48064_for_over_struct.cset, #37364
        StatusConfirmed Patch Submitted
    2016-06-02 mtmiller Dependencies- bugs #48089 is dependent
    2016-06-02 lachlan StatusNone Confirmed

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code