CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [StaedarN] script not producing output: Edit Log



FishMonger
Veteran / Moderator

May 11, 2013, 1:11 PM


Views: 878
Re: [StaedarN] script not producing output

I made a few adjustments. See if this works better.


Code
#!C:\Perl64\bin 

use strict;
use warnings;
use Getopt::Long;

my $first = "Windows7-Snapshot9.vmem";
my $second = "Windows7-Snapshot13.vmem";
my $verbose = 1; ## Any Debugging and other info
my $diff = 0; ## print all the pages with differences
my $ident = 0; ## Print all the pages which are identical
my $summary = 1; ## Print the statistical summary
my $size = 4096;
my $help = 0;

GetOptions( "help|?" => \$help,
"size=i" => \$size,
"verbose+" => \$verbose,
"diff" => \$diff,
"ident" => \$ident,
"summary" => \$summary,
"first|f=s" => \$first,
"second|s=s" => \$second,
) or die "Error in command line arguments\n";

unless (-e $first and -e $second) {
die "One of the required input files can't be found\n";
}

if( -s $first != -s $second )
{
die "Files are not of equal size. Can not proceed\n";
}

# Perl Best Practice is to use the 3 arg form of open
# and a lexical var for the filehandle instead of a bareword
open my $first_fh, '<', $first or die "Could not open: '$first' $!";
open my $second_fh, '<', $second or die "Could not open: '$second' $!";
#open FIRST, "< ".$first or die "Could not open: ".$first."\n";
#open SECOND, "< ".$second or die "Could not open: ".$second."\n";


my $wordcount = 0;
my $pagenumber = 0;
my $pagecount = 0;
my $diffcount = 0;
my $fline;
my $sline;


while( read( $first_fh, $fline, $size ) && read( $second_fh, $sline, $size ) )
{
print "Comparing $size bytes\n" if $verbose;

$pagenumber += $size ;
$pagecount++;

# why are youy using the square brackets in the unpack statements?
# I suspect that they are unneeded/unwanted
# my @fcmpare = unpack("C[$size]", $fline );
# my @scmpare = unpack("C[$size]", $sline );
my @fcmpare = unpack("C$size", $fline );
my @scmpare = unpack("C$size", $sline );

my $nodiff = 0;

# perl's for loop construct is cleaner and more efficient than the C style
for my $index (0..$size-1)
#for( $index = 0; $index < $size ; $index++)
{
# is your intension to apply a regex pattern match
# or did you mean to test for equality?
$nodiff++ if( $fcmpare[$index] !~ $scmpare[$index] )
# $nodiff++ if( $fcmpare[$index] !~ /$scmpare[$index]/ )
# $nodiff++ if( $fcmpare[$index] ne $scmpare[$index] )
}

$diffcount++ if $nodiff;

if( $nodiff && $diff )
{
printf("\n%d : %x : Differences found\n", $pagecount, $pagenumber);
}
elsif( $ident and not $nodiff)
{
printf("\n%d : %x : No Differences found\n", $pagecount, $pagenumber);
}

if( $verbose )
{
printf("Completed page %d : %x\n", $pagecount, $pagenumber);
}

}

if( $summary )
{
print '#' x 30, "\n",
"Total pages read = $pagecount \n",
"Total different pages found = $diffcount\n",
"Percentage Different = ".($diffcount / $pagecount * 100)."\n";
}

close $first_fh;
close $second_fh;



(This post was edited by FishMonger on May 11, 2013, 2:18 PM)


Edit Log:
Post edited by FishMonger (Veteran) on May 11, 2013, 2:18 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives