• I want to read two attached files a1.txt and a2.txt simultaneously.
  • I want to get output as mentioned in result.txt file.
  • Through my perl code I am able to read a single file.
  • Please help me to modify my code to get the desired output.

I am not able to type the code. I have attached my perl code in text file (perfresult.txt)

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 );
}

Hi mtr1amit,

I re-wrote your script to get your required output. However, to arrange the output in your result text file is left for the Original Poster to complete.

Here is the script that generate your required output:

#!/usr/bin/perl
use warnings;
use strict;

my %retrieve = (
    1  => q{Retrieve_generic_},
    2  => q{Retrieve_assembly_1_},
    3  => q{Retrieve_assembly_2_},
    4  => q{300_wireframe_view_},
    5  => q{80_wireframe_view_},
    6  => q{3_hidden_view_},
    7  => q{Fast_HLR_},
    8  => q{120_hidden_view_},
    9  => q{shaded_view_},
    10 => q{shaded_mouse_},
    11 => q{realtime_rendered_},
);

my $file1 = read_file( 'a1.txt', \%retrieve );
my $file2 = read_file( 'a2.txt', \%retrieve );

print generate_output($file1);  # print out match string from file1
print generate_output($file2);  # print out match string from file2

sub read_file {
    my ( $filename, $matched_data_to_retrieve ) = @_;

    my $msg_href = {};

    open my $fh, '<', $filename or die "can't open file:$!";
    while (<$fh>) {
        chomp;
        foreach my $matched_str ( keys %{$matched_data_to_retrieve} ) {
            if (/$matched_data_to_retrieve->{$matched_str}/) {
                my @data_arr = split;
                push @{ $msg_href->{ $data_arr[0] } }, $data_arr[1];
            }
        }
    }
    return $msg_href;
}

sub generate_output {
    my $final_data = shift;
    my $msg_string;

    foreach ( keys %{$final_data} ) {
        $msg_string .= $_;
        my $tm;
        do {
            my $total = 0;
            for ( @{ $final_data->{$_} } ) {
                my ( $hr, $min, $sec ) = split /:/, $_;
                $total += $hr * 3600 + $min * 60 + $sec;
            }
            my $hms = secondsToHMS( $total / scalar @{ $final_data->{$_} } );
            $tm .= $hms . $/;
        };
        $msg_string .=' ' . $tm;
    }
    return $msg_string, $/;
}

sub secondsToHMS {
    my $seconds = shift;
    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 );
}

Hope this help.
Please, if your have some other questions you may also ask.
Thank you

Thanks for your reply.

I want to pass a1.txt and a2.txt with their path from command line. Both files are in different directory.May you help me to do it in above code.

Thank you,

Hi,

I want to pass a1.txt and a2.txt with their path from command line. Both files are in different directory

Say you are doing this from the Command Line Interface:
perlscript_name.pl directory_to_file_a1.txt directory_to_file_a2.txt

The above directory_to_file_... either of the files are in the array variable @ARGV, so from your code, change Line 19 and 20 from the pervious code which is

my $file1 = read_file( 'a1.txt', \%retrieve );
my $file2 = read_file( 'a2.txt', \%retrieve );

to this:

my $file1 = read_file( $ARGV[0], \%retrieve );
my $file2 = read_file( $ARGV[1], \%retrieve );

that should do.

Hi,

I want to parse path of filename that I am passing through command prompt.
e.g e:\perf_result\P-10-20\a1.txt
ARGV[0] and ARGV[1] contains full path including filename.
Here I just want to grep filename a1.txt.

Here I just want to grep filename a1.txt.

use File::Basename qw(basename);

my $file_input1 = basename($ARGV[0]);  # this gives a1.txt
my $file_input2 = basename($ARGV[1]);  # this gives a2.txt

my $file1 = read_file( $file_input1, \%retrieve );
my $file2 = read_file( $file_input2, \%retrieve );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.