oomwrtu has asked for the wisdom of the Perl Monks concerning the following question:
If the ids are not the same, I don't want it to write anything to the final file unless one of the ids was blank, but I do want to write them if they are the same, such as:File 1: File 2: 0001,aname,bname 0001,aname,bname 0002,cname,bname 0003,aname,bname 0004,dname,bname 0005,fname,bname 0005,dname,bname
I can do this for about 900 of the approximately 1700 lines I have in each of the files before it just stops doing anything. My code is below (shortened for this post), along with snippets from two of the files I would like to compare: - - - solved using the code below, see my message about editing it, please.Final: 0001,aname,bname 0002,cname,bname 0004,dname,bname
where a parsed file follows the form:use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); print "Cache-Control: max-age=30\n"; my %final; my %compare1; my %compare2; my %compare3; my $maxid = "2000"; open(DAT, "data/parsed-Black_Dragon.txt"); my @data = <DAT>; close(DAT); for(my $i = 0; $i < scalar(@data); $i++) { # dump file data into ha +shed array my $id = substr($data[$i], 0, 4); # get current planet id $compare1{$id} = $data[$i]; delete $data[$i]; } @data = (); open(DAT, "data/parsed-BMoom.txt"); @data = <DAT>; close(DAT); for(my $i = 0; $i < scalar(@data); $i++) { # dump file data into ha +shed array my $id = substr($data[$i], 0, 4); # get current planet id $compare2{$id} = $data[$i]; delete $data[$i]; } @data = (); open(DAT, "data/parsed-Litex.txt"); @data = <DAT>; close(DAT); for(my $i = 0; $i < scalar(@data); $i++) { # dump file data into ha +shed array my $id = substr($data[$i], 0, 4); # get current planet id $compare3{$id} = $data[$i]; delete $data[$i]; } @data = (); open(DAT,">data/parsed-all.txt"); # open appropriate parsed file an +d clear it close(DAT); for(my $i = 1; $i <= $maxid; $i++) { my $currid = changeID($i); my $delid = changeID($i - 1); delete $compare1{$delid}; delete $compare2{$delid}; delete $compare3{$delid}; next if( defined $compare1{$currid} && defined $compare2{$currid} && defined $compare3{$currid} && $compare1{$currid} ne $compare2{$currid} && $compare1{$currid} ne $compare3{$currid} && $compare2{$currid} ne $compare3{$currid} ); open(DAT,">>data/parsed-all.txt"); if( defined $compare1{$currid} && !defined $compare2{$currid} && ! +defined $compare2{$currid} ) { print DAT $compare1{$currid}; next; } if( defined $compare2{$currid} && !defined $compare1{$currid} && ! +defined $compare3{$currid} ) { print DAT $compare2{$currid}; next; } if( defined $compare3{$currid} && !defined $compare1{$currid} && ! +defined $compare2{$currid} ) { print DAT $compare2{$currid}; next; } if( defined $compare1{$currid} && defined $compare2{$currid} ) { if( $compare1{$currid} eq $compare2{$currid} ) { print DAT $compare1{$currid}; next; } } if( defined $compare1{$currid} && defined $compare3{$currid} ) { if( $compare1{$currid} eq $compare3{$currid} ) { print DAT $compare1{$currid}; next; } } if( defined $compare2{$currid} && defined $compare3{$currid} ) { if( $compare2{$currid} eq $compare3{$currid} ) { print DAT $compare2{$currid}; next; } } close(DAT); } print "Location: planetDiscovered.cgi\n\n"; exit; sub changeID { return sprintf "%04d", $_[0]; }
0001,Nunki 2 1,5847,71%,0.71,4151.37,ThrevenGuard,-18,-26
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Comparing lines of multiple files
by Zed_Lopez (Chaplain) on Oct 09, 2005 at 19:26 UTC | |
Re: Comparing lines of multiple files
by graff (Chancellor) on Oct 09, 2005 at 19:43 UTC | |
by Zed_Lopez (Chaplain) on Oct 09, 2005 at 19:52 UTC | |
by oomwrtu (Novice) on Oct 09, 2005 at 23:06 UTC | |
by Tortue (Scribe) on Oct 11, 2005 at 11:02 UTC | |
Re: Comparing lines of multiple files
by GrandFather (Saint) on Oct 09, 2005 at 22:53 UTC | |
Re: Comparing lines of multiple files
by GrandFather (Saint) on Oct 09, 2005 at 22:39 UTC | |
Re: Comparing lines of multiple files
by EvanCarroll (Chaplain) on Oct 09, 2005 at 21:52 UTC |