Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^2: Array looping

by vrk (Chaplain)
on Apr 12, 2017 at 15:01 UTC ( #1187775=note: print w/replies, xml ) Need Help??

in reply to Re: Array looping
in thread Array looping

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

Replies are listed 'Best First'.
Re^3: Array looping
by johngg (Canon) on Apr 12, 2017 at 16:53 UTC

    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.

Re^3: Array looping
by Anonymous Monk on Apr 12, 2017 at 22:42 UTC

    32 strokes:

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


Re^3: Array looping
by james28909 (Deacon) on Apr 13, 2017 at 11:55 UTC
    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://1187775]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-02-02 15:21 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (19 votes). Check out past polls.