Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Array looping

by johngg (Canon)
on Apr 12, 2017 at 10:12 UTC ( #1187743=note: print w/replies, xml ) Need Help??

in reply to Array looping

Rather than walking the arrays, use join and xor the joined strings, counting nuls with tr.

johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my @arr1 = qw{ A T C G T C G A G C G }; my @arr2 = qw{ A C G T C C T G T C G }; my $count = ( ( join q{}, @arr1 ) ^ ( join q{}, @arr2 ) ) =~ tr{\0}{}; say $count;' 4

I hope this is of interest.



Replies are listed 'Best First'.
Re^2: Array looping
by vrk (Chaplain) on Apr 12, 2017 at 15:01 UTC

    That's interesting. I hardly ever think of using the string xor operator. Surely your solution can be golfed, though...

    $ perl -alpE '$_=($F[0]^$F[1])=~tr{\0}{}' ATCGTCGAGCG ACGTCCTGTCG 4 ^D

      Well, since the OP's post started with arrays rather than strings your golf is cheating slightly ;-)

      However, if the problem comes from the real world I think the odds are better than even that the OP started with strings and split them into arrays in order to do the comparison. In that scenario your golf is fine, though anything other than "snippet" dna strings are unlikely to fit on the command line!



        Oh, it will work with file input too since it's reading from stdin. Just make sure the two sequences are separated by a space.

      32 strokes:

      $ perl -pale '$_^=$F[1];$_=y/\0//' ATCGTCGAGCG ACGTCCTGTCG 4


      Do you all know of a good place I can study or learn about these short/compact functions? Is it called perl golf? What is perl golf?
        Do you all know of a good place I can study or learn about these short/compact functions? Is it called perl golf? What is perl golf?

        Re^3: Golf code in perl (Context)

        And no, please don't use "golfed" code for any other purpose than to play golf. It's simply unmaintainable in that form, write-only code in its purest form. Prefer writing longer, readable code that you (or any other maintainer) will still understand in half a year, a year, or five years. If you resort to "dirty tricks" (like string xor plus counting NUL bytes), add a comment that explains how the trick works and the requirements for that trick (e.g. strings must only contain ACGT, all upper case, both the same length, etc). Don't make your future self invent a time machine to come back and severely hurt you for the mess you will have written.


        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1187743]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2022-01-18 22:10 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (54 votes). Check out past polls.