473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Comparing version strings within two files

file1 contains data like this:
Expand|Select|Wrap|Line Numbers
  1. C-tree version v8.27.123
  2.  
file2 contains data like this:
Expand|Select|Wrap|Line Numbers
  1. some text......
  2. Database version
  3. C-tree server v8.27.123
  4. some text......
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. my $df = 'D:\serv.pl';
  2. my $df2 ='D:\dbver.pl';
  3.  
  4. open file, "$df" or die "can't open $df";
  5. open file2, "$df2" or die "can't open $df2";
  6.  
  7. @a = <file>;
  8. @a2 = <file2>;
  9.  
  10. foreach $line (@a) {
  11.     if($line =~ m/c-tree Server/i) {
  12.         $f = $line;
  13.         @b = split(/ /, $f);
  14.         foreach $i (@b) {
  15.             print "$i\n";
  16.         }
  17.     }
  18. }
  19.  
  20. foreach $line1 (@a2) {
  21.     if($line1 =~ m/c-tree Server/i) {
  22.         $f2 = $line1;
  23.         @c = split(/ /, $f2);
  24.         foreach $j (@c) {
  25.             foreach $i (@b) {
  26.                 if ($j eq $i) {
  27.                     print"Equal";
  28.                 }
  29.             }
  30.         }
  31.     }
  32. }
  33.  
  34. close (file);
  35. close (file2);
  36.  
Aug 8 '07 #1
2 1917
miller
1,089 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my $df1 = 'D:\serv.pl';
  4. my $df2 = 'D:\dbver.pl';
  5.  
  6. my $version1 = get_ctree_version($df1);
  7. my $version2 = get_ctree_version($df2);
  8.  
  9. if ($version1 eq $version2) {
  10.     print "Equal: $version1\n";
  11. } else {
  12.     print "Not Equal: $version1 vs $version2\n";
  13. }
  14.  
  15. sub get_ctree_version {
  16.     my $file = shift;
  17.  
  18.     open my $fh, $file or die "Can't open $file: $!";
  19.     while (<$fh>) {
  20.         chomp;
  21.         my $line = $_;
  22.  
  23.         if ($line =~ m/^C-tree (?-i:version|server) (\S+)/) {
  24.             return $1;
  25.         }
  26.     }
  27.  
  28.     die "Unable to find version string in $file";
  29. }
  30.  
- Miller
Aug 8 '07 #2
Thanks Miller,
Thanks u very much for you immediate reply....
Aug 8 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Doug | last post by:
I need to compare two "address" structures within a document, and perform some action if they are not equal. The XML document is a purchase order, with an address at both the header and line...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
2
by: darrel | last post by:
I have two comma delimted strings that I need to compare individual values between the two. I assume the solution is likely to put them into an array? If so, do I need to loop through one,...
8
by: Frost | last post by:
Hi All, I am a newbie i have written a c program on unix for line by line comparison for two files now could some one help on how i could do word by word comparison in case both lines have the...
12
by: colincolehour | last post by:
I am new to Python and am working on my first program. I am trying to compare a date I found on a website to todays date. The problem I have is the website only shows 3 letter month name and the...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.