
-----------------------------------
keyboardwalker
Mon Dec 30, 2013 2:59 pm

Is Dynamic Record Access Possible?
-----------------------------------
Since it's the holiday season, I'm lazy from all of the food and I'm wishful for cool things.
So this is what I'm wishing for:

type REC_ARRAY_EX : array 1 .. 5 of
    record
        %Name
            1 : string
        %Age
            2 : nat
        %Married
            3 : boolean
    end record
%Access all of the elements of the record
for i : 1 .. upper (REC_ARRAY_EX) %(5)
    %Access all of the fields of the record
    for ii : 1 .. 3 %The Number of Fields
        REC_ARRAY_EX (i).ii := %What the user has entered
        %with appropriate input error checking applied and 
        %proper type casting
    end for
end for


Q1: Is there a way to have numerically named fields in a record that can be accessed via a for?  

Q2: Is there a way to detect the type of a variable? Say I had two variables that have different data types in a record and I wanted to get input into them but one is an irrational number and the other is whole number. I would have to do input error checking and type casting (strrealok/strnatok/strreal/strnat etc) but how would I detect which to do on which variable without doing it with special lines of code for each field?

-----------------------------------
np_123
Mon Dec 30, 2013 4:54 pm

RE:Is Dynamic Record Access Possible?
-----------------------------------
For your second question:

To my knowledge there is no shortcut method of identifying the 'type' of data entered by a user

Based on the type of input you desire, I think you would be forced to do error-checking and type-conversion individually for each different type of input desired.
(strintok, strnatok, strealok, round, etc.)

Remember that some type conversions will be implicit


I have no experience with records, so I cannot help you there

-----------------------------------
Raknarg
Mon Dec 30, 2013 5:17 pm

RE:Is Dynamic Record Access Possible?
-----------------------------------
Generally when you take in input, you always know what the type is. There's nothing to tell you what type it is, unless you create an object to handle that kind of information.

For your first question, as far as I am aware, no. Like i said above you could probably create some kind of object to do that.

-----------------------------------
np_123
Mon Dec 30, 2013 5:39 pm

RE:Is Dynamic Record Access Possible?
-----------------------------------
you don't necessarily know what input type you'll receive from the user unless you assume the user will follow prompts and/or use common sense

by default you could say that all input would be string type, but apart from that, the user could just as easily type 'five' instead of '5', right?

To avoid confusion, I will specify that I am talking solely about what the user is entering, not what type the variable is

-----------------------------------
Dreadnought
Mon Dec 30, 2013 6:26 pm

Re: Is Dynamic Record Access Possible?
-----------------------------------

Q1: Is there a way to have numerically named fields in a record that can be accessed via a for? 

There is no way of doing this that will be more of a blessing than a curse (it's likely going to be MUCH more of a curse). What kind of record are you creating that has so many fields that this is worth it? (saving 2-3 lines isn't necessarily worth it)

Q2: Is there a way to detect the type of a variable? Say I had two variables that have different data types in a record and I wanted to get input into them but one is an irrational number and the other is whole number. I would have to do input error checking and type casting (strrealok/strnatok/strreal/strnat etc) but how would I detect which to do on which variable without doing it with special lines of code for each field?

You ALWAYS know what the type of a variable hence there is no need to have a way of determining it at run-time. (this is different when dealing with objects)
If you feel that you have too much code to handle the input of each field, consider doing something like this
fcn GetRealInput () : real
    var input : string
    loop
        get input
        exit when strrealok (input)
        put "Invalid input, expected a real number"
    end loop
    result strreal (input)
end GetRealInput

% Create other functions GetIntInput, GetNatInput, etc.

% Then we can write

var struct :
    record
        intVal : int
        realVal : real
        natVal : nat
        stringVal : string
        %etc.
    end record


struct.intVal := GetIntInput()
struct.realVal := GetRealInput()
% ...


-----------------------------------
np_123
Mon Dec 30, 2013 6:38 pm

RE:Is Dynamic Record Access Possible?
-----------------------------------
My apologies, I believe i misunderstood the question.
I had assumed the question was detecting the type of the data being entered by the user, seeing as when variables are declared you specify the type. 

Sorry if I caused any confusion

-----------------------------------
keyboardwalker
Mon Dec 30, 2013 6:54 pm

Re: RE:Is Dynamic Record Access Possible?
-----------------------------------
you don't necessarily know what input type you'll receive from the user unless you assume the user will follow prompts and/or use common sense

by default you could say that all input would be string type, but apart from that, the user could just as easily type 'five' instead of '5', right?

To avoid confusion, I will specify that I am talking solely about what the user is entering, not what type the variable is

To rephrase the question, I'm wondering whether you can SAFELY cast a string into a variable that has a changing data type depending on said variable's data type. This would require the data type to be known and the correct input error checking and casting function to be called (strrealok/strnatok/strreal/strnat etc) based on its type. From what I understand, this cannot be done.

Generally when you take in input, you always know what the type is. There's nothing to tell you what type it is, unless you create an object to handle that kind of information.

For your first question, as far as I am aware, no. Like i said above you could probably create some kind of object to do that.

That's unfortunate since I'm unable to apply objects to this project due to the structured paradigm that this project must to adhere to.

-----------------------------------
keyboardwalker
Mon Dec 30, 2013 7:39 pm

Re: Is Dynamic Record Access Possible?
-----------------------------------

Q1: Is there a way to have numerically named fields in a record that can be accessed via a for? 

There is no way of doing this that will be more of a blessing than a curse (it's likely going to be MUCH more of a curse). What kind of record are you creating that has so many fields that this is worth it? (saving 2-3 lines isn't necessarily worth it)

Q2: Is there a way to detect the type of a variable? Say I had two variables that have different data types in a record and I wanted to get input into them but one is an irrational number and the other is whole number. I would have to do input error checking and type casting (strrealok/strnatok/strreal/strnat etc) but how would I detect which to do on which variable without doing it with special lines of code for each field?

You ALWAYS know what the type of a variable hence there is no need to have a way of determining it at run-time. (this is different when dealing with objects)
If you feel that you have too much code to handle the input of each field, consider doing something like this
fcn GetRealInput () : real
    var input : string
    loop
        get input
        exit when strrealok (input)
        put "Invalid input, expected a real number"
    end loop
    result strreal (input)
end GetRealInput

% Create other functions GetIntInput, GetNatInput, etc.

% Then we can write

var struct :
    record
        intVal : int
        realVal : real
        natVal : nat
        stringVal : string
        %etc.
    end record


struct.intVal := GetIntInput()
struct.realVal := GetRealInput()
% ...


I could do something like this that would really limit the number of subprograms that I would have to make.

fcn GetRealInput (byteSize : nat) : real
    var input : string
    loop
        get input
        exit when strrealok (input) %and a real of that byte size can hold that large of a number 
        put "Invalid input, expected a real number"
    end loop
    result strreal (input)
end GetRealInput

% Create other functions GetIntInput, GetNatInput, etc.

% Then we can write

var struct :
    record
        intVal : int
        realVal : real
        natVal : nat
        stringVal : string
        %etc.
    end record


struct.intVal := GetIntInput ()
struct.realVal := GetRealInput (sizeof (struct.intVal))
% ...[/quote]
