July 23, 2004 : My First Computer Programming

© 安岡喜晴 (JoyShine)

My first experience with computer programming.
I was satisfied because it worked very well.
You need to install "Active Perl" to make this program work.

This program will output the subtracted data as "result.txt", which is the subtraction of "subtraction_data.txt" from "source_data.txt" as below.

source_data.txt
dog
cat
lion
tiger
bird
light
tree
bird
snow
sea
tree
diamond
biohazard
bird
cat
sea
-
subtraction_data.txt
biohazard
sea
bird
cat
tree
snow
cat
sea
bird
cat
biohazard
tree
snow
cat
sea
tree
=
result.txt
diamond
dog
light
lion
tiger

First, I came up with the method of comparing two files by merging those files because I couldn't figure out how to compare two file lines directly.
I found out that duplicate lines appear when the source_data.txt lines have the same lines as the subtraction_data.txt lines after merging two files.
So removing duplicate lines after merging two files seems to work well.
But things happened that I didn't expect.
This method returns the merged data that source_data.txt has and subtraction_data.txt doesn't, and what source_data.txt doesn't have and subtraction_data.txt has.
This is quite natural, but I didn't notice it along the way.
So I added the subtraction_data.txt lines twice and it worked perfectly.

This will automatically remove duplicate lines, but it's limited to 5 duplications.
If you want to increase the limit, change the script line highlighted as green lines.
The more you increase the number of green lines, the higher the limit you will get in the duplication removal function.

open (PRE,">>source_data.txt");
@pre=<PRE>;
seek (PRE,0,2);
print PRE "\n";
close(PRE);
open (PREA,">>subtraction_data.txt");
@pre2=<PREA>;
seek (PREA,0,2);
print PREA "\n";
close(PREA);
open (DATA,"<source_data.txt");
@array=<DATA>;
$list=@array;
#push (@array,"TEMP");
open (DATA2,"<subtraction_data.txt");
@array2=<DATA2>;
$list2=@array2;
#push (@array2,"TEMP");

open (DATAS,">>datatemp.txt"); #The beginning of the method to remove duplicate lines from the file.
open (DATAS2,">>datatemp2.txt");
@array_s=sort @array;
@array2_s=sort @array2;
$array_s=@array_s;
$array2_s=@array2_s;
$max=@array_s;
foreach ($t=0; $t<$max; $t++){
if ("$array_s[$t]" eq "$array_s[$t+1]"){
print DATAS "$array_s[$t]";}
else {
print DATAS "$array_s[$t]";}}
$max=@array2_s;
foreach ($t=0; $t<$max; $t++){
if ("$array2_s[$t]" eq "$array2_s[$t+1]"){
print DATAS2 "$array2_s[$t]";}
else {
print DATAS2 "$array2_s[$t]";}}
close(DATAS);
close(DATAS2);

open (DATATEMP,"<datatemp.txt");
@array1m=<DATATEMP>;
close(DATATEMP);
open (DATATEMP2,"<datatemp2.txt");
@array2m=<DATATEMP2>;
close(DATATEMP2);
open (ABC,">>abc.txt");
open (DE,">>de.txt");
@abc=sort @array1m;
@de=sort @array2m;
$abc=@abc;
$max=@abc;
foreach ($t=0; $t<$max; $t++){
if ("$abc[$t]" eq "$abc[$t+1]"){
$abc[$t+1]="";}}
print ABC @abc;
$de=@de;
$max=@de;
foreach ($t=0; $t<$max; $t++){
if ("$de[$t]" eq "$de[$t+1]"){
$de[$t+1]="";}}
print DE @de;
close(ABC);
close(DE);

open (LPF,">>LPF.txt");
open (CEH,">>CEH.txt");
@LPF=sort @abc;
@CEH=sort @de;
$LPF=@LPF;
$max=@LPF;
foreach ($t=0; $t<$max; $t++){
if ("$LPF[$t]" eq "$LPF[$t+1]"){
$LPF[$t+1]="";}}
print LPF @LPF;
$CEH=@CEH;
$max=@CEH;
foreach ($t=0; $t<$max; $t++){
if ("$CEH[$t]" eq "$CEH[$t+1]"){
$CEH[$t+1]="";}}
print CEH @CEH;
close(LPF);
close(CEH);

open (JBA,">>JBA.txt");
open (LHR,">>LHR.txt");
@JBA=sort @LPF;
@LHR=sort @CEH;
$JBA=@JBA;
$max=@JBA;
foreach ($t=0; $t<$max; $t++){
if ("$JBA[$t]" eq "$JBA[$t+1]"){
$JBA[$t+1]="";}}
print JBA @JBA;
$LHR=@LHR;
$max=@LHR;
foreach ($t=0; $t<$max; $t++){
if ("$LHR[$t]" eq "$LHR[$t+1]"){
$LHR[$t+1]="";}}
print LHR @LHR;
close(JBA);
close(LHR);

open (PHF,">>PHF.txt");
open (PRH,">>PRH.txt");
@PHF=sort @JBA;
@PRH=sort @LHR;
$PHF=@PHF;
$max=@PHF;
foreach ($t=0; $t<$max; $t++){
if ("$PHF[$t]" eq "$PHF[$t+1]"){
$PHF[$t+1]="";}}
print PHF @PHF;
$PRH=@PRH;
$max=@PRH;
foreach ($t=0; $t<$max; $t++){
if ("$PRH[$t]" eq "$PRH[$t+1]"){
$PRH[$t+1]="";}}
print PRH @PRH;
close(PHF);
close(PRH);       #The method for removing duplicate lines within a file ends.


open (DATA3,">>temp.txt");
print DATA3 @PHF;
seek(DATA3,0,2);
print DATA3 @PRH; #Merging the lines of subtraction_data.txt twice.
seek(DATA3,0,2);
print DATA3 @PRH; #The end of the merging of the subtraction_data.txt lines.
close(DATA);
close(DATA2);
close(DATA3);

open (DATA4,"<temp.txt");
@array3=<DATA4>;
@data3s=sort @array3;
close(DATA4);
open(DATA5,">temp2.txt");
print DATA5 @data3s;
close(DATA5);
open(DATA11,"<temp2.txt");
@line=<DATA11>;
$line=@line;
close(DATA11);
open (DATA15,">temp2.txt");
$max=@data3s;
foreach ($i=0; $i<$max; $i++) {
if ("$line[$i]" eq "$line[$i+2]"){
$line[$i]="\n";
$line[$i+1]="\n";
$line[$i+2]="\n";}}
foreach ($t=0; $t<$max; $t++) {
if ("$line[$t]" eq "$line[$t+1]"){
$line[$t]="\n";
$line[$t+1]="\n";}}
print DATA15 @line;
close(DATA15);
unlink "temp.txt";
rename <temp2.txt>,<temp.txt>;
open (DATA15,"<temp.txt");
@array15=<DATA15>;
@data15s=sort @array15;
close(DATA15);
open(DATA15,">temp2.txt");
print DATA15 @data15s;
close(DATA15);
open(DATA11,"<temp2.txt");
@line2=<DATA11>;
$line2=@line2;
close(DATA11);
open (DATA15,">temp2.txt");
$max=@data15s;
foreach ($i=0; $i<$max; $i++) {
if ("$line2[$i]" eq "$line2[$i+1]"){
$line2[$i]="";
$line2[$i+1]="";}}
print DATA15 @line2;
close(DATA15);
unlink "temp.txt";
unlink "abc.txt";
unlink "de.txt";
unlink "datatemp.txt";
unlink "datatemp2.txt";
unlink "CEH.txt";
unlink "JBA.txt";
unlink "LHR.txt";
unlink "LPF.txt";
unlink "PHF.txt";
unlink "PRH.txt";
rename <temp2.txt>,<result.txt>;

Home  |  Profile  |  Software  |  Medicine  |  Diary  |  Photos  |  Contact