This is so weird. I was just working with S-record files yesterday. Here's the start of my S-record parser.

#!/usr/bin/perl use strict; use warnings; process( $ARGV[0] ); sub process { my $file = shift; # First, grab the contents of the firmware hex (S19) file my $contents = slurp( $file ); my @lines = split /\n/, $contents; my %data; for my $line ( @lines ) { next if $line =~ /^S0/; # skip comments next if $line =~ /^S7/; # skip 32-bit termination records next if $line =~ /^S8/; # skip 24-bit termination records next if $line =~ /^S9/; # skop 16-bit termination records # We could do all sorts of things to bulletproof this but it w +orks my ( $rectype, $length, $address, $data, $checksum ) = $line =~ / ^ # start of line (S[123]) # S1, S2, or S3 record ([0-9A-F]{2}) # record length including the address +and checksum ([0-9A-F]{8}) # address ([0-9A-F]+) # data or payload ([0-9A-F]{2}) # checksum \r?$ # end of line /x; my @bytes = unpack "(A2)*", $data; my $numbytes = scalar @bytes; my $nextaddr = sprintf( "%X", hex( $address ) + $numbytes ); $data{ $address } = { rectype => $rectype, length => $length, data => $data, checksum => $checksum, bytes => [ @bytes ], numbytes => $numbytes, nextaddr => $nextaddr, }; } my @ordered = sort { hex( $a ) <=> hex( $b ) } keys %data; for my $recno ( 0 .. $#ordered ) { my $curraddr = $ordered[ $recno ]; if ( ! exists $data{ $curraddr } ) { print "$curraddr <no data found>\n"; next; } my $rec = $data{ $curraddr }; my $nextrec = $ordered[ $recno + 1 ]; print "$rec->{rectype} $curraddr $rec->{length} $rec->{data} $ +rec->{checksum}\n"; if ( defined $nextrec && $nextrec ne $rec->{nextaddr} ) { print " <no data until $nextrec>\n"; } } } sub slurp { my $file = shift; my $text = do { local( @ARGV, $/ ) = $file ; <> }; return $text; }

In reply to Re: Storing unordered data from file in memory by Mr. Muskrat
in thread Storing unordered data from file in memory by Dirk80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.