perl array

Perl string character processing - How to process every character in a string

Perl string processing FAQ: How can I process every character in a Perl string?

I recently had to write some Perl code to process every word in a file, and that made me wonder how to process every character in a Perl string. I didn't know how to do this, but I just cracked open my copy of the Perl Cookbook, and found a couple of possible solutions.

Perl hash add element - How to add an element to a Perl hash

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 hash introduction tutorial

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.

A Perl “read file into array” example

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 array/sort FAQ - Perl integer array sorting

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 and pop syntax and examples

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 case-insensitive string array sorting

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:

Perl array copy example: How to copy an array in Perl

Perl array copying FAQ: Can you share some examples of how to copy a Perl array?

Copying a Perl array is actually so simple it seems like a trick question, but unfortunately it's one of those things that is only easy once you know how to do it. (Personally I find the syntax a little non-standard.)

A Perl copy array example

To copy a Perl array named @array1 to a Perl array named @array2, just use this syntax:

A Perl reverse array example - How to reverse file contents

Perl reverse array FAQ: Can you show an example of how to reverse a Perl array (reverse the Perl array contents)?

Sure. I just create a Perl script to print the contents of a text file in reverse order. I named this program rcat (for "reverse cat"), and I use it as a helper program with my transport command (a replacement for the Linux cd command). We can use it to demonstrate the Perl "reverse array" technique.

Perl string array - How to create and use an array of strings

With Perl we work with strings and arrays (or lists) of strings all the time. I thought I'd take a few moments here to show the most common ways to create a Perl string array, and also to loop through (or iterate through) the list of strings I create.

How to create a Perl string array

When you first start to work with Perl, you might create a string array like the following example: