This tutorial will teach you how to remove a single line from a text file.
This may be useful if you have a block list, with one person on each line
in the file; or you may have greater uses for it.
This tutorial will show you some example code, and then explain how it works
in a more easy to understand kind of way.
In this example, we'll start with a text file that has a list of colors. Our
final goal will be to remove the color green from the list. Here's how
our list started:
red
blue
green
yellow
cyan
fuchsia
|
Now, we want to remove the color green from the list above. Here's the
code that would do that:
# The color we want to remove.
my $wanted = "green";
# Open the text file and load it into an array.
open (OLD, "./colors.txt");
my @colors = <OLD>;
close (OLD);
# Chomp @colors to remove trailing line breaks.
chomp @colors;
# Create a new array that will store the new file
# (without the color "green" because we're trying
# to get that one out of there!). Let's call it @result.
my @result = ();
# Go through each item and find the color
# we want: green.
foreach my $color (@colors) {
# If this is the color we're trying to remove, we
# will NOT add it to @result. Everything else will
# go into @result, and therefore this item will be
# left out.
# See if THIS is the one we want!
if ($color eq $wanted) {
# It is! We will NOT add this to @result because
# we don't want to keep it.
}
else {
# This is NOT the one we're looking for, so we
# want to keep it. Add it to $result to keep it
# for later.
push (@result, $color);
}
}
# At this point, @result should NOT contain the
# color "green," however it should have every
# other color that it started out with.
# Save the new data.
open (NEW, ">./colors.txt");
print NEW join("\n",@result);
close (NEW);
|
After all that, colors.txt should now read:
red
blue
yellow
cyan
fuchsia
|
Notice that the color green is no longer in the list, because we just
removed it.
Now for an explanation of that code:
In the first part, we opened the original file that contained every color
(including "green"). We're looking for the color "green" out of the list
so that we can remove it.
After chomping the list and preparing it for parsing, we created a variable
called $result. If all went well, $result should be the entire list we started
out with, the only difference being that "green" wouldn't be in that list.
So at this point, $result is nothing but an empty variable.
Next, we went on a foreach loop, to check each color in that list of
colors we had. The Perl interpreter would read it like this:
|
Checking each $color out of array @colors...
ITEM: red - Not what we want, add it to $result.
ITEM: blue - Not what we want, add it to $result.
ITEM: green - THAT'S IT! That's the one we
were looking for! Let's leave it out of $result, we don't want to
keep it.
ITEM: yellow - Not what we want, add it to $result.
ITEM: cyan - Not what we want, add it to $result.
ITEM: fuchsia - Not what we want, add it to $result.
|
After doing that, $result would have every color except for green in it.
Value of $result:
red
blue
yellow
cyan
fuchsia
The line between blue and yellow is where green would have been, but
the code was programmed to leave green out of the list.
After having a new list that's completely rid of that horrible green
we wanted to get out of there, it then saves the list back into the
same file it came out of.
And so, in the end, the file no longer has the color green. I hope I
didn't confuse too many people with this tutorial! It's really a simple concept.