* Newbie question # 2
@ 2020-08-06 18:40 Ian Douglas
2020-08-06 18:56 ` Simon Wright
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Ian Douglas @ 2020-08-06 18:40 UTC (permalink / raw)
Hi all
I did try Google search and assorted books but could not find answer.
In PHP, let's say we have a variable $fruit which contains the string "banana".
In PHP, if I do $$fruit, then it creates a variable $banana, which I can then do things with.
Does Ada support any such concept of taking the contents of one variable and using THAT as a variable?
I'm reading in a file which has a name of an object followed by some properties so I want to use the name as a variable ...
File is something I created, so it's not some random stuff, and the variables will be existing already.
Thanks, Ian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question # 2
2020-08-06 18:40 Newbie question # 2 Ian Douglas
@ 2020-08-06 18:56 ` Simon Wright
2020-08-06 19:41 ` Ian Douglas
2020-08-06 19:20 ` Niklas Holsti
2020-08-06 19:41 ` Dmitry A. Kazakov
2 siblings, 1 reply; 9+ messages in thread
From: Simon Wright @ 2020-08-06 18:56 UTC (permalink / raw)
Ian Douglas <ian@vionia.com> writes:
> In PHP, let's say we have a variable $fruit which contains the string
> "banana".
>
> In PHP, if I do $$fruit, then it creates a variable $banana, which I
> can then do things with.
>
> Does Ada support any such concept of taking the contents of one
> variable and using THAT as a variable?
>
> I'm reading in a file which has a name of an object followed by some
> properties so I want to use the name as a variable ... File is
> something I created, so it's not some random stuff, and the variables
> will be existing already.
I'd think of a record type to contain the properties, and then a map
from object name to properties:
type Properties is record
Length : Positive;
Width : Positive;
end record;
package Object_Maps is new Ada.Containers.Indefinite_Ordered_Maps
(Key_Type => String,
Element_Type => Properties);
Objects : Object_Maps.Map;
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question # 2
2020-08-06 18:40 Newbie question # 2 Ian Douglas
2020-08-06 18:56 ` Simon Wright
@ 2020-08-06 19:20 ` Niklas Holsti
2020-08-06 19:45 ` Ian Douglas
2020-08-06 19:41 ` Dmitry A. Kazakov
2 siblings, 1 reply; 9+ messages in thread
From: Niklas Holsti @ 2020-08-06 19:20 UTC (permalink / raw)
On 2020-08-06 21:40, Ian Douglas wrote:
> Hi all
>
> I did try Google search and assorted books but could not find
> answer.
>
> In PHP, let's say we have a variable $fruit which contains the string
> "banana".
>
> In PHP, if I do $$fruit, then it creates a variable $banana, which I
> can then do things with.
Fortran has a similar feature, NAMELIST, for reading values into
variables also named in the input. It can also be used for output.
Ada does not have such a feature.
> Does Ada support any such concept of taking the contents of one
> variable and using THAT as a variable?
The closest thing in Ada is a variable of an access type, which can be
set to refer to some other variable. However, access types do not work
with the variable's (string) identifier, but use its run-time memory
address (the 'Access attribute).
> I'm reading in a file which has a name of an object followed by some
> properties so I want to use the name as a variable ... File is
> something I created, so it's not some random stuff, and the variables
> will be existing already.
In Ada, you, yourself, have to program the mapping from the name
(string) to the variable. This can be as simple as an if-then-else cascade:
if Name = "foo" then
Set_Properties (foo);
-- Where "foo" is the identifier of a variable.
elsif Name = "bar" then
Set_Properties (bar);
...
end if;
Alternatively, you can implement the mapping with a map container from
the Ada container library, as Simon Wright suggested. However, if the
variables already exist and are used directly in various parts of your
program, you may want to store, in the map, /accesses/ to the existing
variables, instead of using the map's elements themselves as these
variables.
--
Niklas Holsti
niklas holsti tidorum fi
. @ .
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question # 2
2020-08-06 18:40 Newbie question # 2 Ian Douglas
2020-08-06 18:56 ` Simon Wright
2020-08-06 19:20 ` Niklas Holsti
@ 2020-08-06 19:41 ` Dmitry A. Kazakov
2 siblings, 0 replies; 9+ messages in thread
From: Dmitry A. Kazakov @ 2020-08-06 19:41 UTC (permalink / raw)
On 06/08/2020 20:40, Ian Douglas wrote:
> I did try Google search and assorted books but could not find answer.
>
> In PHP, let's say we have a variable $fruit which contains the string "banana".
>
> In PHP, if I do $$fruit, then it creates a variable $banana, which I can then do things with.
You cannot, because you do not know its name before program run, so you
will have to keep on using indirection "$fruit" rather than the direct
name "banana".
> Does Ada support any such concept of taking the contents of one variable and using THAT as a variable?
Sure. Indirection is supported in almost all known programming
languages. You can have a map: string->value. Here string is "banana",
or an array: index->value etc.
> I'm reading in a file which has a name of an object followed by some properties so I want to use the name as a variable ...
> File is something I created, so it's not some random stuff, and the variables will be existing already.
This would be filling a predefined map reading it from the file in the
form of (key,value) pairs. Just deserialization of a map with fixed keys.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question # 2
2020-08-06 18:56 ` Simon Wright
@ 2020-08-06 19:41 ` Ian Douglas
2020-08-06 21:10 ` Simon Wright
0 siblings, 1 reply; 9+ messages in thread
From: Ian Douglas @ 2020-08-06 19:41 UTC (permalink / raw)
On Thursday, 6 August 2020 20:56:25 UTC+2, Simon Wright wrote:
>
> I'd think of a record type to contain the properties, and then a map
> from object name to properties:
Yes, the variables are actually records.
>
> package Object_Maps is new Ada.Containers.Indefinite_Ordered_Maps
> (Key_Type => String,
> Element_Type => Properties);
>
> Objects : Object_Maps.Map;
Okay that's a new construct I haven't come across yet. Let me see what I can dig up on that.
Thanks, Ian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question # 2
2020-08-06 19:20 ` Niklas Holsti
@ 2020-08-06 19:45 ` Ian Douglas
2020-08-06 20:08 ` Niklas Holsti
0 siblings, 1 reply; 9+ messages in thread
From: Ian Douglas @ 2020-08-06 19:45 UTC (permalink / raw)
On Thursday, 6 August 2020 21:20:15 UTC+2, Niklas Holsti wrote:
>
> Ada does not have such a feature.
I figured as much, probably "unsafe programming practice" at the end of the day.
> In Ada, you, yourself, have to program the mapping from the name
> (string) to the variable. This can be as simple as an if-then-else cascade:
Yes, I was trying to avoid cascading Ifs or case statement :-)
In this case there are only 5 things so a case statement would be the quickest/easiest, but it would be good to have a better technique for when there are more.
Thanks, Ian
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question # 2
2020-08-06 19:45 ` Ian Douglas
@ 2020-08-06 20:08 ` Niklas Holsti
0 siblings, 0 replies; 9+ messages in thread
From: Niklas Holsti @ 2020-08-06 20:08 UTC (permalink / raw)
On 2020-08-06 22:45, Ian Douglas wrote:
> On Thursday, 6 August 2020 21:20:15 UTC+2, Niklas Holsti wrote:
>>
>> Ada does not have such a feature.
>
> I figured as much, probably "unsafe programming practice" at the end of the day.
I wouldn't say so. I think "namelist" input is a perfectly reasonable
function to have in some programs, and is not particularly unsafe in any
way -- if the programmer can limit the set of variables that can be
named and changed by such input, which is the case in Fortran (and also
in our various suggestions for implementing it in Ada).
PHP is (I believe) an interpreted language, so the symbol table is
around at run-time, which makes it easy for PHP to support variables
that refer to any other variable by its symbolic name. This can make
"namelist" input in PHP unsafe, since the input can change any variable
-- including variables that the programmer did not intend to be
changeable in this way.
Ada is usually compiled, and the symbol table is not present when the
compiled program runs, so it would be harder to implement a "namelist"
input/output feature. But not impossible, as Fortran shows.
--
Niklas Holsti
niklas holsti tidorum fi
. @ .
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question # 2
2020-08-06 19:41 ` Ian Douglas
@ 2020-08-06 21:10 ` Simon Wright
2020-08-07 11:28 ` Ian Douglas
0 siblings, 1 reply; 9+ messages in thread
From: Simon Wright @ 2020-08-06 21:10 UTC (permalink / raw)
Ian Douglas <ian@vionia.com> writes:
> Okay that's a new construct I haven't come across yet. Let me see what
> I can dig up on that.
ARM 18.14 The Generic Package Containers.Indefinite_Ordered_Maps
http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-18-14.html
which is like
A.18.6 The Generic Package Containers.Ordered_Maps
http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-18-6.html
where almost all the operations are defined; the reason for the
Indefinite version is that the key and element types can be indefinite,
whih you probably wouldn't need if it wasn't that the key in my
suggestion is of type String, which is indefinite (i.e. you can have key
strings, or in your case names, of different lengths).
There's a discussion of maps in the Rationale for Ada 2005 section 8.3.
https://www.adaic.org/resources/add_content/standards/05rat/html/Rat-8-3.html
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Newbie question # 2
2020-08-06 21:10 ` Simon Wright
@ 2020-08-07 11:28 ` Ian Douglas
0 siblings, 0 replies; 9+ messages in thread
From: Ian Douglas @ 2020-08-07 11:28 UTC (permalink / raw)
On Thursday, 6 August 2020 23:10:52 UTC+2, Simon Wright wrote:
> Ian Douglas writes:
>
> > Okay that's a new construct I haven't come across yet. Let me see what
> > I can dig up on that.
>
> ARM 18.14 The Generic Package Containers.Indefinite_Ordered_Maps
> http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-18-14.html
>
> which is like
>
> A.18.6 The Generic Package Containers.Ordered_Maps
> http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-18-6.html
>
Thanks Simon, will investigate.
Cheers, Ian
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2020-08-07 11:28 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-06 18:40 Newbie question # 2 Ian Douglas
2020-08-06 18:56 ` Simon Wright
2020-08-06 19:41 ` Ian Douglas
2020-08-06 21:10 ` Simon Wright
2020-08-07 11:28 ` Ian Douglas
2020-08-06 19:20 ` Niklas Holsti
2020-08-06 19:45 ` Ian Douglas
2020-08-06 20:08 ` Niklas Holsti
2020-08-06 19:41 ` Dmitry A. Kazakov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox