Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comaparing two files 3

Status
Not open for further replies.

reishi

Technical User
Jun 25, 2007
19
HK
i have a problem...

there are two files that i need to compare for example:


signal 1 signal 2
a a
b c
c d
d b
e e


signal 1 represents the first file and signal 2 represents the second file. even though that their contents are in no particular order they are still the same. what script can i use to compare these kind of files.

i need a script that would tell me that they are just the same even though they are not arrange in the same order.

or if for example i have seen something like:

signal 1 signal 2
a a
b c
c d
d z
e d
e

how can i make my program to tell that "z" has no partner to the other file.

i could need some help...thank you.
 
If the contents of the two files are lines containing a single alphanumeric word, you can read the first file in a hash, using the word in each line as the key and whatever as the value, then read the second one, checking line by line for existence of the corresponding key in the hash.
If the files may contain special characters, some additional care may be required, though hash keys are relatively tolerant with respect to special characters.

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
yes, thank you for that...but what if the format is like:

signal 1 signal 2
a b c d e f a c d j f e
j i k l b k i l

these should also be the same...what is the script for this kind
 
but how can i print the word with no "partner" on the other file...
 
As your code loops through looking for words with no partners, keep a list of each word that doesn't exist in which file, and then just do an open >> and add the new words to the correct files.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Following my scheme above (step 1:read file 1 in a hash, step 2: read file 2 and check for existence), just increment each hash element in step 2 for every existing key: at the end of step 2 the hash elements with a value of 0 contain the words that exist in file 1 and not in file 2.

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
well, thank you guys for the info...i've been just using perl for at least a month. i'm an electronics engineer and i'm not keen in programming. but my boss ask me to create a program that would compare the netlist of two schematics generated through OrCad. the program should show if the pin of one component is not connected as compared to the other file. It is a layouting tool for PCB. that's where i'm gonna use the program...thanks for the help...
 
another thing can you help me with this one....

what if i have something like this on a txt file:

signal 1
r1 r2 r3 u1 u6 u7
c1 d4 q3
signal 2
l5 r6 r7 r9 u7
u8
signal 3
u10 u5 q2 r6
c1
signal 4
r10 r11 c6 c7 c9
l10

...how can i store in an array, for example the contents of signal 2

i would really apreciate some help...

 
have you tried to write any code reishi?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
yes, but just simple codes...im not familiar with perl....i'd written codes but using C++ and perl is very different...
 
One way, that is very very dependent on your file format:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]diagnostics[/green][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%signals[/blue] = [red]([/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$sig[/blue]  = [red]'[/red][purple][/purple][red]'[/red][red];[/red]
[olive][b]while[/b][/olive][red]([/red]<DATA>[red])[/red][red]{[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
   [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^signal[purple][b]\s[/b][/purple]+([purple][b]\d[/b][/purple]+)[/purple][red]/[/red][red])[/red] [red]{[/red]
      [blue]$sig[/blue] = [red]"[/red][purple]signal [blue]$1[/blue][/purple][red]"[/red][red];[/red]
      [blue]$signals[/blue][red]{[/red][blue]$1[/blue][red]}[/red] = [red][[/red][red]][/red][red];[/red]
   [red]}[/red]
   [olive][b]else[/b][/olive] [red]{[/red]
      [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$signals[/blue][red]{[/red][blue]$sig[/blue][red]}[/red][red]}[/red],[url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red][red])[/red][red];[/red]
   [red]}[/red]
[red]}[/red]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$signal[/blue] [red]([/red][url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%signals[/blue][red])[/red][red]{[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$signal[/blue] = [blue]@[/blue]{[blue]$signals[/blue]{[blue]$signal[/blue]}}[purple][b]\n[/b][/purple][/purple][red]"[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]signal 1[/teal]
[teal]r1 r2 r3 u1 u6 u7[/teal]
[teal]c1 d4 q3[/teal]
[teal]signal 2[/teal]
[teal]l5 r6 r7 r9 u7[/teal]
[teal]u8[/teal]
[teal]signal 3[/teal]
[teal]u10 u5 q2 r6[/teal]
[teal]c1[/teal]
[teal]signal 4[/teal]
[teal]r10 r11 c6 c7 c9[/teal]
[teal]l10[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]diagnostics - Produce verbose warning diagnostics[/li]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
this line:

Code:
      $signals{$1}= [];

should be:

Code:
      $signals{$sig}= [];

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
thank you for the....pls be patient with me always...

i'm just beginning to learn perl...im have a long way to go...
 
thank you for the....pls be patient with me always...

i'm just beginning to learn perl...im have a long way to go...

I think most everyone here will be patient. If you have questions, feel free to ask.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top