Home arrow Perl Programming arrow Page 8 - Perl 101 (Part 4) - Mind Games

Shifting Things Around - Perl

This week's article teaches you how to use Perl to interact withfiles on your system, and also provides you with a quick crash course invarious array functions.

  1. Perl 101 (Part 4) - Mind Games
  2. Handle With Care
  3. Different Strokes
  4. A Little Brainwashing
  5. Die! Die! Die!
  6. Testing Times
  7. Popguns And Pushpins
  8. Shifting Things Around
  9. The Real World
  10. Miscellaneous Stuff
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 11
June 29, 2000

print this article


pop() and push() work on the last element of the array. If you'd prefer to add something to the beginning of the array list, you need to use unshift():
# open file and define a handle for it
open(MIND,"thoughts.txt") || die("Unable to open file!\n");
# suck the file into an array
@file = <MIND>;
# close file when done
# use a loop to keep reading the file
# until it reaches the end
foreach $line (@file)
print $line;
# ask for input and process it
print "How about a title?\n";
$title = <STDIN>;
# add title to beginning of array
unshift (@file, $title);
# open file for writing
open(MIND,">thoughts.txt") || die("Unable to open file!\n");
# print array back into file
foreach $line (@file)
print MIND $line;
# close file when done

And the file "thoughts.txt" will now contain a title, as entered by the user.

Obviously, removing the first element of an array requires you to use the shift() function - we'll leave you to experiment with that one yourself.

This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=The Greatest Things Since Sliced() Bread} Next, two of Perl's most frequently-used functions - split() and join(). split() is used to split a string value into sub-sections, and place each of these sections into an array, while join() does exactly the opposite - it takes the various elements of an array, and joins them together into a single string, which is then assigned to a scalar variable.

Let's take a simple example:
# set up a variable
$string = "This is a string";
# split on spaces and store in array
@dummy = split(" ", $string);
# print the array
foreach $word (@dummy)
print "$word\n";

Here's the output:

In this case, we're splitting the string using a space as the separator - each element of the split string is then stored in an array.

You can also join array elements into a single string:
# set up a variable
$string = "This is a string";
# split on spaces and store in array
@dummy = split(" ", $string);
# join the words back with a different separator
$newstring = join(":", @dummy);
# print the result
print "$newstring\n";

And the output is:

And finally, we have the splice() function, which is used to extract contiguous sections of an array [if you don't know what contiguous means, this is a good time to find out!]. Here's an example which uses the splice() function to extract a section of an array, and assign it to a new array variable.
# set up a variable
$string = "Why did the fox jump over the moon?";
# split on spaces and store in array
@dummy = split(" ", $string);
# extract three words
@newdummy = splice(@dummy, 1, 4);
# join them together and print
$newstring = join(" ", @newdummy);
print "$newstring\n";

The splice() function takes three arguments - the name of the array variable from which to extract elements, the starting index, and the number of elements to extract. In this case, the output would be:
did the fox jump

You should note that splice() alters the original array as well - in the example above, the original array would now only contain
@dummy = ("Why", "over", "the", "moon?");

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: