Scala, Java, Unix, MacOS tutorials (page 375)

Perl random integer FAQ: Can you provide a Perl random integer example?

In my earlier Perl random tutorial, I described several different ways of generating random numbers in Perl. Today I'd like to share a Perl script I wrote that uses a random integer number to rotate files on this website.

A Perl random integer script example

The following Perl script performs the following functions:

Perl random number FAQ: Can you show me some examples of how to get a random number in Perl?

Perl random number - solution

In its simplest form, if you just need a random decimal number between 0 and 1.0, you can use the Perl rand function like this:

# generate a random number in perl with the rand function

my $random_number = rand();
print $random_number, "\n";

When I save this Perl random number code to a file and run it three times, I get these results:

Perl until loop FAQ: Can you share some examples of the Perl until loop syntax?

The Perl until loop is similar to the Perl while loop, but essentially does the opposite thing. Where the Perl while operator will run this loop forever:

while ('true')
{
  # do stuff here
}

the Perl until loop will never run this loop:

Perl hash "add" FAQ: How do I add a new element to a Perl hash? (Or, How do I push a new element onto a Perl hash?)

The Perl hash is a cool programming construct, and was very unique when I was learning programming languages in the late 1980s. A Perl hash is basically an array, but the keys of the array are strings instead of numbers.

Basic Perl hash "add element" syntax

To add a new element to a Perl hash, you use the following general syntax:

Perl unless FAQ: Can you share some examples of the Perl unless operator?

Perl has a cool keyword named "unless" that can make your code easier to read and write from time to time. The Perl unless operator is similar to the Perl "if" keyword ... but a little different.

A Perl unless/else example

Here's a quick example (inspired by one of the Star Trek movies) that shows how to use the Perl unless syntax. Hopefully it's fairly easy to read, in part due to the unless operator:

Perl hash FAQ: Can you share some simple Perl hash examples?

Sure. If you're not familiar with them, a Perl hash is simply a Perl array that is indexed by a string instead of a number. A Perl hash is like a Map in the Java programming language, or an array in PHP.

Perl hash - Background information

To get started looking at a hash in Perl, let's look at a simple example. First, let's assume that Perl hashes don't exist. Next, lets assume that we need to store the prices of various food items you'll find in a restaurant.

One item I forgot to include on any of my "geek gift ideas" holiday shopping lists are the Bose Around-Ear headphones. I spent almost $300 on different headphones and earbuds last year, until I finally bought a set of these headphones earlier in 2009.

Here's what I like about the Bose Around-Ear headphones:

I'm looking at getting another Mac for the office (a free Mac for everyone at the devdaily.com world headquarters, that's what I always say!), this time a Apple MacBook, and after some digging around, Amazon currently seems to have the best price on a MacBook.

On the date of this posting (November 26, 2009), they have the MacBook at $884.98, with free Super Saver shipping. (I'd link to Amazon here, but their URLs are just insanely long these days.)

A few days ago a friend and I were talking about gifts for geeks, and she told me about a "smart pen" from a company named Livescribe. When I went to their website and looked at the Livescribe pen, I originally saw the price as $24.95, and I thought wow, that's a pretty good deal; you must have to buy their paper refills, and that's probably where they make all their money.

After that, I had a very good laugh.

I took a little time off this morning and went on an exploring trip, to see what new, free software (and shareware) is available for Mac OS X.

MySQL FAQ: How do I define a MySQL auto increment field?

Here's an example of the way I normally create a MySQL auto increment (auto_increment) field:

Perl subroutines - multiple return values FAQ: Can you share some examples of how to return multiple values from a Perl subroutine?

Did you know that you can return multiple values from a Perl subroutine (function)? As a practical matter I haven't used this feature very much, but I've always thought it was an interesting programming language feature, very different from many other languages.

Perl printing FAQ: Can you share some Perl printing examples?

There are several different ways to print in Perl, and I thought I'd share some examples here today.

The Perl print function

Generally you'll print simple output with the Perl print function. As a simple example, you can print a string literal using the Perl print function, like this:

MySQL foreign key FAQ: How do I define a foreign key in MySQL?

Answer: Here's a quick example of how I typically define a foreign key in MySQL.

Diving right into an example, here's the definition for a MySQL database table named nodes that I will link to from a second table:

[toc hidden:1]

After seeing some ads for the Java Store on this website, I just read up on it again, and it sounds pretty interesting. The Java Store is currently in a beta program state ... hmm, I just finished poking around, and I can't find any information on when they intend to go from a Beta to a Live state, but after seeing the ads here, I'm wondering how close they are to going live.

Perl read file FAQ: How can I read a file into an array in Perl (Or, Can you share a Perl read file into array example?)

One of the really cool things about Perl is that it's super-easy to read a file into a Perl array. This is really handy any time you need to read a file for any reason.

A Perl read file into array example

If you want to read a file into a Perl array, you actually only need one line of code, like this:

Perl reverse array FAQ: How do I reverse the contents of an array in Perl? (or, How do I use the Perl reverse function?)

It turns out that it's easy to reverse a Perl array, especially if you have already defined the sorting/comparing algorithm that Perl needs to sort the array.

A Perl reverse array example

For example, if you just want to reverse the lines in a Perl array that you've read from a text file, you can just use this Perl script:

[toc hidden:1]

Summary: How to sort Perl arrays, in this case, a Perl integer array.

Sorting a Perl integer array (technically a Perl numeric array) is relatively simple, well, at least once you know the magic formula. The key thing to know is that you need to provide the Perl sort function a helper function (or block of code) that tells it how to sort integers. By default, the Perl sort function sorts arrays in ASCII order, and that's not going to work very well for you.

Perl array push pop FAQ: How do I push elements onto a Perl array, and how do I pop element off a Perl array? (Or, What is the Perl push and pop syntax?)

I really like the Perl push (and Perl pop) syntax. The push function makes adding elements to a Perl array very easy, and the pop function also makes a Perl array work a lot like a stack.

Perl string array sorting FAQ: How can I sort a Perl string array in a case-insensitive manner?

I mentioned in my first Perl array sorting tutorial that the default Perl sorting algorithm sorts characters by their ASCII values. Because of that, my simple Perl array sorting example using the following strings worked just fine:

# a simple perl string array
@pizzas = qw(pepperoni cheese veggie sausage);

However, if I add a few uppercase strings to that string array, like this: