How to merge columns from two different files in perl?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amit1mtr
    New Member
    • Dec 2012
    • 2

    How to merge columns from two different files in perl?

    Hi,

    I have written a following perl script to read particular entities from a text file and avg their timestamp.( I have attached this code as perfresult.txt)

    Code:
    use strict;
    
    my %retrieve;
    my $count = 0;
    
      my $file1 = 'a1.txt';
      open (R, $file1) or  die ("Could not open $file1!");
    
      while (<R>) {
    
            next unless /^*Retrieve_generic_/ ||
                      /^*Retrieve_assembly_1_/ ||
                      /^*Retrieve_assembly_2_/ ||
                      /^*300_wireframe_view_/ || 
                      /^*80_wireframe_view_/ ||
                      /^*3_hidden_view_/ || 
                      /^*Fast_HLR_/ || 
                      /^*120_hidden_view_/ ||
                      /^*shaded_view_/ ||
                      /^*shaded_mouse_/ || 
                      /^*realtime_rendered_/;
          $count++;
          my ( $retrieve, $time ) = split;
          my ( $h, $m, $s ) = split ':', $time;
          $retrieve{$retrieve} += $h * 3600 + $m * 60 + $s;
    
      }
      close(R);
      for my $retrieve ( keys %retrieve ) {
          my $hms = secondsToHMS($retrieve{$retrieve} / ( 
          3));
          print "$retrieve\t$hms\n" if defined $hms;
       }
    
      # For seconds < 86400, else undef returned
      sub secondsToHMS {
          my $seconds = $_[0]; 
          return undef if $seconds >= 86400;
    
          my $h = int $seconds / 3600;
          my $m = int( $seconds - $h * 3600 ) / 60;
          my $s = $seconds % 60;
    
          return sprintf( '%02d:%02d:%02d', $h, $m, $s );
      }
    - I want read to two files (a1.txt and a2.txt)
    simultaneously and compare the entities to get the
    timestamp difference.(I have attached result.txt for
    the output format)
    - Please help me to modify my code to get desired output.
    Attached Files
    Last edited by acoder; Dec 15 '12, 09:46 PM. Reason: Fixed code tags
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Bytes is not a code writing nor homework service. With the way your current post is written, it is just shy of violating the posting guidlines.

    If you will provide the details as to what you are currently getting verses what your current results are then I'm sure someone will nudge you in the right direction.

    Comment

    Working...