Perl Programming Page 8 - Perl 101 (Part 4) - Mind Games |
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(): #!/usr/bin/perl
# 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
close(MIND);
# 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
close(MIND);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: #!/usr/bin/perl
# 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: This is a string 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: #!/usr/bin/perl
# 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: This:is:a:string 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. #!/usr/bin/perl
# 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.
blog comments powered by Disqus |