tag:blogger.com,1999:blog-65613534321152293092024-03-21T17:45:44.996-07:00Some Conditions May ApplyBill Monizhttp://www.blogger.com/profile/05565071592041415667noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-6561353432115229309.post-62360637408819818492012-05-08T05:40:00.000-07:002012-05-08T05:40:21.800-07:00CFScript: Odd Struct Initialisation Syntax<span style="font-family: Arial, Helvetica, sans-serif;">Ok, so today I was putting together an application.cfc in CFScript and I ran into an interesting problem. I was setting up my application variables, and I arrived at </span><span style="font-family: 'Courier New', Courier, monospace;">this.mappings</span><span style="font-family: Arial, Helvetica, sans-serif;"> where I needed to add a couple of custom locations. With tags it was easy enough:
<span style="font-family: Arial, Helvetica, sans-serif;"></span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;"> <cfset this.mappings["/stuff"] = "C:\assets\resources\stuff"></span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;"> <cfset this.mappings["/things"] = "C:\assets\lib\things"></span></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">And I know that in script I could have used:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">
<span style="color: blue; font-family: 'Courier New', Courier, monospace;"> this.mappings = {};</span><br />
</span><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;">this.mappings["/stuff"] = "C:\assets\resources\stuff";</span><br />
</span><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;">this.mappings["/things"] = "C:\assets\lib\things";</span></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">But I thought, wouldn't it be better to declare and initialise that struct at the same time? When I tried to look this up in the Adobe docs, I found the following syntax:</span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;">myStruct = { </span><br />
</span><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> key1 = "value1",</span><br />
</span><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> key2 = "value2",</span><br />
</span><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> key3 = "value3"</span><br />
</span><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span> <span style="color: blue; font-family: 'Courier New', Courier, monospace;">};</span><br /><span style="font-family: Arial, Helvetica, sans-serif;">The problem is, in the case of </span><span style="font-family: 'Courier New', Courier, monospace;">mappings</span><span style="font-family: Arial, Helvetica, sans-serif;">, my key names are all prefixed with a slash. So trying to write</span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span>
<span style="color: blue; font-family: 'Courier New', Courier, monospace;">this.mappings</span> <span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> = { </span></span>
<span style="color: blue; font-family: 'Courier New', Courier, monospace;">/stuff</span> <span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> = "</span></span><span style="color: blue; font-family: 'Courier New', Courier, monospace;">C:\assets\resources\stuff</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;">" };</span><br />
</span><span style="font-family: Arial, Helvetica, sans-serif;">just doesn't work, since it's invalid syntax. Based on that, I supposed it just wasn't possible to do a declare and initialise in that situation. </span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Then I had a crazy thought...waaaay too crazy to work, but I gave it a shot anyway:</span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;">this.mappings = {</span></span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> "/stuff" = "</span></span><span style="color: blue; font-family: 'Courier New', Courier, monospace;">C:\assets\resources\stuff</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;">",</span></span><br />
<span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span></span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;">"/things" = "</span></span><span style="color: blue; font-family: 'Courier New', Courier, monospace;">C:\assets\lib\things</span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;">"</span></span> <span style="color: blue; font-family: 'Courier New', Courier, monospace;"><br /></span><span style="color: blue; font-family: 'Courier New', Courier, monospace;"> </span> <span style="font-family: Arial, Helvetica, sans-serif;"><span style="color: blue; font-family: 'Courier New', Courier, monospace;">};</span></span>
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">And that WORKS! To me, the syntax seems completely bizarre because it looks like we're assigning one literal string to another literal string. But somehow, the universe doesn't implode and ColdFusion interprets it just fine. As a bonus, the case of the keys is preserved just like when we use the square-bracket notation. Good stuff!</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Does that seem really weird to anyone else?</span>Bill Monizhttp://www.blogger.com/profile/05565071592041415667noreply@blogger.com4tag:blogger.com,1999:blog-6561353432115229309.post-38199040369631158782012-04-28T22:18:00.001-07:002012-04-28T22:19:54.284-07:00Multiselect Checkboxes and ColdFusion<span style="color: #0b5394; font-family: Arial, Helvetica, sans-serif; font-size: large;">Use Checkboxes to Make Multi-Select Option Lists</span><br />
<div>
<span style="font-family: Arial, Helvetica, sans-serif;">I've been working with HTML on and off for a while, but somehow I managed to completely miss the idea of multi-select checkboxes until a few days ago. I've used </span><span style="font-family: 'Courier New', Courier, monospace;"><select></span><span style="font-family: Arial, Helvetica, sans-serif;"> lists with the </span><span style="font-family: 'Courier New', Courier, monospace;">multiple</span><span style="font-family: Arial, Helvetica, sans-serif;"> attribute, but recently I came across something like this:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><i><b>Example 1</b></i></span></div>
<iframe src="http://pastebin.com/embed_iframe.php?i=ZeW2cwpm" style="border: none; height: 175px; width: 100%;"></iframe><br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">Notice that all the checkboxes share the same </span><span style="font-family: 'Courier New', Courier, monospace;">name</span><span style="font-family: Arial, Helvetica, sans-serif;"> attribute. Now, my first though was that this was just plain wrong. I knew that in the case of radio buttons, options were grouped by giving them the same name. But I always thought of checkboxes as isolated yes/no controls. Consequently, if I wanted to add the fruit choices above to a form, I might have done something like this:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><b><i>Example 2</i></b></span><br />
<iframe src="http://pastebin.com/embed_iframe.php?i=EhpVzj6a" style="border: none; height: 175px; width: 100%;"></iframe><br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">Checkbox inputs have the interesting beahaviour that, if they're not checked, they're simply not submitted as part of the form. In terms of ColdFusion, unchecked checkboxes are not contained in the Form struct after submission. So, in <b>Example 2</b> (what I previously thought was the "right" way), let's say I check Orange, Kiwi, and Pear but leave Lime unchecked. Then I submit the form and dump the Form struct in fruitPicker.cfm. I see the following:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWqcy5ksIH3KIYoZtPLXEgvPidaKQ8WfnRvlRo581uE7lyC1hYE-r29p236KUlw2EbLM2IZNBp2bjHMSX3kSUC0tCrxTmHFvA-xzr31cjqVwkbug7L1XVOUZnHcgCLEnCFKJN3c6BOAtU/s1600/MutlipleCheckboxesOldWay.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWqcy5ksIH3KIYoZtPLXEgvPidaKQ8WfnRvlRo581uE7lyC1hYE-r29p236KUlw2EbLM2IZNBp2bjHMSX3kSUC0tCrxTmHFvA-xzr31cjqVwkbug7L1XVOUZnHcgCLEnCFKJN3c6BOAtU/s1600/MutlipleCheckboxesOldWay.PNG" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<span style="font-family: Arial, Helvetica, sans-serif;">So, my code to determine what fruits had been chosen would simply examine the fieldnames list, and extract the elements that contained the word fruit. Then I could build a list of selected fruit options. Something like this:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /><iframe src="http://pastebin.com/embed_iframe.php?i=jtzAMsqH" style="border: none; height: 195px; width: 100%;"></iframe></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">That works fine, but it turns out I've been doing extra work for nothing. If we use the form code in Example 1 and submit it with Orange, Kiwi and Pear checked, a dump of the form scope shows the following:</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtRHth2L-JsmSRLD5CKzUx8vnDTNg9KD0J4ZztpWVex5GfS-DNhowLWG4o_HWvbD71ANbZrNtJ00yBpPD2bCzpsQQuJQD94-FgAh45xFYwo84_E5cZKFFZFtXK-Wy23KZOOYJ2sUMMDQM/s1600/MutlipleCheckboxesNewWay.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtRHth2L-JsmSRLD5CKzUx8vnDTNg9KD0J4ZztpWVex5GfS-DNhowLWG4o_HWvbD71ANbZrNtJ00yBpPD2bCzpsQQuJQD94-FgAh45xFYwo84_E5cZKFFZFtXK-Wy23KZOOYJ2sUMMDQM/s1600/MutlipleCheckboxesNewWay.PNG" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<span style="font-family: Arial, Helvetica, sans-serif;">There it is. Giving all of the checkboxes the same </span><span style="font-family: 'Courier New', Courier, monospace;">name</span><span style="font-family: Arial, Helvetica, sans-serif;"> attribute causes ColdFusion to interpret the selected values as a nice pre-built list associated with that name. That saves a bit of code and I thought it was pretty cool. I wondered if it was exploiting some HTML quirk, but it turns out that it's actually in the spec. Check it out: <a href="http://www.w3.org/TR/html4/interact/forms.html#checkbox" target="_blank">http://www.w3.org/TR/html4/interact/forms.html#checkbox</a>.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">This is likely old-news to some, but if you were in the dark like me, give it a try!</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">NOTE: It's only the </span><span style="font-family: 'Courier New', Courier, monospace;">name</span><span style="font-family: Arial, Helvetica, sans-serif;"> attribute you want to make the same. Never give the same </span><span style="font-family: 'Courier New', Courier, monospace;">id</span><span style="font-family: Arial, Helvetica, sans-serif;"> to multiple elements on the page. It's bad for your DOM and people will laugh at you.</span>Bill Monizhttp://www.blogger.com/profile/05565071592041415667noreply@blogger.com2tag:blogger.com,1999:blog-6561353432115229309.post-57996829442723846952011-12-12T18:07:00.000-08:002012-04-26T05:12:15.272-07:00Using T-SQL REPLACE<span style="color: #0b5394; font-family: Arial, Helvetica, sans-serif; font-size: large;">Replacing Specific Text in Microsoft SQL (T-SQL)</span><br />
<b style="font-family: Arial, Helvetica, sans-serif;">Scenario:</b><span style="font-family: Arial, Helvetica, sans-serif;"> Within your database you have a table, say NewsStories for example. This table has a varchar column containing significant amounts of text per row, say ArticleBody. You notice that in every article written this month, a particularly reporter's name has been spelled wrong. The articles are live, and this needs to be fixed... fast.</span><br />
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;">So, you need to replace a particular bit of text (the name), in a particular subset of data (the articles from this month) without disturbing anything else. Luckily, the Replace function makes it easy!</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;">T-SQL Replace takes 3 arguments. I like to think of them as the 3 Ws - <b>Where, What, With</b>. That is:</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;">Replace( <span class="Apple-style-span" style="color: red;">Where</span>, <span class="Apple-style-span" style="color: orange;">What</span>, <span class="Apple-style-span" style="color: lime;">With</span> )</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><span class="Apple-style-span" style="color: red;">Where</span>: the full string we're going to be working on. It could be a literal ('some text') or a column name (ArticleBody), or any other valid expression that produces a char string.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><span class="Apple-style-span" style="color: orange;">What</span>: the sub-string we're looking to replace. Again this can be any valid character expression, but it can't be an empty string. </span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><span class="Apple-style-span" style="color: lime;">With</span>: The string we want to put there instead. </span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;">The Replace function returns a new string with the replacement completed.</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;">So, how do tackle the replacement within a particular column. Using our example scenario above:</span><br />
<br />
<iframe src="http://pastebin.com/embed_iframe.php?i=bfmsEyqV" style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; width: 100%;"></iframe></div>
<div>
<br />
<span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;">That's it, all done! Remember of course, like any update, this is a destructive change so be damn sure you triple-check what you're replacing. It's a simple function but extremely useful on many occasions. </span></div>Bill Monizhttp://www.blogger.com/profile/05565071592041415667noreply@blogger.com5