Comparing version strings within two files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • umamaheswarig
    New Member
    • Aug 2007
    • 6

    Comparing version strings within two files

    file1 contains data like this:
    Code:
    C-tree version v8.27.123
    file2 contains data like this:
    Code:
    some text......
    Database version
    C-tree server v8.27.123
    some text......
    Now, I want to compare these two files and find out whether the versions of two files are same or not..
    if versions are same we have to print saying equal...else not..

    As am entirely new to perl,I did some coding like this...

    so kindly help me out......

    Here the code I tried:
    [CODE=perl]
    my $df = 'D:\serv.pl';
    my $df2 ='D:\dbver.pl';

    open file, "$df" or die "can't open $df";
    open file2, "$df2" or die "can't open $df2";

    @a = <file>;
    @a2 = <file2>;

    foreach $line (@a) {
    if($line =~ m/c-tree Server/i) {
    $f = $line;
    @b = split(/ /, $f);
    foreach $i (@b) {
    print "$i\n";
    }
    }
    }

    foreach $line1 (@a2) {
    if($line1 =~ m/c-tree Server/i) {
    $f2 = $line1;
    @c = split(/ /, $f2);
    foreach $j (@c) {
    foreach $i (@b) {
    if ($j eq $i) {
    print"Equal";
    }
    }
    }
    }
    }

    close (file);
    close (file2);
    [/CODE]
    Last edited by miller; Aug 8 '07, 05:45 AM. Reason: Code tag
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Note that your first file contains the text "C-tree version", while the second one contains "C-tree server". This may be the source of your problem. You code looks like it's on it's way to working, but I would suggest that you extrapolate the file reading process into a subroutine so that you only have to type it once.

    Here is what I would have in mind:

    [CODE=perl]
    use strict;

    my $df1 = 'D:\serv.pl';
    my $df2 = 'D:\dbver.pl';

    my $version1 = get_ctree_versi on($df1);
    my $version2 = get_ctree_versi on($df2);

    if ($version1 eq $version2) {
    print "Equal: $version1\n";
    } else {
    print "Not Equal: $version1 vs $version2\n";
    }

    sub get_ctree_versi on {
    my $file = shift;

    open my $fh, $file or die "Can't open $file: $!";
    while (<$fh>) {
    chomp;
    my $line = $_;

    if ($line =~ m/^C-tree (?-i:version|serve r) (\S+)/) {
    return $1;
    }
    }

    die "Unable to find version string in $file";
    }
    [/CODE]

    - Miller

    Comment

    • umamaheswarig
      New Member
      • Aug 2007
      • 6

      #3
      Thanks Miller,
      Thanks u very much for you immediate reply....

      Comment

      Working...