Home arrow Perl Programming arrow Page 6 - Perl 101 (part 6) - The Perl Toolbox

Slice And Dice - Perl

This time, Perl 101 visits some of Perl's more useful in-builtfunctions, and teaches you the basics of pattern matching and substitution.Also included is a list of common string and math functions, together withexamples of how to use them.

  1. Perl 101 (part 6) - The Perl Toolbox
  2. Expressing Yourself
  3. Engry Young Men
  4. Aardvark, Anyone?
  5. Needles In Haystacks
  6. Slice And Dice
  7. Going Backwards
  8. Math Class
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
August 30, 2000

print this article


Next up, the substr() function. As the name implies, this is the function that allows you to slice and dice strings into smaller strings. Here's what it looks like:

substr(string, start, length)

where "string" is a string or a scalar variable containing a string, "start" is the position to begin slicing at, and "length" is the number of characters to return from "start".

Here's a Perl script that demonstrates the substr() operator:

# get a string
print "Gimme a line!\n";
$line = ;
chomp ($line);
# get a chunk size
print "How many characters per slice?\n";
$num_slices = ;
chomp ($num_slices);
$length = length($line);
$count = 0;
print "Slicing...\n";
# slice the string into sections
while (($num_slices*$count) < $length)
$temp = substr($line, ($num_slices*$count), $num_slices);
print $temp . "\n";

Here, after getting a string and a block size, we've used a "while" loop and a counter to keep slicing off pieces of the string and displaying them on separate lines.

And here's what it looks like:

Gimme a line!
The cow jumped over the moon, giggling madly as a purple pumpkin with fat
ears exploded into confetti
How many characters per slice?
The cow jum
ped over th
e moon, gig
gling madly
as a purpl
e pumpkin w
ith fat ear
s exploded
into confet

You've already used the print() function extensively to sent output to the console. However, the print() function doesn't allow you to format output in any significant manner - for example, you can't write 1000 as 1,000 or 1 as 00001. And so clever Perl programmers came up with the printf() function, which allows you to define the format in which data is printed to the console.

Consider a simple example - printing decimals:

print (5/3);

And here's the output:


As you might imagine, that's not very friendly. Ideally, you'd like to display just the "significant digits" of the result. And so, you'd use the printf() function:

printf "%1.2f", (5/3);

which returns


A quick word of explanation here: the Perl printf() function is very similar to the printf() function that C programmers are used to. In order to format the output, you need to use "field templates", templates which represent the format you'd like to display.

Some common field templates are:

%s string
%c character
%d decimal number
%x hexadecimal number
%o octal number
%f float number

You can also combine these field templates with numbers which indicate the number of digits to display - for example, %1.2f implies that Perl should only display two digits after the decimal point.

Here are a few more examples of printf() in action:

printf("%05d", 3); # returns 00003
printf("$%2.2f", 25.99); # returns $25.99
printf("%2d%", 56); # returns 56%

And here's a calculator which uses the printf() function to display numbers in various numerical bases like hexadecimal and octal.

print "Enter a number: ";
chomp($number = );
printf("In decimal format: %d\n",$number);
printf("In hexadecimal format: %x\n",$number);
printf("In octal format: %o\n",$number);

Perl also comes with a sprintf() function, which is used to send the formatted output to a variable instead of standard output.

This article copyright Melonfire 2000. All rights reserved.

>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: