Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Difficulty removing element from array

Reply
Thread Tools

Difficulty removing element from array

 
 
Robert TV
Guest
Posts: n/a
 
      09-21-2003
Hi ... I'm loosing much hair over this array element removal problem i'm
having. I will describe my setup. I have a web form with a single text box
named "recipient" ... on submit, the data is passed to my delete.pl script.
In the same folder, there is a text file called "addresses.txt" This file
contains the following data:

Gerald Genry|(E-Mail Removed)
Gerry Smith|(E-Mail Removed)
Robert TV|(E-Mail Removed)
Terry Valcourt|(E-Mail Removed)
Dan Perterson|(E-Mail Removed)
James Smiley|(E-Mail Removed)

These are names and email addresses separated by a "|" symbol. Here is the
delete script I have so for that is not functional:

#############

#!/usr/bin/perl

use Fcntl qw(EFAULT :flock);
use CGI::Carp qw(fatalsToBrowser);

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$form{$name} = $value;
}

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

foreach $entry (@entrys) {
$count++;
if ($entry eq $form{'recipient'}) {
$count--;
splice(@entrys, $count, 1);
}
}

print @entrys;
exit;

#############

If I type "Terry Valcourt|(E-Mail Removed)" in the web form and submit,
nothing happens. When @entrys prints, it still contains all entires read
from the text file. It's the same for any name and email address grouping
entered. To match the form input to the array element, I used the "eq"
operator. It is my belief that there is no successful matching because there
is a "\n" at the end of each "$entry". So I thought I might add a "chomp
($entry);" command to the equation that would remove the "\n"s from the end
of each $entry. Below is code with the chomp:

#############

#!/usr/bin/perl

use Fcntl qw(EFAULT :flock);
use CGI::Carp qw(fatalsToBrowser);

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$form{$name} = $value;
}

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

foreach $entry (@entrys) {
chomp ($entry);
$count++;
if ($entry eq $form{'recipient'}) {
$count--;
splice(@entrys, $count, 1);
}
}

print @entrys;
exit;

#############

This is where things get REALLY messed up. I go back to the web form and
type "Terry Valcourt|(E-Mail Removed)" This time when @entrys prints, all
lines of data have been removed except "Dan Perterson|(E-Mail Removed)" I've
spent hours trying to understand what is going on ... please help me see the
light!

P.S. I know most of you may look at my coding methods and laugh, bear in
mind i'm very new and still learning

Robert


 
Reply With Quote
 
 
 
 
Chief Squawtendrawpet
Guest
Posts: n/a
 
      09-21-2003
Robert TV wrote:
> foreach $entry (@entrys) {
> $count++;
> if ($entry eq $form{'recipient'}) {
> $count--;
> splice(@entrys, $count, 1);
> }
> }


You're modifying an array while iterating over it, which seems like a bad
idea to me. If the entries are unique you could store them in a hash
instead of an array, making the deletion process very easy.
# Read entries [not tested]
while (<ADDRESSES>){
chomp; # Don't you need to remove the newlines from your entries?
$entrys{$_} = 1;
}
# Delete one
delete $entrys{$form{recipient}};

If the entries must be in an array, you could use grep() to delete those
matching $form{recipient}.
# Not tested
@entrys = grep $_ ne $form{recipient}, @entrys;

Chief S.
 
Reply With Quote
 
 
 
 
David Efflandt
Guest
Posts: n/a
 
      09-21-2003
On Sun, 21 Sep 2003 02:07:53 GMT, Robert TV <(E-Mail Removed)> wrote:
> Hi ... I'm loosing much hair over this array element removal problem i'm
> having. I will describe my setup. I have a web form with a single text box
> named "recipient" ... on submit, the data is passed to my delete.pl script.
> In the same folder, there is a text file called "addresses.txt" This file
> contains the following data:
>
> Gerald Genry|(E-Mail Removed)
> Gerry Smith|(E-Mail Removed)
> Robert TV|(E-Mail Removed)
> Terry Valcourt|(E-Mail Removed)
> Dan Perterson|(E-Mail Removed)
> James Smiley|(E-Mail Removed)
>
> These are names and email addresses separated by a "|" symbol. Here is the
> delete script I have so for that is not functional:
>
> #############
>
> #!/usr/bin/perl
>
> use Fcntl qw(EFAULT :flock);
> use CGI::Carp qw(fatalsToBrowser);
>
> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
> @pairs = split(/&/, $buffer);
> foreach $pair (@pairs) {
> ($name, $value) = split(/=/, $pair);
> $value =~ tr/+/ /;
> $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> $form{$name} = $value;
> }
>
> open (ADDRESSES, "<addresses.txt") or die "Can't open file: $!";


If you are trying to remove a recipient from the stored list, try this
here instead (filter as you read them):

while (<ADDRESSES>) {
chomp;
push @entrys,$_ if $_ ne $form{'recipient'};
}
close ADDRESSES;
# add code to print them back to addresses.txt
print "Content-type: text/plain\n\n";
foreach (@entrys) { print "$_\n"; }

--
David Efflandt - All spam ignored http://www.de-srv.com/
http://www.autox.chicago.il.us/ http://www.berniesfloral.net/
http://cgi-help.virtualave.net/ http://hammer.prohosting.com/~cgi-wiz/
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      09-21-2003
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Robert TV" <(E-Mail Removed)> wrote in
news:ZJ7bb.1004827$(E-Mail Removed) .ca:

> Hi ... I'm loosing much hair over this array element removal problem
> i'm having. I will describe my setup. I have a web form with a single
> text box named "recipient" ... on submit, the data is passed to my
> delete.pl script. In the same folder, there is a text file called
> "addresses.txt" This file contains the following data:
>
> Gerald Genry|(E-Mail Removed)
> Gerry Smith|(E-Mail Removed)
> Robert TV|(E-Mail Removed)
> Terry Valcourt|(E-Mail Removed)
> Dan Perterson|(E-Mail Removed)
> James Smiley|(E-Mail Removed)
>
> These are names and email addresses separated by a "|" symbol. Here is
> the delete script I have so for that is not functional:
>
> #############
>
> #!/usr/bin/perl
>
> use Fcntl qw(EFAULT :flock);
> use CGI::Carp qw(fatalsToBrowser);
>
> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
> @pairs = split(/&/, $buffer);
> foreach $pair (@pairs) {
> ($name, $value) = split(/=/, $pair);
> $value =~ tr/+/ /;
> $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> $form{$name} = $value;
> }
>
> open (ADDRESSES, "<addresses.txt") or die "Can't open file: $!";
> @entrys=<ADDRESSES>;
> close(ADDRESSES);
>
> foreach $entry (@entrys) {
> $count++;
> if ($entry eq $form{'recipient'}) {
> $count--;
> splice(@entrys, $count, 1);
> }
> }
>
> print @entrys;
> exit;
>
> #############
>
> If I type "Terry Valcourt|(E-Mail Removed)" in the web form and submit,
> nothing happens. When @entrys prints, it still contains all entires
> read from the text file. It's the same for any name and email address
> grouping entered. To match the form input to the array element, I used
> the "eq" operator. It is my belief that there is no successful
> matching because there is a "\n" at the end of each "$entry".


It is my belief that there is no successful matching because you're
trying to match the recipient to the whole line of name and | and email
address and newline. But that's only a guess, because you don't tell us
what the "recipient" field contains.

I would strongly recommend that you reorganize your program. You have
boilerplate code that parses out the CGI parameters. I'll bet you have
that same code in every one of your CGI programs. Why not move it to a
subroutine, so you don't have to copy/paste so much code everywhere?
Also, if you should need to change the code, you'll only need to change
it once in the subroutine module, rather than in every single program
file. (There are at least three bugs in the code that you posted).

Better yet, use the CGI.pm module. Why code by hand that which has been
done excellently already?

Also, I would encourage you to move your database handling code to a
module. You have a simple flat-file database model. Why not write
routines to add, update, delete, and look up records, and then just
invoke those routines everywhere you use the database, instead of writing
and debugging code to do those functions everywhere?

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP223oWPeouIeTNHoEQJe7gCbBNy05RZ8J68eNxedjWSCYD 4UhakAn0jI
H9lR+mcMVWcqdF3ymaNHIuMQ
=j0++
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
removing an element from an array laredotornado Java 5 10-26-2008 01:55 AM
Array Of Objects, Removing Element From Memory Larsenmtl Larsenmtl Ruby 3 08-07-2008 06:16 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Removing an element of an array effendi@epitome.com.sg Javascript 4 03-05-2005 05:13 PM
Difficulty with slice of referenced array Henry Law Perl Misc 8 11-14-2004 04:28 AM



Advertisments