Parsing unsorted data from large fixed width text

后端 未结 4 659
时光取名叫无心
时光取名叫无心 2021-01-19 07:09

I am mostly a Matlab user and a Perl n00b. This is my first Perl script.

I have a large fixed width data file that I would like to process into a binary file with a

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-19 07:27

    First, you should always have 'use strict;' and 'use warnings;' pragmas in your script.

    It seems like you need a simple array (@param_name) for reference, so loading those values would be straight forward as you have it. (again, adding the above pragmas would start showing you errors, including the $line = =~ s/^\s+//; line!)

    I suggest you read this, to understand how you can load your data file into a Hash of Hashes. Once you've designed the hash, you simply read and load the file data contents, and then iterate through the contents of the hash.

    For example, using time as the key for the hash

    %HoH = (
        1 => {
            name   => "Param1",
            ty       => "UI",
            value       => "5",
        },
        2 => {
            name   => "Param3",
            ty       => "TXT",
            value       => "Some Text 1",
        },
        3 => {
            name   => "Param1",
            ty       => "UI",
            value       => "10",
        },
    );
    

    Make sure you close the INFILE after reading in the contents, before you start processing.

    So in the end, you iterate over the hash, and reference the array (instead of the file contents) for your output writes - I would imagine it would be much faster to do this.

    Let me know if you need more info.

    Note: if you go this route, include Data:Dumper - a significant help to printing and understanding the data in your hash!

提交回复
热议问题