Concatenating Fields in MySql

April 2nd, 2012

I’m working on an iPhone/iPad product that needs to select a word based on the phonemes in it. I have about 40 phonemes that are stored in a MySql table as booleans. I didn’t feel like adding 40 attributes to my datamodel and then I’d have to write 40 conditionals to figure out whether a word contained the phoneme. Fortunately, MySql has a CONCAT function String Operator that I could use to concatenate all the fields into one. Now I can use REGEX expressions to query one field to determine if it is on or off for a specific word.

My first attempt to understand the operator was to just copy the example.


UPDATE `ArticIV_Phonemes` SET `Concatenated_phonemes` = CONCAT(`R`,`VocR`)

This isn’t what I wanted since it treats the stuff between the `s as literals. The result is RVocR. Not the values of the fields. Removing the `s works.


UPDATE `ArticIV_Phonemes` SET `Concatenated_phonemes` = CONCAT(R,VocR)

Now I get, 01, 10, 00, etc. which is what I want.

One minor complication. One of the phonemes is \or\ so naturally I used or as it’s name. My treats this name as a conditional and the CONCAT operation fails. I also used a hyphen in some of the names. My doesn’t like that either. It probably thinks it’s an arithmetic operator.

This is what I ended up using.


UPDATE `ArticIV_Phonemes` SET `Concatenated_phonemes` = CONCAT(R_Words,Vocalic_R_Words,S_Words,Z_Words,L_Words,Voiced_Th_Words,Voiceless_Th_Words,
Initial_R, Initial_R_Clusters, Medial_R, Medial_R_Clusters, 
VocR_aer, VocR_ar, VocR_eer, VocR_er, VocR_ier, VocR_or, VocR_our, VocR_yer, 
Initial_S, Initial_S_Clusters, Medial_S, Medial_S_Clusters, Final_S, Final_S_Clusters, 
Initial_Z, Initial_Z_Clusters, Medial_Z, Medial_Z_Clusters, Final_Z, Final_Z_Clusters, 
Initial_L, Initial_L_Clusters, Medial_L, Medial_L_Clusters, Final_L, 
Final_L_Clusters, Pre_vocalic_L, Post_vocalic_L, Pre_and_Post_V_L, 
Initial_Th, Medial_Th, 
Initial_Voiceless_Th, Initial_Voiceless_Th_Clusters,
Medial_Voiceless_Th, Final_Voiceless_Th, Final_Voiceless_Th_Clusters)

And here are the first few lines.


00001000000000000000000000000000010000010000000
00000000100000000000000000000000000000000000000
00000000000000000100000100000000000000000000000

When I want to find all of the words that have say, VocR_ier, in them I use this REGEX


SELECT *  FROM `ArticIV_Phonemes` WHERE `Concatenated_phonemes` REGEXP '[01]{9}1[01]{34}'

Basically this says, look for 9 occurrences of either a 0 or a 1. Then a 1. Then the rest of the string can be either 0 or 1. Note: you must put the final part in. The first part of the REGEX looks for a string of 9 0′s or 1′s followed by a 1. It doesn’t care where they are in the string. They could be the last 10 digits or the first.

One way to get around that is to tell the REGEX to start at the beginning of the string and look for the pattern.
Note the ^ in this example.


SELECT *  FROM `ArticIV_Phonemes` WHERE `Concatenated_phonemes` REGEXP '^[01]{9}1'

Both examples give the same result,


00000000110000000000000000000000000000000000000
00000000110000000000000000000000100000000000000
00000000100000000000000000000000010000010000000

Here’s what I used in a different database.
UPDATE `OldArtic_Categories` SET `Concatenated_Category` = CONCAT(A, B, M, D, C, E, F, G, H, I, K, L, O, P, Q, R, S, T, U, V, W, Y, Z, N)

Keith S Anvick is a moron!

March 26th, 2012

In a recent letter to the editor of AOPA he rightly criticized the imposition of user fees on general aviation. However, he then went on to claim that “It is unfortunate that this country is crippled by a president who clearly has little understanding of the free enterprise system and values big government greater than the fundamental economic values…”

Had he said this three years ago, you might be able to make a case that Bush/Cheney had no respect for free enterprise but President Obama has a clear understanding of the economy and how it works best. Remember, this is the president who managed to bail out Detroit—and make a profit from the bailout. This is the president who rescued Wall Street. He’s also the president who passed a comprehensive overhaul of the health cares system that leaves the choice of provider in the hands of the consumers. This is the president who led us out of a depression caused by a president who supposedly embraced the free-enterprise system. All this despite opposition from Republicans in Congress.

I don’t understand how these wingnuts can say these things with a straight face. To quote Paul Volker, former Fed Chairman, “This business that he’s a great socialist and out to undermine the free enterprise system and so forth, I just think it has no connection with reality.”

Obama Socialist

Xcode Nav Buttons

February 20th, 2012

You can change the title of the back button and put a rightBarButton on the screen using the following code.

In the – (void)viewDidLoad method, just after [super viewDidLoad]; put this code.


UIBarButtonItem *backButton =
    UIBarButtonItem *backButton =
    [[UIBarButtonItem alloc] initWithTitle: @"Main"
                                     style: UIBarButtonItemStyleBordered
                                    target: nil
                                    action: nil];
    self.navigationItem.backBarButtonItem = backButton;


    UIBarButtonItem *helpButton =
    [[UIBarButtonItem alloc] initWithTitle: @"Help"
                                     style: UIBarButtonItemStylePlain
                                    target: self
                                    action: @selector(goToHelp)];
    self.navigationItem.rightBarButtonItem = helpButton;
    self.navigationItem.rightBarButtonItem.enabled = YES;

If you don’t want a back button use this


[self.navigationItem setHidesBackButton:YES];

And these people vote!

February 19th, 2012

I had this forwarded to me by someone who should know better.

“Very Interesting Bit Of Detective Work”
1. Back in 1961 people of color were called ‘Negroes.’ So how can the Obama ‘birth certificate’ state he is ‘African-American’ when the term wasn’t even used at that time?

2. The birth certificate that the White House released lists Obama’s birth as August 4, 1961. It also lists Barack Hussein Obama as his father. No big deal, right? At the time of Obama’s birth, it also shows that his father is aged 25 years old, and that Obama’s father was born in ” Kenya , East Africa “. This wouldn’t seem like anything of concern, except the fact that Kenya did not even exist until 1963, two whole years after Obama’s birth, and 27 years after his father’s birth. How could Obama’s father have been born in a country that did not yet exist? Up and until Kenya was formed in 1963, it was known as the ” British East Africa Protectorate”.

3. On the birth certificate released by the White House, the listed place of birth is “Kapi’olani Maternity & Gynecological Hospital “. This cannot be, because the hospital(s) in question in 1961 were called “KauiKeolani Children’s Hospital” and “Kapi’olani Maternity Home”, respectively. The name did not change to Kapi’olani Maternity & GynecologicalHospital until 1978, when these two hospitals merged. How can this particular name of the hospital be on a birth certificate dated 1961 if this name had not yet been applied to it until 1978?

Is this on the level?

My response:

I think you need to stop watching Faux News so much. You’ve been trained to believe anything you hear even when its clearly absurd. Attached is the birth certificate. (Anyone can find it at Whitehouse.gov.) Nowhere on it does it indicate that President Obama is African-America. It states that his Father is African and his Mother is Caucasian. The very first point is an easily-debunked lie. So no. The email is not on the level. It is aimed at people too gullible to look at the facts themselves.

birth-certificate-long-form

Also, according to Wikipedia, “The British Empire established the East Africa Protectorate ((also known as British East Africa) in 1895, known from 1920 as the Kenya Colony. ”
So he was born in Keyna, East Africa—which is what is says on the certificate. Now technically, it probably should have said “Kenya Colony”, but how many clerks in Hawaii would know the difference between the two? Would you?

Of course this level if idiocy can’t be debunked. They just pile on more nonsense.

When I went into the addresses (below the article) that were given from which the article was taken, it sounds pretty legit to me. Especially when the hospital gives its history. Also the history of Kenya is an eye-opener. Then there is the time, in a one of his speeches, Obama himself, gave a different birthdate than what was on the b.c. He apparently slipped on this lie. Incidentally, I have seen a tape where he is speaking to a Muslim crowd and states that his dad was a Muslim and he said something to the effect “and I am one, too”. What is interesting, if you go into the Shari-law (sp), believe it is the 7th command which states that a person cannot lie to his parents, but it is ok to lie if it benefits Allah. To me, that is pretty scarry. Also is frighting to me is the czars he has appointed which have more say than Congress.

However, I am afraid he will be reelected because history is fast fulfilling Biblical prophecy.

How do you even begin to address this kind of lunacy?

Does Objective-C have anything like PHP’s include_once?

February 15th, 2012

The short answer is “That’s not how you do things in iOS”.

You could mimic the code replacement functionality of PHP includes by using #defines. If you wanted to you could write code just like you do in PHP and pull repetitive portions into #defines. Put them into {ProjectName}-Prefix.pch and they will be automatically available to all of your classes.

What you want to do is restructure your code so that it uses the Model-View-Controller pattern. Pull model-related code out of the main view and put it into a series of classes. Same with view-related code. Sometimes that’s all you need to do and sometimes it gets more complicated.

For example, in my quiz titles I have a preference pane where users can choose which category or categories they want to be quizzed on and the degree of difficulty. When the game first initializes I need to set the default preferences in NSUserDefaults. I created a class for the code and invoke it using

PreferencesInitialization *prefs =[[PreferencesInitialization alloc] init];
[prefs initializePreferences];

The code snippet above acts just like a PHP include and runs the code when it is called.

That works fine if the code in question has no interaction with the current view or data. In the quiz game I put scoring buttons at the bottom of the screen in a toolbar. I pulled all of the code out of the view and put it into a class. I call the class like I did above. Except that the class knows nothing about the environment it is running in. So I need to pass _everything_ it needs to know about to it.

self.scoringToolbar = [[ScoringToolbar alloc] initWithScoreKeeper:self.scoreKeeper parentView:self.view];

This is where it gets a bit tricky, it needs to know about the **current instance** of the scoreKeeper (the class that tallies the results) and the current view. In order to place the toolbar at the bottom of the screen scoringToolbar uses the self.view information to calculate positions. All of the self.view calculations are changed to self.parentView.
e.g.

CGSize windowSize = self.parentView.frame.size;

The scoringToolbar is completely self-contained so once it has its initial parameters it runs on its own. The view controller can get information from it by invoking its methods.

The third type of interaction is where the view controller tells the object to do something, say play a sound, and the target tells the view controller when it is done. The way they do that is with the use of delegates. I pulled the recording and playback portion of the game out of the main view and put it into a class. Just like the scoringToolbar it needs to know about the current view.

self.recordPlay = [[RecordPlay alloc] initWithParentView:self.view ];
[self.recordPlay setDelegate:self];

Tell the recordPlay class to play a sound with a playSound method.

[self.recordPlay playWord:targetSnd];

By setting up the view controller as a delegate of recordPlay it can be notified when the sound is finished playing and respond accordingly.

In recordPlay call

[self.delegate soundFinishedPlaying:self.soundToPlay];

This calls the soundFinishedPlaying method in the view controller and it does whatever is appropriate. Like telling the picture class to highlight a picture or the recordPlay class to play the next sound.

Delegates turn out to be key to the Model-View-Controller pattern. In my game, the controller asks the wordList class for a word using a method in wordList. If there aren’t any words, the wordList delegate lets the controller know and the controller launches the preferencesPane. When an picture is clicked on, the pictureView class tells the controller and the controller decides which sound to play. Then it tells the recordPlay class which sound to play.

Classes don’t communicate with each other—in fact they don’t even know about each other. The controller is an intermediary and it tells everyone else what to do.

I suppose this is obvious to someone with an object-oriented background, but it took me a year to figure out. I’ve rewritten my game to use these concepts and its much easier to manage. By swapping out classes I can reuse the basic structure in several similar games.

Hope his helps someone else.

Do not donate to Susan G. Komen for the Cure

February 1st, 2012

The pink ribbon people have annoyed the hell out of me with their incessant begging. But that’s just an annoyance that we have to live with if we want groceries. I would never give them money, just based on them being annoying. But now they’ve gone beyond annoying to morally bankrupt.

Many people have pointed out the recent anti-woman decisions by The leaders of Komen. Previous questionable actions, like suing anyone who uses ‘for the cure’, distorting the incidence of breast cancer, and even the name itself—which implies that there is a cure for breast cancer—could be attributed to over-zealous marketing. But defunding Planned Parenthood’s breast cancer screening programs is just wrong. And it appears that they’re doing it to pander to religious nutjobs which makes it even worse. Lots of people have details, here’s one. If you can afford it, Planned Parenthood could use the money.

And if that wasn’t bad enough, they’ve apparently stopped funding research using stem cells.

Evil Little Thing

January 20th, 2012

Jessica Alhquist has taken a lot of flack from neighbors and classmates (and even elected officials) because she asked her school to take down a prayer banner. She’s blogged about the experience and for the most part the comments are supportive. That’s not the case in the comments on the local news sites. Most of the commenters who attack her don’t understand the First Amendment. Many confuse the Constitution with the Declaration of Independence. And many of the commenters are just downright nasty. Sabine however, gets it.

Jessica stood up for everyone by doing what she did. Including christians.
As you presumable know, christians come in many variations, some are hateful (Westboro Baptists), some are oppressive (mormons), some are violent (abortion clinic bombers, the norwegian terrorist), some are whacky (Harry Camping), many are immoral (catholic priests, mega church leaders), most are hypocrites (personal observation), many are bullies (just read the threats directed by CHRISTIANS at Jessica), many are uneducated (read the posts here), and some are christian in name only.
The Separation of Church and State protects us ALL from ALL of them.

And what’s ironic about this is that this all takes place in Rhode Island—the state that was founded by Roger Williams after he was expelled from the Plymouth colony, by the government-run General Court of Massachusetts, for “diverse, new, and dangerous opinions”. Many of the comments in the papers and from the people protesting at the school board meeting are along the lines of “We’ll she doesn’t have to look at it.” or “If she doesn’t like it she can move.” What they don’t seem to understand is that the First Amendment was put in place by the founders because they knew firsthand the dangers of allowing the government to impose religion on the citizens. And this isn’t a gray area. The banner is clearly in violation of the First Amendment, which applies to the states because of the 14th Amendment.

More MySQL goodness

January 11th, 2012

It can be tricky to update records in a MySQL database using regular expressions because the stock install doesn’t support replacement strings. Occasionally, you get lucky and your data can be manipulated with a combination of REGEXP and strings. This is one case.

My phonemes field is constructed of phonemes separated by spaces. e.g.


b ɛ,i n
bl æ,ə n tʃ
br æ,ə n t æ,ə

I want to update the Initial and Final sounds fields with the sounds IF they are consonants but not if they are vowels. My REGEXP selects all of the consonants—including sh (ʃ) and th (θ or ð) at the beginning ‘^’ or end ‘$’ of the phoneme. Then I split the phonemes field by the first or last space and place that value into the Initial or Final sound field. Along the way I convert it to lower case. Also note that I want initial the initial w to count as a consonant, but not if it occurs at the end—but my phoneme rules should have caught most of those.


UPDATE `words_for_slps` 
SET `I` = LOWER(SUBSTRING_INDEX(phonemes,' ', 1)) 
WHERE `phonemes` REGEXP '^[ʃθðbcdfghjklmnpqrstvwxz]+'


UPDATE `words_for_slps` 
SET `F` = LOWER(SUBSTRING_INDEX(phonemes,' ', -1)) 
WHERE `phonemes` REGEXP '[ŋʃθðbcdfghjklmnpqrstvxz]$'

REGEX and MySQL

December 23rd, 2011

I’m working on my words database using phpMyAdmin and thought others might benefit from some of the search expressions.

Find words that start with ‘Wr’ or ‘wr’ and the CVC category is not null.


SELECT *  FROM `words` WHERE `word` REGEXP '^[Ww]r[a-z]*' 
AND `CVC` IS NOT NULL

This gives me words like, Rhoda, Rhone, rhyme because I’m asking to start at the beginning of the word ‘^’ then look for either a capital or lowercase w ‘[Ww] followed by an ‘r’ and then any letter ‘[a-z]‘ that occurs zero or more times ‘*’. I’m further restricting the search to words that have an entry in the CVC category. In phpMyAdmin you can paste this into the SQL editing field or just use the REGEX in the word field and set it to REGEXP.

Here’s one looking for all the words that end in the letters mb and the CVC category is not null.


SELECT *  FROM `words` WHERE `word` REGEXP 'mb$' 
AND `CVC` IS NOT NULL

The ‘$’ means the end of the word.

This search yields words like climb, plumb, thumb.

This search looks for words like gnome and gnat. If you want you can leave out the [a-z]* and you’ll get the same result.


SELECT *  FROM `words_for_slps` WHERE `word` REGEXP '^[Gg]n[a-z]*' 
AND `CVC` IS NOT NULL

This one’s a little more complicated, I’m looking for words like tongue and dengue but don’t know if there are words with gue in the middle—like tongues.


SELECT *  FROM `words_for_slps` WHERE `word` REGEXP '^[a-zA-z]*ngue[a-z]*' 
AND `CVC` IS NOT NULL

You can’t do OR queries in phpMyAdmin interface everything is an AND. But if you do a query, then edit the SQL it is easy to make the query into an OR.

This is the original query to find words where the grade and level are NULL.


SELECT *  FROM `words_for_slps` WHERE `level` IS NULL AND `grade` IS NULL 
AND `CVC` IS NOT NULL

And here is the same code where all I changed is the AND to OR and just to be sure I’m getting the right results, I grouped the OR with parentheses. (I can never the default rules for the different languages so I always use parentheses so I’m sure I get what I want.)


SELECT *  FROM `words_for_slps` WHERE (`level` IS NULL OR `grade` IS NULL) 
AND `CVC` IS NOT NULL

I like this result but want to look deal with the shorter words first and then work my way up to the harder ones. I also want to skip the words that do not follow the rules (DNFR). Use <> for not equal.


SELECT *  FROM `words_for_slps` WHERE (`level` IS NULL OR `grade` IS NULL) 
AND `CVC` IS NOT NULL AND `CVC` <> 'DNFR' ORDER BY LENGTH(word)

Here’s the code I use to find all of the conosonants


([bcdfghjklmnpqrstvwxz]|ch|sh|zh|th|wh|ng|qu|wh|tch|ph)

And this finds all the vowels.


(a|e|i|o|u|aw|ow|ie|ae|oe|oy|oo|uu|y)

And this finds all of the CVCCV words. Note the {2} that finds exactly two occurrences of the previous search string.


SELECT *  FROM `words_for_slps` WHERE `word`
^([bcdfghjklmnpqrstvwxz]|ch|sh|zh|th|wh|ng|qu|wh|tch|ph)
(a|e|i|o|u|aw|ow|ie|ae|oe|oy|oo|uu|y)
([bcdfghjklmnpqrstvwxz]|ch|sh|zh|th|wh|ng|qu|wh|tch|ph){2}
(a|e|i|o|u|aw|ow|ie|ae|oe|oy|oo|uu|y)$

Roxio Toast – Crashes

October 4th, 2011

We’re redoing all of our software so that it works on OSX Lion so that means lots of Toasting. A couple of times I went home without quitting Toast and the next morning it was frozen. Force quitting got out of the app but it wouldn’t launch. I tried removing the preferences files. Still no luck. On two occasions, I reinstalled the app and it would still not open. The only solution I have found is very un-Mac like—reboot the computer. It’s worked the last two times so I think that’s the solution to freezes.

Learn to Fly

Cherokee
Gas prices are way down so now is the time to get up in the air. This economical trainer is so easy to fly you'll be out on your own in no time!

Buy from Powell's.
Buy from Barnes & Noble.

Buy from Powell's.
Buy from Barnes & Noble.

Buy from Powell's.
Buy from Barnes & Noble.