Perl Example #1
Basic Datatypes - Scalar, Array, and Hash

About The Program

This program uses variables of each of three basic types: scalar, array, and hash. Scalar variables can be numbers or strings, and the language will understand how to treat the data by the context of its use. An array is a linear collection of scalar data. A hash, or associative array, is a built-in data type that will associate a "key", with a piece of data.

The code presented here demonstrates how flexible these datatypes can be, such as having arrays that contain numbers as well as words, and the fact that array sizes can change dynamically. It also shows how the format of array output can be modified in a print statement using one of the special forms, "$,".

The output of the perl script shown in red italics has been interspersed between the Perl code in order to make it easier to follow what is happening in the program. The full program as well as the observed output are available as links at the bottom of the page.


#!/usr/bin/perl      
# The first line of the script envokes Perl 
# Use "/usr/bin/perl -w" option for debugging

# Scalar variables
$var1 = "Hello World";   
$var2 = 14.6;

# Array variables
@arr1 = (0,1,2,3,4);
@arr2 = ("zero","one","two","three","four");

# Hash variable, or associative array
%hash1 = ("one","Monday","two", "Tuesday","three", "Wednesday","four","Thursday");


# Some simple printing examples

print $var1;   # Printing out Scalar Variables
print (" ",$var2,"\n");

Hello World 14.6

print (@arr1,"\n");  # Print out the arrays
print (@arr2,"\n\n");

01234
zeroonetwothreefour

@arr3 = @arr1;  # Create a third array and copy everything 
print (@arr3,"\n");
print "\n";

01234

print ($arr1[0], "\n");  # Print specific srray elements (scalar values)
print ($arr2[3], "\n");
print "\n";

0
three

print (%hash1,"\n");    # Printing out the full hash array

threeWednesdaytwoTuesdayoneMondayfourThursday

$key = "two";
print ($hash1{$key}, "\n\n");  # Print out an element in the hash array 

Tuesday

# Here's where things get kewl...

$arr2[1] = $arr1[1];  # Working with different data types
$, = " ";   # Kewlness: Changing the separator between array elements
print (@arr1,"\n");
print (@arr2,"\n\n");

0 1 2 3 4
zero 1 two three four

$, = ": ";  # Change the separator again
print (@arr1,"\n");
0: 1: 2: 3: 4:

print (@arr2,"\n\n");
zero: 1: two: three: four:

print (%hash1,"\n\n");
three: Wednesday: two: Tuesday: one: Monday: four: Thursday:




$arr1[4] = $var1;  # Add on at the end of the array
print (@arr1,"\n");

0: 1: 2: 3: Hello World:

$arr2[7] = $var2;    # Go beyond the array
print (@arr2,"\n\n");

zero: 1: two: three: four: : : 14.6:

@arr1[3..5]=@arr2[2..4];  # Copy portions of one array to another
$, = " -> ";  # Change separator again
print (@arr1,"\n");

0 -> 1 -> 2 -> two -> three -> four ->

print (@arr2,"\n\n");


zero -> 1 -> two -> three -> four -> -> -> 14.6 ->

# Dealing with Hashing

print (keys %hash1, "\n");   #Print out the keys of the hash

three -> two -> one -> four ->

foreach $key ( keys %hash1)  # Cycle through all key
  {print $hash1{$key};
  }
print "\n\n";

WednesdayTuesdayMondayThursday

$, = ":";
print @arr1;  # Print array 1, just for reference
0:1:2:two:three:four:


for ($i=0; $i<7; $i++)   # Loop through all elements in array 1
  { print ($hash1{$arr1[$i]}, "\n");  #Print Hash value if it exists
  }

:
:
Tuesday:
Wednesday:
Thursday:
:

The Actual Code for Experimentation:   ex1.pl

The Actual Output:   ex1.out

dhyatt@thor.tjhsst.edu