Mercurial > hg > octave-lyh
comparison examples/structdemo.cc @ 11051:2204120e38d2
structdemo.cc: use octave_scalar_map instead of Octave_map
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 29 Sep 2010 04:23:13 -0400 |
parents | 6cb30a539481 |
children | db1f49eaba6b |
comparison
equal
deleted
inserted
replaced
11050:b1ee705aef45 | 11051:2204120e38d2 |
---|---|
4 DEFUN_DLD (structdemo, args, , "Struct demo.") | 4 DEFUN_DLD (structdemo, args, , "Struct demo.") |
5 { | 5 { |
6 int nargin = args.length (); | 6 int nargin = args.length (); |
7 octave_value retval; | 7 octave_value retval; |
8 | 8 |
9 if (nargin != 2) | 9 if (args.length () == 2) |
10 { | |
11 octave_scalar_map arg0 = args(0).scalar_map_value (); | |
12 | |
13 if (! error_state) | |
14 { | |
15 std::string arg1 = args(1).string_value (); | |
16 | |
17 if (! error_state) | |
18 { | |
19 octave_value tmp = arg0.contents (arg1); | |
20 | |
21 if (tmp.is_defined ()) | |
22 { | |
23 octave_scalar_map st; | |
24 | |
25 st.assign ("selected", tmp); | |
26 | |
27 retval = octave_value (st); | |
28 } | |
29 else | |
30 error ("sruct does not contain field named '%s'\n", | |
31 arg1.c_str ()); | |
32 } | |
33 else | |
34 error ("expecting character string as second argument"); | |
35 } | |
36 else | |
37 error ("expecting struct as first argument"); | |
38 } | |
39 else | |
10 print_usage (); | 40 print_usage (); |
11 else | |
12 { | |
13 Octave_map arg0 = args(0).map_value (); | |
14 std::string arg1 = args(1).string_value (); | |
15 | 41 |
16 if (! error_state && arg0.contains (arg1)) | |
17 { | |
18 // The following two lines might be written as | |
19 // octave_value tmp; | |
20 // for (Octave_map::iterator p0 = | |
21 // arg0.begin(); | |
22 // p0 != arg0.end(); p0++ ) | |
23 // if (arg0.key (p0) == arg1) | |
24 // { | |
25 // tmp = arg0.contents (p0) (0); | |
26 // break; | |
27 // } | |
28 // though using seek is more concise. | |
29 Octave_map::const_iterator p1 = arg0.seek (arg1); | |
30 octave_value tmp = arg0.contents(p1)(0); | |
31 Octave_map st; | |
32 st.assign ("selected", tmp); | |
33 retval = octave_value (st); | |
34 } | |
35 } | |
36 return retval; | 42 return retval; |
37 } | 43 } |
38 | 44 |