[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: variable.(key) what is?
From: |
Andrew Janke |
Subject: |
Re: variable.(key) what is? |
Date: |
Thu, 19 Dec 2019 14:17:09 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 |
On 12/19/19 2:21 PM, shivax via Help list for GNU Octave wrote:
> hi, i see in example code the .() after variable
>
> Example:
>
> function v = mod09_05_state(key, val)
> persistent state;
> if (nargin == 0)
> % Return the entire struct
> printf("0 \n" );
> v = state;
> elseif (nargin == 1)
> % Return a particular field from the struct
> printf("1 \n" );
> v = state.(key);
> elseif (nargin == 2)
> printf("2 \n" );
> % Set a key value (key must be a string)
> state.(key) = val;
> v = val;
> end
> end
>
> what is .(key)? i dont find in help octave variable
>
It's dynamic field/property access, used on structs and objects. `name =
"foo"; s.(name)` is the same as `s.foo`. Example:
>> s.foo = 42
s =
scalar structure containing the fields:
foo = 42
>> fieldname = "foo";
>> s.(fieldname)
ans = 42
>> s.foo
ans = 42
>>
You can also use it to make structs with weird field names (but you
probably shouldn't):
>> s.("foo bar") = 12345
s =
scalar structure containing the fields:
foo = 42
foo bar = 12345
Cheers,
Andrew