0

i'm currently tasked with translating an old pascal script to python. Problem is, i don't have any of experience with pascal... Until now, all went fine (most of the script is pretty self-explanatory), but now i encountered a small snippet of code which i just can't figure out:

# some other code here...

type MeasurementPoint = record
                         lambda : double;
                         value : double;
                        end;

# some more code...

procedure foo(MyFileName: string, somemoreargs):
var  somevars: integer; 
     somemorevars: double;
     temp: MeasurementPoint; 

# even more code...

  i:= 0;
  Assign(MyInFile,MyFileName);
  Reset(MyInFile);
  repeat
    Inc(i);
    SetLength(Reflexion, i);
    readln(MyInFile, temp.lambda, temp.value);
    Reflexion[i-1]:=temp;
  until EoF(MyInFile);
  Close(MyInFile);

I just cant't wrap my head around what this part of code is supposed to do... I understand so much that the full file MyInFile is being read line after line, and that each line contains two values, namely 'lambda' and 'value', which are extracted as doubles.

According to the pascal wiki, record is a container able to hold objects of different types and logically group them together (could this be compared to a dictionary in python?). If i understand SetLength correctly, it is used to definde the length of an array, which makes sense here. i is increased every time a line is read, so the length of temp is increased to fit the number of lambda and value pairs already extracted from the file (please correct me if my assumptions are wrong!). But i don't understand what the rest of the code is supposed to do, especially Reflexion[i-1]:=temp. Is Reflexion an object of the type MeasurementPoint with length i-1? Or what does that part mean? Why are lambda and value extracted as temp.lambda and temp.value? does this automatically pair them together in a record...?

If anyone could help me by explaining this, i would highly appreciate it. And, of course, if you happen to have any idea how this could be translated into python 3.x, it would be even better ;-)

Flob
  • 898
  • 1
  • 5
  • 14

1 Answers1

1

Pascal's record is like Python's namedtuple. One record (namedtuple) is read field by field (hence temp.lambda, temp.value) in readLn function.

Reflexion[i-1]:=temp looks like an array (Python's list) of records. Since arrays have a constant length (declared at initialization; n-1) the new records are stored in the consecutive slots.

I hope this answers your questions about what the Pascal code does...

sophros
  • 14,672
  • 11
  • 46
  • 75
  • It definitely gave me some more insight, thank you! And i just learned about named tuples through you... so i'm even more thankful. – Flob Nov 09 '18 at 09:10