I am working on a simple loop but it is not working. I have 2 files that I compare on a normal ID basis. This works because it results first and the first result only outputs! So it is not looping through the rest of the files (or the final file because the output contains all the rows of the first file). It outputs all the lines from the first file but it only adds the first result to the second file in the outfile. To clarify, my code is:
Use strict; Use warnings; My $ inputfile1 = shift || Die "input and output! \ N"; My $ inputfile2 = shift || Die "input and output! \ N"; My $ outputfile = shift || Die "input! \ N"; Open my INFILE1, '& lt;', INFOFile1 or die "in use / did not get found: $! \ N"; Open my $ INFILE2, '& lt;', $ inputfile2 or die "in use / did not get found: $! \ N"; Open my $ OUTFILE, '& gt;', $ output file or die "in use / not found: $! \ N"; While (& lt; $ INFILE1 & gt;) {s / "//g; my @elements = split /; /, $ _; while (& lt; $ INFILE2 & gt;) {s /" // g; My @Lopelments = Partition /; /, $ _; If ($ element [11] ek $ looplaments [0]) {$ element [12] = $ loopimments [1]; $ Element [13] = $ loopimments [2]; }} My $ output_line = join (";", @elements); Print output $ $ output line; # Print "\ n"} Close $ INFILE1; About $ INFILE2; About $ OUTFILE; Exit 0; My first attempt was to make this piece of code, and why it worked partially?: It crashes through half way. When I check out the output file which prevents it around halfway and does not know why! And on one side note, I think that is a low efficiĆ «NT or is there a better option for both of them? $ inputfile1 = $ ARGV [0]; $ Inputfile2 = $ ARGV [1]; $ Outputfile1 = $ argv [2]; Open (INFILE1, $ inputfile1) || Die "input and output: $! \ N"; Open (INFILE2, $ inputfile2) || Die "input and output: $! \ N"; Open (OUTFILE_1, "> $ outputfile1") || Die "input and output: $! \ N"; $ I = 0; $ J = 0; @ Infile1 = & lt; INFILE1 & gt; @ Infile2 = & lt; INFILE2 & gt; Foreign (@ Infel 1) {@elements = split (";", $ infile1 [$ i]); $ J = 0; Foreign (@infil2) {@loopelements = split (";", $ infile2 [$ j]); If ($ element [11] ek $ looplaments [0]) {$ element [12] = $ loopimments [1]; $ Element [13] = $ loopimments [2]; $ Printon = 1; last; } $ J = $ j + 1; } @elements = join (";", @ element); Print "$ i \ r"; If ($ printen == 1) {print OUTFILE_1 "@elements"; }; $ I = $ i + 1; } Closed (INFILE1); Close (INFILE2); Close (OUTFILE_1); So can someone tell me where I am going to the top in my code?
-
The first line of the first file is read in the first iteration of the outer loop.
-
During this first visit, the lines of the second file all are read in the internal loop. -
Then the first recurrence of the outer loop ends.
-
Now, the second instance of the external loop comes up. Are there any lines of the second file left to read more?
Breaking the problem with its simplest, strength two lines with the comments given to both of them, the program loop is another file every time : Use warnings; My $ inputfile1 = shift || Die "input and output! \ N"; My $ inputfile2 = shift || Die "input and output! \ N"; Open my INFILE1, '& lt;', INFOFile1 or die "in use / did not get found: $! \ N"; Open my $ INFILE2, '& lt;', $ inputfile2 or die "in use / did not get found: $! \ N"; Tell me $ infile2_pos = $ INFILE2; # Remember to start position (& lt; $ INFILE1 & gt;) {print; Find $ INFILE2, $ infile2_pos, 0; # Find the Start Status (& lt; $ INFILE2 & gt;) {Print; }} If it is very slow , I have two things that you can do: - Read the larger file in the outer loop (I think you know why it will speed).
- Initially read the small file in an array, so you do not have to do disk I / O repeatedly on this.
What this means:
Open your $ BigFile, '& lt;', $ bigfile or die! In Use / Not Found: $! \ N "; Open your $ SMALLFILE, '& lt;', $ smallfile or die "in use / did not get found: $! \ N"; My @smallfile_array = & lt; $ SMALLFILE & gt; While (& lt; $ BIGFILE & gt;) {Print; Foreach (@smallfile_array) {Print; }}
Comments
Post a Comment