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: Sergei Steshenko
Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
Date: Tue, 13 Nov 2012 11:13:03 -0800 (PST)




----- Original Message -----
> From: Sergei Steshenko <address@hidden>
> To: Yury T. <address@hidden>; "address@hidden" <address@hidden>
> Cc: 
> Sent: Tuesday, November 13, 2012 6:31 PM
> Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
> 
> 
> 
> 
> 
> ----- Original Message -----
>>  From: Yury T. <address@hidden>
>>  To: address@hidden
>>  Cc: 
>>  Sent: Tuesday, November 13, 2012 8:58 AM
>>  Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
>> 
>>  Sergei Steshenko-2 wrote
>>>   The matter is not syntax, it's semantics.
>>>   ...
>> 
>>  And semantics (sense) is brought to us by a means of syntax (language),
>>  right? Your example is of course quite appropriate. More might be provided.
>>  :) As the language will hardly change to accomodate multitude of
>>  perceptions, more the reason to provide a better explanation of what is
>>  actually there, yes? :)
>> 
>> 
>> 
> 
> Yuri, my point was/is that Octave data types are very counter-intuitive.
> 
> I'll extend the code sample I've sent to you and the list to illustrate 
> it further - in the next Email.
> 
> Regards,
>   Sergei.
> 
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave
>

Here is the promised modified code and its output:

"
octave:1> system("cat -n /home/sergei/junk/consistent_struct.m");
     1  function os = consistent_struct(varargin)
     2    if(rem(length(varargin),2))
     3      error("there must be even number of input arguments");
     4    endif
     5
     6    os = [];
     7    for struct_field_number = 1:2:length(varargin)
     8      #fprintf(stderr, "key: %s\n", varargin{struct_field_number});
     9      key = varargin{struct_field_number}; # no check of 'key' 
correctness is performed
    10      val = varargin{struct_field_number + 1};
    11      os = setfield(os, key, val);
    12    endfor
    13  endfunction
    14
    15  # testcase for Yury T - assuming Yuri wants to access things by 
"file1*.dat"
    16
    17  par1_1=1;
    18  par2_1=2;
    19  par1_2=3;
    20  par2_2=4;
    21
    22  name = consistent_struct\
    23           (
    24           "file1.dat", {par1_1, par2_1, "ID1"},
    25           "file2.dat", {par1_2, par2_2, "ID2"}
    26           );
    27
    28
    29
    30  getfield(name, "file2.dat"){1}
    31  getfield(name, "file2.dat"){1, 1}
    32  # pay attention - the above two lines produce the same output - 2 * 
crap !!
    33
    34  getfield(name, "file2.dat"){2}
    35  getfield(name, "file2.dat"){1, 2}
    36  # pay attention - the above two lines produce the same output - 2 * 
crap !!
    37
    38  getfield(name, "file2.dat"){3}
    39  getfield(name, "file2.dat"){1, 3}
    40  # pay attention - the above two lines produce the same output - 2 * 
crap !!
    41
    42
    43  name
    44  # pay attention - _two_ indices (like [1,2]) are printed - 2 * crap !!
    45
octave:2> source("/home/sergei/junk/consistent_struct.m");
ans =  3
ans =  3
ans =  4
ans =  4
ans = ID2
ans = ID2
name =

  scalar structure containing the fields:

    file1.dat =
    {
      [1,1] =  1
      [1,2] =  2
      [1,3] = ID1
    }
    file2.dat =
    {
      [1,1] =  3
      [1,2] =  4
      [1,3] = ID2
    }

octave:3> 
".

...

If we are talking about documentation and gotchas, the following approach: 
http://perldoc.perl.org/perltrap.html can be used.

Regards,
  Sergei.


reply via email to

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