I have pairs of same-lengthed strings which I am counting the orientation of the paired characters via bitwise operators. (Based on a great suggestion way back in time from BrowserUk: Re: Speeding permutation counting).

The previous solution works great in my application, but I'd now like to make the solution more flexible. I'm tring to do this with eval for a tr expression, and setting counted terms from variable. However, I'm doing something wrong, as I'm not able to return the value from the eval. Here is a sample of my code.

my @num = ( "0000", "0101", "1010", "1111" ); my ( $n1, $n2 ) = ( "0", "1" ); my $ndiff = abs( $n2 - $n1 ); for ( my $i = 0 ; $i < @num ; $i++ ) { for ( my $j = $i + 1 ; $j < @num ; $j++ ) { # Count oriented matches and mismatches along string print join( "\t", $num[$i], $num[$j], ( ( $num[$i] | $num[$j] ) =~ tr[0][0] ), #count 00 ( ( ~$num[$i] & $num[$j] ) =~ tr[\1][\1] ), #count 01 ( ( $num[$i] & ~$num[$j] ) =~ tr[\1][\1] ), #count 10 ( ( $num[$i] & $num[$j] ) =~ tr[1][1] ) #count 11 ), "\n"; # Attempt at flexible version via eval my ( $string1, $string2 ) = ( $num[$i], $num[$j] ); print join( "\t", $string1, $string2, ( eval "( $string1 | $string2 ) =~ tr[$n1][$n1]" ), ( eval "( ~$string1 & $string2 ) =~ tr[\$ndiff][\$ndiff]" +), ( eval "( $string1 & ~$string2 ) =~ tr[\$ndiff][\$ndiff]" +), ( eval "( $string1 & $string2 ) =~ tr[$n2][$n2]" ) ), "\n\n"; } } __END__ # Output from example # In each pair of lines, the first is the desired. 0000 0101 2 2 0 0 0000 0101 0 0 0 0 0000 1010 2 2 0 0 0000 1010 2 0 0 0 0000 1111 0 4 0 0 0000 1111 0 0 0 0 0101 1010 0 2 2 0 0101 1010 1 0 0 0 0101 1111 0 2 0 2 0101 1111 0 0 0 0 1010 1111 0 2 0 2 1010 1111 1 0 0 0
I'm fairly sure that there may be a trivial solution to this, but I'm not versed enough with eval to figure out how to get the flexible version working. Any help in improving this is greatly appreciated.

-albert


In reply to Returning transliteration from eval by albert

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.