Title photo
frugal technology, simple living and guerrilla large-appliance repair
Sun, 13 Sep 2015

I build an app, Part 1: Hashes (aka key-value pairs) are easy in JavaScript (and even in Java) ... plus my coding bio

I like to learn by doing. I'm reading and typing in code and futzing around with it. But I had an idea, and I'm betting I can learn what I need to make it happen.

The idea is a "What is this acronym?" app, where there's a web page, the user types in an acronym (or partial acronym) and gets in return a list of possible full names for that acronym.

Nothing too crazy, and I'm going to keep it as simple as I can.

I want to do it as a single-page JavaScript app. Call it "just a web page with some interactive JavaScripty elements." Is that an "app"? (Don't know, don't care).

I'm choosing JavaScript for this project because I want to keep it simple. And I want to learn JavaScript.

There shouldn't be an overwhelming amount of data. At the beginning anyway. I don't want to deal with frameworks. Or server-side anything. I want all the processing to take place in the browser.

Hence, JavaScript.

And because I'm a beginner. A Code Newbie, as in my new favorite podcast/blog/forum (Thanks to host Saron (do I call her "Chief Newbie"?) and all the great guests on what is my new go-to podcast. And yes, I will start an account on the Discourse forum. Soon.).

Back to my app (or "dynamic Web page," if you will).

The data setup is simple: Acronym = Full Name

From my (very little) study of Perl and Ruby, I know about what some languages call hashes. That would work.

My first exposure to hashes, also known as key-value pairs, was in "Learning Perl" (and there are many good web explanations of how to create a hash in Perl).

(Side note: As I'm learning JavaScript, I'm trying to learn Java at the same time. And yes, I know nobody recommends doing this. At first I was am more than a bit puzzled as to how to do a hash/key-value pair in Java. I still haven't think I figured it out with the help of Mozilla's great comparison of objects in JavaScript and Java.

Back to hashes in JavaScript. When looking for how to do a hash/key-value pair in JS, I quickly found out that JavaScript relies on what are called Objects to handle many data types.

I found a helpful page from the JavaScript Tutorial.

As a result, here is my JavaScript object (aka hash/key-value pair):

var acronyms = { 
    usa:    "United States of America",
    ussr:   "Union of Soviet Socialist Republics",
    usc:    "University of Southern California",
    csun:   "California State University Northridge",
    lsu:    "Louisiana State University",
    cpk:    "California Pizza Kitchen"
    lbj:    "Lyndon Baines Johnson"
}

This Mozilla page on JavaScript objects doesn't seem to address the kind of object I created above.

Looking at the Mozilla tutorial, I would create the object this way:

var acronyms = new Object();
    acronyms.usa = "United States of America";
    acronyms.ussr = "Union of Soviet Socialist Republics";
    acronyms.usc = "University of Southern California";
    acronyms.lsu = "Louisiana State University";
    acronyms.cpk = "California Pizza Kitchen";
    acronyms.lbj = "Lyndon Baines Johnson";

I'm going to play with both of these (and a few other objects I've set up) to see what works best for my program.

As a learning exercise (for me), here is the same object in Java:

public class acronym {
    public String usa = "United States of America";
    public String ussr = "Union of Soviet Socialist Republics";
    public String usc = "University of Southern Califoria";
    public String lsu = "Louisiana State University";
    public String cpk = "California Pizza Kitchen";
    public String lbj = "Lyndon Baines Johnson";

}

Usual caveat: I am no expert. I am LEARNING this material. This writing helps me "cement" that learning. I hope it helps you.

Who the hell am I? I am a journalist. What that means for me is that I push news to the web. Crashes, stabbings, shootings, fires. That's my morning, which begins at 5 a.m. I've dabbled in coding a bit, and been more of a "fixer" in the sysadmin/support realm. That means when things break, I try to fix them.

The last five or so years, I've been focused on Linux/Unix, blogging and blogging software. After using Unix in college to write my papers way (way) back in the day, the idea of running my own Unix system in the form of Linux and BSD pretty much blew my mind. I wrote hundreds of blog posts on that journey.

So I know a lot more about Linux than I do about coding.

I've been thinking of getting serious about coding over the past three years, if I'm being honest with myself. I've actually been doing something about it over the past year. Now I'm stepping on the gas.

As far as "real" apps go, I've made a few. They've all been things I could do with the skills I had at the time. It's hard for me to learn without that learning going into something real and immediate. I want to make things. To solve problems.

About a year ago, I did a fairly extensive bash script, basically a huge sed hack, that turns Los Angeles County and State of California election data into usable HTML for the nine (yes, NINE) websites I work on. It's not pretty in any way. It probably would be better using something other than bash. And I should have used a web framework instead of a cron job to update the HTML at regular intervals. But I used what I knew to make something.

Also over the past year, I've coded a couple of scripts, one in Perl, the other in Go, that output a timestamp line I need for the Ode blogging system I use for this site. That's like a three-line program. Nothing heavy. I heard about Go a few months ago and found out that it was easy to rewrite my Perl script in this newish, Google-created language. It isn't hard for a #CodeNewbie to figure out Go, trust me.

I've also written a couple of bash scripts that automate the posting of new entries to my Ode site, leveraging the great Unison and curl utilities to do so. (I think I used wget in at least one, but I switched to curl because that ships in Mac OSX, but wget doesn't.)

About a month ago, I had an idea for a Ode plugin that the project's developer, Rob Reed, graciously helped bring to life. It's basically a Perl script that crawls the filesystem on the server and tells the reader how many posts are in the blog. I figured out how to do it on my local filesystem, and Rob helped Ode-ize that script into a full-fledged Ode plugin.

Rob is a great developer who values education very highly. He created Ode as a teaching tool and has been an inspiration to me as long as I've known him and run his software.

Do you like how I shoved a coding bio at the bottom of this post about key-value pairs in JavaScript and Java? I guess it's a bit stream-of-consciousness.

Next steps for my new app: Figure out the JavaScript to iterate over this object, deliver results based on user input, and create HTML forms for that input.