Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: decoding pattern expression

by kennethk (Abbot)
on Aug 22, 2014 at 14:25 UTC ( [id://1098325]=note: print w/replies, xml ) Need Help??


in reply to decoding pattern expression

Is there any way I can do it with bash scripting ?
If you want bash scripting, you've come to the wrong site. If you'd like some help doing this with Perl scripting, I'd probably use indices 0-4 instead of A-E, make an array (my @values = (1, 7, 11, 3, 100)) and then split your coded strings:
my @terms = split //, $input; my $sum = 0; for my $i (0 .. @terms-1) { $sum += $values[$i] if $terms[$i] ne '?'; } print "$input = $sum\n";

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: decoding pattern expression
by AnomalousMonk (Archbishop) on Aug 22, 2014 at 15:19 UTC

    Using substr might be slightly faster than split-ing to an array (YMMV):

    c:\@Work\Perl>perl -wMstrict -le "my @vectors = ( [ '--+-+', 122 ], [ '---++', 122 ], [ '?????', 0 ], [ '-????', 1 ], [ '-???-', 101 ], ); ;; my @map = (1, 7, 11, 3, 100); ;; use Test::More 'no_plan'; ;; for my $ar_vector (@vectors) { my ($s, $expected) = @$ar_vector; ;; my $cvt = convert($s); ok $cvt == $expected, qq{'$s' -> $expected (got $cvt)}; } ;; sub convert { my ($s) = @_; ;; my $sum = 0; $sum += substr($s, $_, 1) ne '?' ? $map[$_] : 0 for 0 .. length($s) +-1; return $sum; } " ok 1 - '--+-+' -> 122 (got 122) ok 2 - '---++' -> 122 (got 122) ok 3 - '?????' -> 0 (got 0) ok 4 - '-????' -> 1 (got 1) ok 5 - '-???-' -> 101 (got 101) 1..5

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-23 07:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found