heidi has asked for the wisdom of the Perl Monks concerning the following question:
tried finding the mismatch here and its position. The mismatch over here is G and T in position 23 and 37 respectively.used foreach loop and matched each element. Not succeeded :( can anyone help with simpler logic?$one= "1 AGCTGATCGAGCTAGTACCCTAGCTC 26" $two= "15 AGCTGATCGAGCTAGTACCCTATCTC 40"
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: match and mismatch
by GrandFather (Saint) on Nov 14, 2008 at 08:18 UTC | |
There is a neat trick for finding difference between similar equal length ASCII strings: xor them together and any non-zero bytes are different. Consider:
Prints:
The tr/// changes non-zero bytes to x. The while loop then uses index to search through the difference string for the x bytes and reports their index (0 based position). Perl reduces RSI - it saves typing | [reply] [d/l] [select] |
by johngg (Canon) on Nov 14, 2008 at 10:35 UTC | |
The tr/// changes non-zero bytes to x. The while loop then uses index to search through the difference string for the x bytes and reports their index (0 based position). I think it might be simpler to use a global regex match for non-nulls and pos. You can just match them for 1-based counting or put them in a look-ahead for 0-based counting. Note that I added another difference in the third characters to check that it was working as I had hoped.
The output.
I hope this is of interest. Cheers, JohnGG | [reply] [d/l] [select] |
by heidi (Sexton) on Nov 17, 2008 at 03:25 UTC | |
this is a big file, though the whole file looks like this. What i am trying to do exactly is to grep the header (sxoght) for display in columns and also to display where there is a mismatch in the alignment between query and sbjct. for this input file, the expected results should look like: the code which i have written is : I am not able to use "strict and warning" because using it doesnt allow me to access the scalar variable outside the loop. In my code, i m trying to extract the positions first, so that i will subtract it from the already stored @arr values of beginning and start. I am having problems with the for loop. I know where i am going wrong, but dont know how to correct it. PLEASEEEE HELP !!! | [reply] [d/l] [select] |
by bart (Canon) on Nov 17, 2008 at 03:57 UTC | |
The next is very close to your code, but kind of works. You only need to format the output as desired (I don't know where the large numbers come from): For the format, I propose to store the results in an array, and print "mismatch:" only if the array isn't empty at the end. After that modification, the output I get for this file is
p.s. There's a possible speed improvement if you XOR (^) the two strings, you'll get a string of null bytes for where they are the same and non null where they are not:
| [reply] [d/l] [select] |
by heidi (Sexton) on Nov 17, 2008 at 04:58 UTC | |
by Anonymous Monk on Feb 10, 2017 at 05:00 UTC | |
| [reply] |
Re: match and mismatch
by ccn (Vicar) on Nov 14, 2008 at 08:15 UTC | |
| [reply] [d/l] |
Re: match and mismatch
by artist (Parson) on Nov 14, 2008 at 09:38 UTC | |
And here is the output which shows the difference at each position if any. So on '25th' position, you have 'G' in one, and 'T' in the other.
--Artist
| [reply] [d/l] [select] |
by lima1 (Curate) on Nov 14, 2008 at 17:49 UTC | |
| [reply] |
Re: match and mismatch
by apl (Monsignor) on Nov 14, 2008 at 12:47 UTC | |
| [reply] |
by heidi (Sexton) on Nov 17, 2008 at 02:06 UTC | |
this is a big file, though the whole file looks like this. What i am trying to do exactly is to grep the header (sxoght) for display in columns and also to display where there is a mismatch in the alignment between query and sbjct. for this input file, the expected results should look like: the code which i have written is : I am not able to use "strict and warning" because using it doesnt allow me to access the scalar variable outside the loop. In my code, i m trying to extract the positions first, so that i will subtract it from the already stored @arr values of beginning and start. I am having problems with the for loop. I know where i am going wrong, but dont know how to correct it. PLEASEEEE HELP !!! | [reply] [d/l] [select] |
by apl (Monsignor) on Nov 20, 2008 at 13:23 UTC | |
So, please show the program you are trying to run, and we'll try this again. | [reply] [d/l] [select] |