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.
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.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
-albert
In reply to Returning transliteration from eval by albert
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |