Re: Removing entry from @rray



Robert Valcourt <webmaster@xxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
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).

The methods don't do the same thing. The first one actually removes
the entry from the array (the array length shrinks by one). The second
method doesn't change the array but replaces the element with an empty
string. After printing all entries both results look the same but the
arrays are different after each one.

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.

The simplest method (then and now) to remove an entry is grep:

@entries = grep $_ ne $entry, @entries;

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?

perldoc -f last.

You can also use List::Util::first to find the entry efficiently
(first() leaves the loop after the first match):

my $i = List::Util::first { $_ eq $entry } @entries;
splice @entries, $i, 1 if defined $i;

This should be faster if the lists are very long. Just how long
"very long" is can only be decided by benchmarks.

Anno
.



Relevant Pages

  • Re: Removing entry from @rray
    ... entry from an @rray. ... and places each line into the array. ... Redefining the @array element inside to loop ... I would also like to ask about terminating a loop ... ...
    (comp.lang.perl.misc)
  • Removing entry from @rray
    ... For some time I have been using two different method for removing an entry ... Redefining the @array element inside to loop ... I would also like to ask about terminating a loop ... ...
    (comp.lang.perl.misc)
  • Re: This question seems simpler than it actually is...
    ... find a lot of logic designers forget what HDL stands for... ... For the exercise I went ahead and did this with a 6 element array. ... type invect is array of entry; ... case mystate is ...
    (comp.lang.vhdl)
  • Re: Add a Value to an ArrayList
    ... add the VALUE of u at the point in the for loop, ... don't create a new instance and assign its reference to "u" each time ... then you're going to wind up with an array that has the ... reference will result in getting the same data regardless of which entry in ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: A critique of test-first...
    ... > silly to give a coding test to people of our level; ... Each message will have the cummulative probability. ... To find a random message get a number from randand find the entry ... Use a sparse/dense array of chars. ...
    (comp.programming)