# Parse It! Parse It!

by Joe Burns

Parse the information from the array and give it to the showChord() function.

Script Tip...

Okay! It's the final day of the Guitar Chord Script. The only thing left to do is parse the information from the array and give it to the showChord() function we hit upon last week.

For one last time, here's the script and its effect:

Click a chord on the right and watch the dots provide the pattern.

 A A7 A9 A13 Am Am6 Am7 Bb B B7 B9 Bm Bm6 Bm7 C C6 C7 Cmaj7 C9 Csus4 C7sus4 Cdim7 Cm Cm7 D D6 D7 Dmaj7 D9 Dsus4 D7sus4 Ddim7 Dm Dm7 Eb9 E E7 E9 E13 Em Em6 Em7 F F5 F6 Fmaj7 Fm Fm7 F# F#7 F#9 F#m F#m7 G G5 G6 G7 Gmaj7 G9 G6/9 Gsus4 G7sus4 Gdim7 Gm Gm7

Here's the Code

Remember this from the last Script Tip?

 ``` Frets = parser (chords[Text]) ```
If you recall, that line was used to grab a specific line from the array of chord names and radio button numbers. The "parser([Text])" line meant we were transferring that information to a function to be acted upon. That function, and following code, looks like this:
 ``` function parser (InString)
{
var Sep = ";", NumSeps=1, Count, Start, ParseMark, parse;
for (Count=1; Count < InString.length; Count++)
{
if (InString.charAt(Count)==Sep)
NumSeps++;
}
parse = new Array ();
var Start=0, Count=1, ParseMark=0, LoopCtrl=1;
while (LoopCtrl==1)
{
ParseMark = InString.indexOf(Sep, ParseMark);
TestMark=ParseMark;
if ((TestMark==0) || (TestMark==-1))
{
parse[Count]= InString.substring (Start, InString.length);
LoopCtrl=0;
break;
}
parse[Count] = InString.substring (Start, ParseMark);
Start=ParseMark+1, ParseMark=Start, Count++;
}
parse=Count;
return (parse);
} ```
Before we get into it, let's stop and think about what this mess of code is supposed to do. First, let's once again look at our familiar friend the A13 chord array entry:

 ``` chords["A13"]="1;3;20;22;23" ```
The numbers within the double quotes have been turned into a straight text string (remember that from the last Tip?) See how there are five numbers following each, separated by a semicolon? That's important. Now, look again at the script, underneath the ShowChord() function. This is the last line:

 ``` document.guitar[parseInt(Frets[Count])].checked=true; ```
This is the line of code I basically skipped over in the last Tip. This does the magic of setting the correct radio buttons to "checked." This code, because it's inside the square brackets, will be replaced with the number it represents:
 ``` [parseInt(Frets[Count])] ```
The command "parseInt" is designed to return an integer, depending on what is found within its instance (the parentheses following it). "Frets[Count]" will be one of the numbers returned from the parser script looking at the text string delivered to it by chords["A13"]="1;3;20;22;23".

So, when this parser script runs through the A13 array item, it will loop through five times returning 1, then 3, then 20, then 22, then 23. With me? Then the line:

 ``` document.guitar[parseInt(Frets[Count])].checked=true; ```
...will "check" each of those radio buttons. Okay, so now that we have a general idea of what is to happen, let's see how it happens. We'll start with the top half:
 ``` function parser (InString)
{
var Sep = ";", NumSeps=1, Count, Start, ParseMark, parse;
for (Count=1; Count < InString.length; Count++)
{
if (InString.charAt(Count)==Sep)
NumSeps++;
} ```
There's the name of the function that is to act upon "InString." Remember that we passed the text of the array (A13) to this function. Once it arrives, there is a new variable name inside the instance of the function. That gives the text string a new name. The text string that was sent to the parser function is now known as "InString."

You're probably familiar with the next line. It's one that sets variables and brings others into play. Look at it. I'll refer to it again in a moment.

A For loop is brought into play. The variable "Count" is given the value of 1. As long as "Count" is less than the length of the InString text string, Count will be moved up one each time the loop occurs. That means that when Count is equal to the length of the text string, (1) the loop stops and (2) every character in the text string has been inspected, so we're done!

Remember that each number in the text string is separated by a semicolon and the variable Sep is set to represent a semicolon. So, the next If statement asks if the next character in the text string is a semicolon (the command "charAt" returns the next character in a text string).

If the next character is a semicolon, then "NumSeps" should increase. Basically, if the character is a semicolon, we can't use it, so it should go on to the next character. That way, only the numbers in the text string are evaluated. The semicolons are skipped over. Very clever.

Now this monster:

 ``` parse = new Array ();
var Start=0, Count=1, ParseMark=0, LoopCtrl=1;
while (LoopCtrl==1)
{
ParseMark = InString.indexOf(Sep, ParseMark);
TestMark=ParseMark;
if ((TestMark==0) || (TestMark==-1))
{
parse[Count]= InString.substring (Start, InString.length);
LoopCtrl=0;
break;
}
parse[Count] = InString.substring (Start, ParseMark);
Start=ParseMark+1, ParseMark=Start, Count++;
}
parse=Count;
return (parse);
} ```
We're starting a "new Array" that will be built by this code out of the text string. If we stay with the A13th chord, we know the array will be 1, 3, 20, 22, 23.

The next line is common. Variables are given values to be brought into play later.

Now we have a While loop coming into play. The While loop, you might remember, runs as long as the condition within its instance is true. In this case, While LoopCtrl is equal to 1, the loop rolls... and LoopCtrl is equal to 1 because the authors set it to 1 just above.

The next line sets the variable "ParseMark" to the location of the next character. That's what the method indexOf does; it returns the location of the character the script is working on. It knows which character the script is working on because of the instance: (Sep, ParseMark).

Remember that Sep was set above. Think of it as the starting point for the full length of the string. By setting Sep one up each time, this loop reads the next value each time it rolls through. What it won't read are the semicolons because we got rid of them in the function above, remember?

Next, we exchange the variable name "TestMark" for "ParseMark." I'm not sure why other than that's the author's preference. That text mark is then tested to see if it is 0 or one less than 0 (-1). If it's one less, then we must be at the beginning, so set parse[Count] to whichever is the smaller of Start and InString.length. It's obviously Start, which is equal to 0.

Next, parse[Count] is set to the value of the character that is being looked at, "Start" is increased by 1, as is "Count," and finally "parse" is set to the next "Count" value in case the function loops through again.

The value found (parse) is returned.

The script loops through again and again, clipping off the next value between the semicolons and returning it so the correct radio button can be checked. The process happens again and again until the script runs out of numbers. No more numbers means there are no more radio buttons to highlight.

The chord is displayed.

Wow! That was a wicked script, huh? Next week we'll get into something a little less hairy.

Next Time: Setting and Getting a Cookie
{"ft_lang_label":"__label__en","ft_lang_prob":0.92926675,"math_prob":0.77498776,"size":3854,"snap":"2021-04-2021-17","text_gpt3_token_len":918,"char_repetition_ratio":0.12805195,"word_repetition_ratio":0.03529412,"special_character_ratio":0.23663726,"punctuation_ratio":0.12855376,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96418124,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-28T09:29:31Z\",\"WARC-Record-ID\":\"<urn:uuid:11d662a0-937a-41a7-9413-097ef2988e63>\",\"Content-Length\":\"61032\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:807a0bda-2d09-48a4-9017-5eff23cf1301>\",\"WARC-Concurrent-To\":\"<urn:uuid:407f396e-cbcf-42cb-8c06-b8bcfc999c51>\",\"WARC-IP-Address\":\"70.42.23.182\",\"WARC-Target-URI\":\"https://mobile.developer.com/lang/jscript/article.php/616811/Parse-It-Parse-It.htm\",\"WARC-Payload-Digest\":\"sha1:TB4XHTRD4PSJ5MECFTU7GOTJDQVZI5P4\",\"WARC-Block-Digest\":\"sha1:Z52FQF76MACQT4VZFZEPJONUFLS7MPS4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610704839214.97_warc_CC-MAIN-20210128071759-20210128101759-00650.warc.gz\"}"}