|
||||||||||||||||||||||||||||||||||
Perl Crash Course: Gettin’ jiggy wit itSo 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:
# 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:
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 "$i\n"; # output to STDOUT
}
else {
warn "$i\n"; # 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 bad\n"); # 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])
$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])
$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) $string = "Hello World"; $chopped = chop($string); print $string; # Hello Worl print $chopped; # d chomp($string) $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 Array functions join
@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 @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 @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 @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
while(($key,$value) = each (%hash)) {
print $key, " => ", $value, "\n";
}
# Prints
# key1 => val1
# key2 => val2
exists
%hash = (key1 => undef,
key2 => 'val2');
print "Got it!!" if exists($hash{key1}); # returns Got it!!
delete
delete($hash{key1});
print "Got it!!" if exists $hash{key1}; # does not print anything
« References and Complex Data Structures (CDS) | TOC | Basic Regular Expressions » |
||||||||||||||||||||||||||||||||||
|
Copyright © 2010 use strict ;#) - All Rights Reserved |
||||||||||||||||||||||||||||||||||