Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Array looping

by johngg (Abbot)
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.

Cheers,

JohnGG

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!

      Cheers,

      JohnGG

        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

        Brilliant!

      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.

        Alexander

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1187743]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2017-12-16 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (453 votes). Check out past polls.

    Notices?