Dirk80 has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
I have the following input file (motorola s-record) which is hex-ascii:
S00600004844521B S315FC0000187C631A787C6001243C804C00388400643C S315FC000028908309003C200100382100007C000278FE S315FC0000387C1043A67C1143A67C1243A67C1343A6DC S315FC00004870630000606320004C00012C7C60012476 S315FC0000587C0004AC4C00012C3C6080007C0004ACA9 S315FC0000684C00012C7C70FBA64C00012C7C0004ACDB S315FC0000787C631A787C79FBA6FF80010CFF00010CD7 S315FC000088FE80010CFE00010CFD80010CFD00010C3C S315FC000098FC80010CFC00010C4C00012C48000008FB S315FC0000A83F8000007C6802A6C0030000C023000055 S315FC0000B8C0430000C0630000C0830000C0A300006A S315FC0000C8C0C30000C0E30000C1030000C123000058 S315FC0000D8C1430000C1630000C1830000C1A3000046 S315FC0000E8C1C30000C1E30000C2030000C223000034 S315FC0000F8C2430000C2630000C2830000C2A3000022 S315FC000108C2C30000C2E30000C3030000C32300000F S315FC000118C3430000C3630000C3830000C3A30000FD S315FC000128C3C30000C3E300004C00012C706300004D S315FC0001387C0004AC4C00012C7C6001244C00012C96 S315FC000148706300004C00012C7C6001A44C00012C5F S315FC0001587C6101A44C00012C7C6201A44C00012C9E S315FC0001687C6301A44C00012C7C6401A44C00012C8A S315FC0001787C6501A44C00012C7C6601A44C00012C76 S705FC000018E6
My goal is it to get the data part of the records starting with S3 of this file. Let's take the following S3-Line to show you how an S3 is working. S315FC0000187C631A787C6001243C804C00388400643C. 2 hex ascii digits mean 1 binary byte
So I wrote the following code which seems to work.
#!/usr/bin/perl use strict; use warnings; my $in_file_name = "D:/temp.s3"; my $data = ""; &extractDataFromSrecFile($in_file_name, \$data); print($data,"\n"); sub extractDataFromSrecFile { my ($file_name, $ref_data) = @_; open(my $fh, "<", $file_name) || die "Could not open \"$file_name\ +""; $$ref_data = ""; while( my $srec = <$fh> ) { chomp($srec); my $id = substr($srec,0,2); my $nb_data_bytes = hex(substr($srec,2,2)) - 4 - 1; my $address = substr($srec, 4, 8); if( ($id eq "S3") && ($address ne "FC000000") ) { $$ref_data .= substr($srec, 12, ($nb_data_bytes * 2)); } } close( $fh ); }
But now let's assume that the S3-Records are not ordered correctly by address as before. Because my script just concatenates data the result would be wrong.
Here an example for the same file, but unordered.
S00600004844521B S315FC0000187C631A787C6001243C804C00388400643C S315FC0000387C1043A67C1143A67C1243A67C1343A6DC S315FC000028908309003C200100382100007C000278FE S315FC00004870630000606320004C00012C7C60012476 S315FC0000587C0004AC4C00012C3C6080007C0004ACA9 S315FC0000787C631A787C79FBA6FF80010CFF00010CD7 S315FC000088FE80010CFE00010CFD80010CFD00010C3C S315FC000098FC80010CFC00010C4C00012C48000008FB S315FC0000684C00012C7C70FBA64C00012C7C0004ACDB S315FC0000A83F8000007C6802A6C0030000C023000055 S315FC0000B8C0430000C0630000C0830000C0A300006A S315FC0000C8C0C30000C0E30000C1030000C123000058 S315FC0000D8C1430000C1630000C1830000C1A3000046 S315FC0000E8C1C30000C1E30000C2030000C223000034 S315FC0000F8C2430000C2630000C2830000C2A3000022 S315FC000108C2C30000C2E30000C3030000C32300000F S315FC000118C3430000C3630000C3830000C3A30000FD S315FC000128C3C30000C3E300004C00012C706300004D S315FC0001387C0004AC4C00012C7C6001244C00012C96 S315FC000148706300004C00012C7C6001A44C00012C5F S315FC0001587C6101A44C00012C7C6201A44C00012C9E S315FC0001687C6301A44C00012C7C6401A44C00012C8A S315FC0001787C6501A44C00012C7C6601A44C00012C76 S705FC000018E6
Now my question is how to do it. Is the best way to go once through the input file, sort it by address and then do it as before? But I think that this will have a slow performance. Here you see only a small example. The file can be really huge (e.g. 200 MB)
Or is the better way to just read in a line and then store the data at the right position in memory by means of the address. But now I ask me what shall I take to store the data. A scalar, an array or a hash (key contains address and value the data)? What is the most efficient way to do it.
It should also be possible to check if the data is complete or not. It could e.g. be that one S3-Record is missing and that at a certain address no data is available.
Also regard that it is possible that the lines could have a different length.
Thank you
Dirk
|
|---|