http://qs321.pair.com?node_id=239209

FoxtrotUniform has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm writing Yet Another Guitar Chord Finder(tm), and as such I want to be able to map easily between note names and their positions in the chromatic scale. I want to be able to find that A is the first note on the chromatic, and that the second note on that scale is A#. My first instinct was to use a hash, but that only gets me one direction. I can always build a second "backwards" hash:

my %position_of = ( 'A' => 1, 'A#' => 2, # ... ); my %note_at = (); for my $note (keys %position_of) { $note_at{$position_of{$note}} = $note; }

but I don't really like the idea of having two hashes that express basically the same thing. (Semantically, I know, the hashes don't, but the data are bijective, and I should be able to do it with one structure, dammit!)

Two hashes probably isn't a bad WTDI, but is there a better way?

--
F o x t r o t U n i f o r m
Found a typo in this node? /msg me
The hell with paco, vote for Erudil!

Replies are listed 'Best First'.
Re: Bijective data association structure? (1 hash)
by tye (Sage) on Feb 27, 2003 at 19:26 UTC

    Then just use one hash, since you never use the same value as both a key and a value...

    my %chordpos; @chordpos{1..12}= qw( A A# ... ); @chordpos{values %chordpos}= keys %chordpos;

                    - tye
Re: Bijective data association structure?
by jasonk (Parson) on Feb 27, 2003 at 19:20 UTC

    I'm not sure of a better data structure, but there is a better way to build that second hash:

    my %note_at = reverse %position_of;
Re: Bijective data association structure?
by jlongino (Parson) on Feb 27, 2003 at 19:35 UTC
    You'll probably just want to build your hash as a circular linked list:
    my $position_of = ( 'A' => { next_key => 'A#', prev_key => 'G#', # other stuff . . . }, 'A#' => { next_key => 'B', prev_key => 'A', # other stuff . . . }, ### in-between stuff 'G#' => { next_key => 'A', prev_key => 'G', # other stuff . . . }, );
    Update: with circular linked lists you usually keep track of head/tail:
    my $head = 'A'; my $tail = 'G#';

    --Jim

Re: Bijective data association structure?
by Ctrl-z (Friar) on Feb 28, 2003 at 00:08 UTC

    edit: on re-read it appears you are asking a different question!. But having got me thinking about it, translating the chord shapes to guitar chord shapes seems pretty hellish - Id be interested to see how you implement this, if your game for posting the code?.


    time was, I could move my arms like a bird and...