value

Perl hash sort - How to sort a Perl hash by the hash value

Question: How do I sort a Perl hash by the hash value?

First ... sorting a Perl hash by the hash key

If you need to sort a Perl hash by the hash key, this is a pretty well-known recipe. It's covered in another Q&A article titled "How to sort a hash by the hash key".

Perl environment variables - How to access Perl environment variables

Here's a quick example program that demonstrates how to access environment variables from within your Perl programs:

# environment variables are held in the %ENV hash
foreach $key (sort keys %ENV)
{
  print "$key is $ENV{$key}\n";
}

Using this simple Perl foreach loop, here's a subset of the output this script prints on my MacBook Pro:

An apology, and a good laugh

I just wanted to offer a quick apology to everyone that has sent me emails lately. Yes, I have received your messages, and I will get to them as soon as possible. I'm just a bit swamped right now, so please bear with me, and I'll get to them all as soon as possible.

Perl true and false - What is true and false in Perl?

Perl is a little unusual in not having true and false boolean operators. Because of this, and my advancing age, I can never seem to remember what equates to true and false when using Perl, so I decided to create this page.

Perl true false summary

In short, the following elements in Perl will equate to "false":

  • The number zero (0) means false.
  • The string zero ('0') means false.
  • The empty string ('') means false.

Lots of other things equate to "true", including:

MySQL example: Default a field to the current date/time

MySQL Question: I need to create a field in a MySQL database table to default to the current date and time whenever a new record is added to my table.

How to add an item to a Perl hash

Perl FAQ: How do I add an item/element to a Perl hash?

Answer: Adding an item to a Perl hash is just like creating a Perl hash initially. Just assign the new item to the hash with the correct syntax, and you're in business.

In the following sample code I show how this is done by creating a hash named prices. The hash key is the name of a product, like pizza, and the hash value is the price of that product. Here's my Perl hash sample code:

How to return multiple values from a Perl function

One of the things I really like about Perl is that you can return multiple values from a function (and you don't have to create some type of artificial class to encapsulate them). Here's the basic way to return multiple values from a function/subroutine named foo:

How to print a Ruby hash sorted by value

I recently needed to print the information in a Ruby hash, with the results sorted by value. Here's a general recipe on how to print the hash results sorted by value. I've created a sample hash, and populated the hash with sample data, to show how this works.

First, here's the sample code, using the first name of each person as the key, and the last name as the value of the key/value pair:

Syndicate content