So here's what I think you could best do:

Read both files line by line, and for each line:

  1. split into date+timestamp on one hand, and data on the other:
    my($datetime, $data) = /^(\w+-\d+,[\d:.]+),(.*)/;
  2. push the $data onto an arrayref that's the value in global hash for that $datetime:
    push @{ $data{$datetime} }, $data;

    Note that autovivification makes it unnecessary to create the arrayref before you do this.

OK, so you now have the data in the hash in a form directly usable for your output. One major problem is that hash keys are unordered, so you'll have to sort them. If all dates are the same, a simple alphanumerical sort will sort them by timestamp:

foreach my $key (sort keys %data) { ...

You can simply join the values in the anonymous array, with a comma:

print $key, ',', join(',', @{ $data{$key} }), "\n";

And that should be close!

The whole program:

#! perl -w @ARGV = ('file1.txt', 'file2.txt'); my %data; while(<>) { my($datetime, $data) = /^(\w+-\d+,[\d:.]+),(.*)/; push @{ $data{$datetime} }, $data; } foreach my $key (sort keys %data) { print $key, ',', join(',', @{ $data{$key} }), "\n"; }
With your sample data, this produces:
Feb-21,19:08:05.2,$GP,48.96,90.92,45.69,$BM,3.89 Feb-21,19:08:06.4,$GP,48.92,90.92,45.70 Feb-21,19:08:07.6,$GP,48.93,90.99,45.66,$BM,6.20 Feb-21,19:08:08.1,$GP,48.92,90.95,45.66 Feb-21,19:08:09.0,$GP,48.85,90.92,45.62,$BM,8.52 Feb-21,19:08:10.8,$GP,48.92,90.94,45.63,$BM,9.68 Feb-21,19:21:20.5,$BM,5.05 Feb-21,19:21:20.8,$BM,7.36

In reply to Re: Merge text data by time-stamp by bart
in thread Merge text data by time-stamp by Quinnz

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.