Note that there are some explanatory texts on larger screens.

plurals
  1. POnested while loop comparing two files, outside loop stops after one iteration, need to compare every line of files
    primarykey
    data
    text
    <p>Firstly I apologise if my formatting here is incorrect, I am very new to writing scripts (3 days) and this is my first post on this site.</p> <p>I have two files which are tab separated, <code>File a</code> contains 14 columns, and <code>File b</code> contains 8 columns.</p> <p>One column in <code>File b</code> has a numeric value which correlates to a range of numbers generated by two numeric fields from <code>File a</code>. For every line in <code>File a</code>, I need to, search through the <code>File b</code> and print a combination of data from fields on both files. There will be multiple matches for each line of <code>File a</code> due to a numeric range being accepted.</p> <p>The code that I have created does exactly what I want it to do but only for the first line of <code>File a</code>, and doesn't continue the loop. I have looked all over the internet and I believe it may be something to do with the fact that both files read from standard input. I have tried to correct this problem but I can't seem to get anything to work</p> <p>My current understanding is that by changing one file to read from a different file descriptor my loop may work... with something such as <code>&gt;$3</code> but I don't really understand this very well despite my research. Or possibly using the <code>grep</code> function which I am also struggling with.</p> <p>Here is the outline of the code I am using now:</p> <pre><code>use strict; use warnings; print "which file read from?\n"; my $filea = &lt;STDIN&gt;; chomp $filea; { unless (open ( FILEA, $filea) { print "cannot open, do you want to try again? y/n?\n?"; my $attempt = &lt;STDIN&gt;; chomp $again; if ($again =~ 'n') { exit; } else { print "\n"; $filea = &lt;STDIN&gt;; chomp $filea; redo; } } } #I also open fileb the same way, but wont write it all out to save space and your time. my output = 'output.txt'; open (OUTPUT, "&gt;&gt;$output"); while (my $loop1 = &lt;FILEA&gt;) { chomp $loop1; ( my $var1, my $var2, my $var3, my $var4, my $var5, my $var6, my $var7, my $var8, my $var9, my $var10, my $var11, my $var12, my $var13, my $var14 ) = split ( "\t", $loop1); #create the range of number which needs to be matched from file b. my $length = length ($var4); my $range = ($var2 + $length); #perform the search loop through fileb while (my $loop2 = &lt;FILEB&gt;) { chomp $loop2; ( my $vala, my $valb, my $valc, my $vald, my $vale, my $valf, my $valg) = split ( "\t", $loop2 ); #there are then several functions and additions of the data, which all work basicly so I'll just use a quick example. if ($vald &gt;= $val3 $$ $vald &lt;= $range) { print OUTPUT "$val1, $vald, $val11, $valf, $vala, $val5 \n"; } } } </code></pre> <p>I hope this all makes sense, I tried to make everything as clear as possible, if anyone could help me edit the code so that the loop continues through all of filea that would be great.</p> <p>If possible please explain what you've done. Ideally I'd like it if its possible to obtain this result without changing the code too much.</p> <p>Thanks guys!!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload