Groups | Search | Server Info | Login | Register
Groups > comp.lang.perl.misc > #4781
| From | Rainer Weikusat <rweikusat@mssgmbh.com> |
|---|---|
| Newsgroups | comp.lang.perl.misc |
| Subject | Re: Increment with '#' and decrement with 'b' |
| Date | 2012-03-17 17:05 +0000 |
| Message-ID | <87ty1nnonn.fsf@sapphire.mobileactivedefense.com> (permalink) |
| References | <Au6dnWYprLy6Uv7SnZ2dnUVZ8g-dnZ2d@giganews.com> |
Henry Law <news@lawshouse.org> writes:
[...]
> I'm turning a standard musical notation for a note into a pitch. The
> note "A" will be zero, and the other notes on the piano, black and
> white, will range from 1 (for B flat/A sharp) up to 11 (for A flat/G
> sharp). A "b" suffix reduces the pitch by 1; "bb" by two. Going the
> other way, a "#" suffix increases by 1 and "##" by two.
> So D is pitch 5, Dbb is 3. Equally D## would be 7.
>
> So I need to match the following strings to the pitches shown
>
> 0 A, Bbb, G##
> 1 A#, Bb
> 2 A##, B, Cb ... and so on
>
> I can write code lots of ways to do this, but it has no style: it
> looks like over-cooked potato dumplings, and there's little point in
> my posting it here.
[...]
> I'm tempted to erect an enormous hash with every combination
> hard-coded in it: %pitches = (A=>0, Bbb=>0, "G\#\#"=>0, "A\#"=>1,
> Bb=>1, ... etc) and then extract the answer in one go with the hash
> lookup.
And that's what you should do: You'll need the hash lookup anyway to
turn the 'base letters' into numbers and since all mappings of some
string to a number are static, they can be precomputed once and used
without additional computation afterwards, instead of being
recalculated each time the translation needs to be made. If you think
that's too much code to write by hand, use the computer to help you:
---------------------
my %base = qw(A 0 B 2 H 2 C 3 D 5 E 7 F 8 G 10);
my $cur;
print("%pitches = qw(\n");
for (keys(%base)) {
$cur = $base{$_};
printf("\t%sbb %d\n\t%sb %d\n\t%s %d\n\t%s# %d\n\t%s## %d\n",
$_, $cur - 2,
$_, $cur - 1,
$_, $cur,
$_, $cur + 1,
$_, $cur + 2);
}
print(");\n");
---------------------
NB: If the Ab, Abb and G## bother you, it is easier to delete them
from the generated code than to special-case them in the generation
code.
Back to comp.lang.perl.misc | Previous | Next — Previous in thread | Find similar
Increment with '#' and decrement with 'b' Henry Law <news@lawshouse.org> - 2012-03-16 23:39 +0000
Re: Increment with '#' and decrement with 'b' Ben Morrow <ben@morrow.me.uk> - 2012-03-17 00:54 +0000
Re: Increment with '#' and decrement with 'b' Henry Law <news@lawshouse.org> - 2012-03-17 12:01 +0000
Re: Increment with '#' and decrement with 'b' Ivan Shmakov <oneingray@gmail.com> - 2012-03-18 22:30 +0700
Re: Increment with '#' and decrement with 'b' Henry Law <news@lawshouse.org> - 2012-03-18 16:55 +0000
Re: Increment with '#' and decrement with 'b' Ben Morrow <ben@morrow.me.uk> - 2012-03-20 15:12 +0000
Re: Increment with '#' and decrement with 'b' Rainer Weikusat <rweikusat@mssgmbh.com> - 2012-03-17 17:05 +0000
csiph-web