Black Bytes
Share this post!

Category Archives for Programming

PHP Arrays

This is an introduction to PHP arrays, so if you are new to the language or you just need a refresher you can get up to speed pretty fast.

This is how we initialize an empty array and add some elements to it:

Then we can see it’s contents using the print_r() function. You may want to put this between <pre> tags for better formatting.
Continue reading

SQLite basics

SQLite is a serverless relational database. It is used notably by modern browsers like Chrome and Firefox to store data like history, cookies, saved passwords…

We are going to see how you can get around in the command line interface. To work with a database file you can start the program in this way:

What you get is a prompt similar to the mysql client. Now let’s see what tables are available:

If we want to see column names we can use the .schema command:

To customize the output format of query results you can use the .mode and .headers options, for example:

You can add these options to .sqliterc for permanent effect. Finally, if you want to exit just type .exit

-> Something annoying about sqlite is that the ALTER TABLE command is very limited. You can only rename a table or add a column, but you can’t modify or delete columns after they have been created. There is an official work-around for this, explained here:

Using SQLite in Ruby:

You might also like:
Redis: the blazing fast datastore

Java: Regular Expressions

Regular expressions, regexp for short, allow us to build expressions for pattern matching. If you aren’t familiar with them you may want to check out some resources here.

In Java you can leverage the power of regexp using the pattern and matcher classes, you can import java.util.regex.* to make them available to your program. Let’s see some examples:

This is a simple method that takes in a pattern and a string to compare against and returns either true or false. We define patterns as strings, for example if we want to see if a word starts with any character but ends with “ello” we would call our method like this:

Which in this case will return true, as an alternative you could also use the String method startsWith()

Example of java regular expressions

Now lets say we want to find all words that match a certain pattern in a piece of text, rather than just comparing against a single word.

This method is a little more involved than the first one. First we need to compile our regexp and get a pattern object then we call the matcher method on our pattern object with the string we want to match against, and what we get is a matcher object which contains the results. Finally we loop over the results and print them to the screen.

Here is a possible call to our findWithPattern method.

For more details check the official documentation.

Ruby tracing – A useful debugging tool

Tracing is following all the steps taken by a program, specially function calls/methods, this can be a useful debugging tool when tracking down some problems with your application.

In Ruby we have a tracing tool built-in, we can invoke it with ruby -rtracer script.rb but as you can see here it’s not easy to tell what’s going on:

What we can do is implement our own tracer and format the output to our liking, for this Ruby provides us with the set_trace_func method, we setup our tracing by giving this method a proc that will be called for each tracing event.

Here is a summary of what each argument means:

  • event, this is whats happening in this step of execution, it can be one of the following: “c-return”, “end”, “return”, “c-call”, “line”, “call”, “class”
  • file, this is the file where the event happens
  • line, the line number
  • id, this is the method name we are in
  • binding, the current scope where we are running
  • classname, this one doesn’t need much explanation 🙂

I have made a gem that implements this and focuses on ruby method calls:

You can call it with just “st my_script.rb” and this is how it looks:

ruby tracing

Alphanumeric sorting

Time for a bit more sorting. Today our task is to sort an array of file names based on the numeric value. This what the array looks like:

If you try a simple sort it won’t come out as you expect…

Ruby sort by value

This is not what we want, so we will have to reach for sort_by again:

And this time we get what we want. What we are doing here is taking each element of the array and using a regexp to scan for decimal numbers (\d+ or also [0-9]+) since we get an string back we need to convert it to a number with to_i. Hope you find it useful.