Removing entry from @rray



Hello,

For some time I have been using two different method for removing an entry
from an @rray. It is my beleif that these methods may be outdated or
erroneous and I wanted to get some opinions. My program reads the contents
of a text file, and places each line into the array. Here are my two
methods:

Using Splicing
=========================

open (ADDRESSES, "<addressbook.txt") or die "Can't open file: $!";
@entries=<ADDRESSES>;
close(ADDRESSES);

$variable = "Smith";

foreach $entry(@entries) {
$location++;
if ($entry eq $variable) {
$location--;
$line = $location;
}
}

splice(@entries, $line, 1);

First of all I understand that many of you will first comment on the means
by which I read the text file and place its content into the array or that
strict isnt used above, and I will be fixing that soon using SLURP and
calling the "use strict" header.

So this method uses the old splice routine. I find this method to be
confusing and im not quite a fan of it. On to the next:

Redefining the @array element inside to loop
=========================

open (ADDRESSES, "<addressbook.txt") or die "Can't open file: $!";
@entries=<ADDRESSES>;
close(ADDRESSES);

$variable = "Smith";

foreach $entry(@entries) {
if ($entry eq $variable) {
$entry = "";
}
}

This method simply remodifies the $entry with a null value killing its data
and the following \n inside the loop (at least I think it does).

Both of these methods do the trick but I have a feeling that neither may be
the most efficient way or worse these methods may have side effects that I
am unaware of. Alot has changed in the world of Perl since I started using
these methods and I wanted to know if there is a better/safer/more efficient
way to do this.

I would also like to ask about terminating a loop ... obviously after a
match is found I would want to terminate the loop so that it doesn't waste
time checking all the other array elements for a match. Suprisingly I was
not able to source this answer. I once read somewhere that a command called
"return" or something is used, thoughts?

I appriciate you time on this, Thanks much,

Robert


.



Relevant Pages

  • RE: Creating a que
    ... and entry count in a collection. ... if eo.Count> 0 then bMoreData = True ... At the beginning of the loop you assume this is the last time through the ... Then you go through each item in the Entries ...
    (microsoft.public.excel.programming)
  • RE: Creating a que
    ... and entry count in a collection. ... if eo.Count> 0 then bMoreData = True ... At the beginning of the loop you assume this is the last time through the ... Then you go through each item in the Entries ...
    (microsoft.public.excel.programming)
  • Re: Very confused by Ada tasking, can not explain the execution outcome.
    ... I am trying to simulate a concurrent resource allocator with Ada's ... task and protected type. ... entry calls, so no entry is defined in tasks. ... loop, 'while rounds<2 loop', how could it get out of the loop if ...
    (comp.lang.ada)
  • Re: How to Delete Rows in Excel In a Do Loop
    ... have to delete there are some that have a entry in each and every cell ... Richard Buttrey wrote: ... time you go through the loop. ... Now copy this formula down column A for the whole database ...
    (microsoft.public.excel.worksheet.functions)
  • RE: Creating a que
    ... and entry count in a collection. ... if eo.Count> 0 then bMoreData = True ... At the beginning of the loop you assume this is the last time through the ... Then you go through each item in the Entries ...
    (microsoft.public.excel.programming)