help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: equivalent for C-style init: structname varname[] = {...} ?


From: Jordi Gutiérrez Hermoso
Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
Date: Wed, 14 Nov 2012 13:05:13 -0500

.On 14 November 2012 11:32, Yury T. <address@hidden> wrote:
> Jordi Gutiérrez Hermoso-2 wrote
>>> Arrgh! :) Sorry, I'd better go with Sergei's solution or with R, indeed.
>>> Thank you all the same.
>>
>> Sergei's if anything, is gonna be even *more* complicated, because
>> you'll have to replace {s.data} with a bigger expression so you can
>> select the ones you want.
>
> Actually, not so. Here's adaptation of my recent example with access to an
> individual fields of an item selected by arbitrary key. Reads almost
> naturally, right?

You've changed the problem. What you've shown me here does not have
the ID and Data fields. Instead, you've made the field names be the id
names. If this is way you want to arrange the data, then a struct
array is probablyl not optimal.

> Of course, I wasn't shown how make a full range iteration for such a data
> structure, so will have to work that out by myself :).
>
>   s = consistent_struct\.
>   (.
>   "Buffat+Borel 1976 (5)", {"Buffat+Borel 1976 (5)", 13, 1337},
>   "Coombes 1972 (2)", {"Coombes 1972 (2)", 13, 1337},
>   "Lai+Guo+Petrova+ 1996 (2a)", {"Lai+Guo+Petrova+ 1996 (2a)", 30, 505}
>   );.
> .
>   getfield( s, "Coombes 1972 (2)" ){1}.
>   getfield( s, "Coombes 1972 (2)" ){2}.
>   getfield( s, "Coombes 1972 (2)" ){3}.

Let me explain something. Seryozha's "consistent_struct" is his
rebellion against struct arrays. His complaint is that struct("field",
{value1, value2}) produces a struct array. SS wants a scalar struct
instead. This can be obtained like so: struct("field", {{value1,
value2}}). SS considers this to be "inconsisent" because doubling up
the {{}} looks to him weird, because he doesn't understand that struct
is for creating struct arrays, i.e. the values in cells get mapped to
each struct in the struct array. Since Octave is an array-based
language, making struct() produce struct arrays is the most natural
implementation.

Thus Sergiyko's consistent_struct is supposed to be equivalent to this

   s = struct (
   "Buffat+Borel 1976 (5)", {{"Buffat+Borel 1976 (5)",13, 1337}},
   "Coombes 1972 (2)", {{"Coombes 1972 (2)", 13, 1337}},
   "Lai+Guo+Petrova+ 1996 (2a)", {{"Lai+Guo+Petrova+ 1996 (2a)", 30, 505}}
   );

This actually has uncovered a bug in Octave. Field names are supposed
to not contain certain characters. In this regard, getfield(s, "foo")
is supposed to be the same as s.('foo"), but SS doesn't seem to know
this. Since he doesn't know this, he used s = setfield(s, "field",
value) instead of s.("field") = value. However, in this case because
of the special characters in the field names, setfield and getfield
should error out here. They do in Matlab. I will patch Octave to error
out on these.

I really recommend you don't take advice from SS. He repeatedly
insists on refusing to learn Octave and compares it to Perl instead.
Do you really want to take advice from someone who considers Perl to
be best language ever written? Maybe you do, but in that case, perhaps
you would be happier with Perl instead of Octave.

- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]