Note that there are some explanatory texts on larger screens.

plurals
  1. POComparison of two array elements and calculation
    primarykey
    data
    text
    <p>I have an issue with a section of code I wish to write. My problem is based around two arrays and the elements they encompass.</p> <p>I have two arrays filled with numbers (relating to positions in a string). I wish to select the substrings between the positions. The elements in the first array are the start of the substrings and the elements in the second array are the ends of the substrings. </p> <p>The code I have supplied reads in the file and makes it a string:</p> <pre><code>&gt;demo_data theoemijono milotedjonoted dademimamted </code></pre> <p>String:</p> <pre><code>theoemijonomilotedjonoteddademimamted </code></pre> <p>so what I want to happen is to extract the substring </p> <pre><code>emijonomiloted emimamted </code></pre> <p>The code I have written takes the the first element array and compares it with the second array corresponding element and then to ensure that there is no cross over and hence hold the substring to start with emi and end with tedas seen in the provided sequences</p> <pre><code>for($i=0; $i&lt;=10; $i++) { if ($rs1_array[$i] &lt; $rs2_array[$i] &amp;&amp; $rs1_array[$i+1] &gt; $rs2_array[$i]) { my$size= $rs2_array[$i]-$rs1_array[$i]+ 3); my$substr= substr($seq, $rs1_array[$i],$size); print $substr."\n"; } } </code></pre> <p>Using this code works for the first substring, but the second substring is ignored as the first array has fewer elements and hence the comparison cannot be completed.</p> <p><em><strong>UPDATE</em></strong></p> <p>Array structures:</p> <pre><code>@rs1_array = (4, 28); @rs2_array = (15, 22, 34); </code></pre> <p>Hi borodin, You were absolutely correct.. I have edited the code now! Thank you for seeing that in relation to the length issue. The reason for the strange offset is that the value in @rs2_array is the start position and it does not take into consideration the remainder of the word "ted" in this case and I require this to complete the string.The Array is built correctly as for the elements in @rs1_array they represent the start position "emi" the @rs2_array elements also hold the start position for each "ted" so as there are 2 emi's and 3 ted's in the string this causes the unbalance.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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