Perl Crash Course: Gettin’ jiggy wit it

Built in Perl functions for Scalars, Arrays, and Hashes

Share This Post

So far we’ve seen how Perl’s basic data structures work and also how to put them all together. Now it’s time to see what built-in functions and techniques Perl provides us to work with scalars, arrays, and hashes.

General functions

print [FILE HANDLE] list of data

Used to print data. If not passed a FILE HANDLE, it will print directly to STDOUT. Interpolation rules apply. Some interesting details about print are:



print @array;  # prints all elements of @array with 
               # no spaces between them
print "@array";  # prints the elements WITH spaces between them
print $var1, $var2, @array;  # prints values with no spaces between them
print FH "some data";  # prints the string "some data" inside file represented by FH (see open below).
# Note the lack of comma between the FH and the data to be printed.

open (FILE HANDLE, mode, filename) or open (FILE HANDLE, “modefilename”)

Opens a file or pipe for reading/writing/appending. Normally filehandles tend to be constants in uppercase, but it’s good practice to use variables to hold your filehandles. That way you can pass them between functions without occupying the main namespace. There are 2 styles of open – old style with 2 parameters and new style with 3.

open‘s modes are:
< (readonly),
> (create/write/truncate),
>> (create/write/append),
+< (read/write/truncate),
+> (create/read/write/truncate),
+>> (create/read/write/append)


# old style - for reading
open (FH, "</path/to/somefile.txt") || die("Can't open file: $!n"); # $! returns the system error
# do something here
close(FH);

# new style - also for reading
open (my $fh, "<", "/path/to/somefile.txt") || die("Can't open file: $!n");
# do something here
close($fh);

When working with pipes, filenames begining with | indicate a pipe to write to. Filenames ending with | indicate filenames to read from. Use the special minus ‘-‘ sign to read from standard input.

printf($format_string,$value1[,$value2,$valueN]) print‘s older brother – takes patterns and prints a formatted string. It is extremely powerful and most people do not use it to its full extent.

These are the formatting options you can use:

Format Result
%% A literal percent sign
%c A character with the given ASCII code
%s A string
%d A signed integer (decimal)
%u An unsigned integer (decimal)
%o An unsigned integer (octal)
%x An unsigned integer (hexadecimal)
%X An unsigned integer (hexadecimal using uppercase characters)
%e A floating point number (scientific notation)
%E A floating point number, uses E instead of e
%f A floating point number (fixed decimal notation)
%g A floating point number (%e or %f notation according to value size)
%G A floating point number (as %g, but using .E. in place of .e. when
appropriate)
%p A pointer (prints the memory address of the value in hexadecimal)
%n Stores the number of characters output so far into the next variable in
the parameter list



printf("Hello, My name is %s. Today is the %dth day of %s.", $name,$day,$month);
printf("This item costs $%.2f.",$some_number);  # escape the dollar sign
printf("I need 2 %%'s to get one printed with printf");

sprintf($format_string,$value1[,$value2,$valueN])

Works the same as printf, but instead of printing the output, it returns the formatted string (allowing you to store it in a variable for later use).



$string1 = sprintf("Hello, My name is %s. Today is the %dth day of %s.", $name,$day,$month);
$string2 = sprintf("This item costs $%.2f.",$some_number);  # escape the dollar sign
$string3 = sprintf("I need 2 %%'s to get one printed with printf");

warn($message)

Use warn to print messages into STRERR without exiting your script. Besides the regular usage of letting the user know when something goes bad (but not fatally so), I also find it handy to filter data from a given input file using print and warn along with shell redirectors (>log.out and 2>err.out). See the example below to better understand what I’m talking about.



# in odd_even.pl
foreach my $i (0..10) { # .. = "to", so for $i == 0 to $i == 10
	if ($i % 2 == 0) { # Modulus (%)  operator, returns the remainder of a division
		print "$in"; # output to STDOUT
	}
	else {
		warn "$in"; # output to STDERR
	}	
}

# in the command line
./odd_even.pl > even.txt 2 > odd.txt

die($message)

Print a message into STDERR and exit the script. If the message inside the die statement ends in a newline, only the message will be printed. If no newline is found, it will print additional information such as line number and script name.



if ($some_fatal_error) {
	die("ACK!! Something went really badn"); # prints out "ACK!! Something went really bad"
}

if ($some_other_error){
	die("ACK!! Something went really bad"); # prints out "ACK!! Something went really bad at script.pl line someline"
}

Scalar functions

split (/pattern/, $string [,number_of_elements])
split breaks a string at a given pattern into an arbitrary number of pieces. It returns a list which is usually assigned into an array for later use. Use an unescaped ‘|’ (pipe) to split strings at each letter.



$string = "Hello World";
@array = split /|/,$string; # same as @array = ('H','e','l','l','o',' ','W','o','r','l','d');

$string2 = "Name,Age,Street,Phone_number,Skills";
($name,$age,$rest) = split (/,/,$string2,3); # $name has 'Name', $age has 'Age', and $rest has 'Street,Phone_number,Skills'

substr($string,offset[,length])
Returns a piece of a string starting from a given offset and having a given length. If the length is ommitted, it will return until the end of the string. Positive offsets count from left to right. Negative offsets count from right to left. It is also handy to get padding done on a string


$string = "Hello World";
print substr($string,0); # Hello World
print substr($string,3,4); # lo W
print substr($string,-5); # World
print substr($string,-5,2); # Wo

$padding_template = "00000";

foreach my $i (10, 100, 1000, 10000) {
	
	print substr($padding_template . $i , (length($padding_template) * -1)), "n";
	# the line above will evaluate to the following stirng:
	# print substr(00000$i,-5),n; where $i will be 10, then 100, then 1000, then 10000

}

# results in 
# 00010
# 00100
# 01000
# 10000

chop($string)
Used to cut off and return the last character of a string. The variable is altered automatically. Note: you cannot chop on a literal string. The value to be chopped must reside in a variable. The reason for that is due to chop modifying the original string to hold the chopped version.



$string = "Hello World";
$chopped = chop($string);

print $string; # Hello Worl
print $chopped; # d

chomp($string)
Similar to chop, however it will only remove the last character if and only if it is a newline (n). The same applies to not being able to chomp a literal string. See the shorthand example if you want to assign/chomp during the same command.



$string = "Hello World";
chomp($string); # no change, since there's no n at the end of $string

chomp($string2 = "with newline n"); # shorthand version
print $string2, $string; # with newlineHello World

pack and unpack
A couple of the most powerful string manipulation functions (inherited from C), it’s so complex that I recommend that you read this to see how it works.

Array functions

join
Opposite of split. Takes an array and joins its values into a string using nothing, a given character, or a string as field separator. I find it handy to create large SQL queries with it. (See example below).



@array = qw(Red Green Blue Black); # qw() puts double quotes around each word separated by space
$string = join(',',@array); # $string is "Red,Green,Blue,Black"

shift and unshift
Use shift to remove and assign the first element of an array, and unshift to do the opposite. Shift is mostly used inside subroutines to get elements from the special @_ array (@_ contains all parameters passed onto a subroutine).

@colors = qw(Red Green Blue Black);

$color = shift @colors; # or shift(@colors);
print "@colors";  # Green Blue Black;
print $color; # Red

unshift(@colors,$color);
print "@colors"; # Red Green Blue Black

pop and push
Similarly to shift and unshift, pop and push work on the right site of an array.



@pets = qw(Cat Dog Bird Fish);
$mypet = pop @pets;
print $mypet; # Fish
print "@pets"; # Cat Dog Bird

push(@pets,$mypet);
print "@pets"; # Cat Dog Bird Fish

array slices
Not a function, but a syntax structure which allows you to access subsets of an array. Keep in mind Perl’s singular/plural constructs (using @ instead of $) while using a list, array, or range as your index.



@pets = qw(Cat Dog Bird Fish);
print $pets[0]; # Cat - NOT a slice
print @pets[0,1,2]; # CatDogBird
print @pets[0..2]; # same thing
print @pets[qw(0 1 2)]; # same thing

Hash functions

each
each iterates through the key/value pairs of a hash, returning them in a list context.



while(($key,$value) = each (%hash)) {

	print $key, " => ", $value, "n"; 

}

# Prints
# key1 => val1
# key2 => val2

exists
Checks for the existence of a key in a hash. Will return true even if the key’s value is false (undefined, empty string, zero, etc.).



%hash = (key1 => undef,
         key2 => 'val2');
		 
print "Got it!!" if exists($hash{key1}); # returns Got it!!

delete
Removes a key/value pair from a hash. Use it when you want exists to return false.

delete($hash{key1});

print "Got it!!" if exists $hash{key1}; # does not print anything

« References and Complex Data Structures (CDS) | TOC | Basic Regular Expressions »

More To Explore

Documentation
vinny

bbPress Notify (No-Spam) Documentation

bbPress Notify (No-Spam) Documentation The bbPress Notify (No-Spam) is a free plugin that lets you control who gets notifications of new Topics and Replies in

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.