[23315] in Perl-Users-Digest
Perl-Users Digest, Issue: 5535 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Sep 21 00:10:44 2003
Date: Sat, 20 Sep 2003 21:10:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sat, 20 Sep 2003 Volume: 10 Number: 5535
Today's topics:
Trying to change ID value in @array <ducott@hotmail.com>
Re: Trying to change ID value in @array <ducott@hotmail.com>
Re: Trying to change ID value in @array <spam@thecouch.homeip.net>
Re: Trying to change ID value in @array <cs@edu.edu>
Re: Trying to change ID value in @array <ducott@hotmail.com>
Re: Trying to change ID value in @array <kuujinbo@hotmail.com>
Re: Trying to change ID value in @array <ducott_99@yahoo.com>
What is wrong with this system "..." command? (Victor)
Re: What is wrong with this system "..." command? <noreply@gunnar.cc>
Re: What is wrong with this system "..." command? <syscjm@gwu.edu>
Re: WWW::Mechanize click() returns "Unexpected field va <bart.lateur@pandora.be>
Re: <bwalton@rochester.rr.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 20 Sep 2003 21:22:13 GMT
From: "\"Dandy\" Randy" <ducott@hotmail.com>
Subject: Trying to change ID value in @array
Message-Id: <9y3bb.996236$ro6.19506362@news2.calgary.shaw.ca>
Hi, I'm trying to replace the ID number in my @array. Each entry of
addressbook.txt has an ID number, a name and an email address separated by a
"|" in this format:
1|Robert|rob@email.com
2|Gerry|gerry@email.com
3|Frank|frank@email.com
A user simply inputs a number in a web form, then the data is passed to this
deletion script:
# Get array entries
open (ADDRESSES, "<data/addressbook.txt") or die "Can't open file: $!";
@recipients=<ADDRESSES>;
close(ADDRESSES);
# Loop to locate and delete entry based on form data
foreach $recipients(@recipients) {
($id,$name,$email)=split(/\|/,$recipients);
$count++;
if ($id eq "$form{'id'}") {
$count--;
splice(@recipients, $count, 1);
}
}
########
# Missing code here to adjust ID numbers
########
# Print the changed bata back to file
open (ADDRESSES, ">data/addressbook.txt") or die "Can't open file: $!";
print ADDRESSES @recipients;
close(ADDRESSES);
So far this script works correctly. Lets say I entered "2" into the web
form, after the script is run, addressbook.txt would now contain the
following:
1|Robert|rob@email.com
3|Frank|frank@email.com
Notice the ID numbers are 1 & 3 ... I would like to add another peice of
code to the above script that will change the ID number based on entries
remaining in the text file ... when the @array data gets saved back to the
text file, it would show as:
1|Robert|rob@email.com
2|Frank|frank@email.com
I tried this "replacement" method in the "Missing code" section but didn't
work:
foreach $recipients(@recipients) {
($id,$name,$email)=split(/\|/,$recipients);
$newid++;
$recipients =~ s/$id/$newid/g;
}
Can someone help point me in the right direction to accomplish my goal? COde
examples welcomed. Thanx everyone.
Randy
------------------------------
Date: Sat, 20 Sep 2003 21:33:47 GMT
From: "\"Dandy\" Randy" <ducott@hotmail.com>
Subject: Re: Trying to change ID value in @array
Message-Id: <%I3bb.1003118$3C2.22414220@news3.calgary.shaw.ca>
""Dandy" Randy" <ducott@hotmail.com> wrote:
> Can someone help point me in the right direction to accomplish my goal?
COde
> examples welcomed. Thanx everyone.
>
> Randy
I think I figured it out ... well it works, but I sure there was a better
way. I inserted the following into the missing code section:
foreach $recipients(@recipients) {
($id,$name,$email)=split(/\|/,$recipients);
$xxx++;
$recipients = "$xxx|$name|$email";
}
Randy
------------------------------
Date: Sat, 20 Sep 2003 17:49:27 -0400
From: Mina Naguib <spam@thecouch.homeip.net>
Subject: Re: Trying to change ID value in @array
Message-Id: <HX3bb.15948$zZ.368880@wagner.videotron.net>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
"Dandy" Randy wrote:
> Hi, I'm trying to replace the ID number in my @array. Each entry of
> addressbook.txt has an ID number, a name and an email address separated by a
> "|" in this format:
>
> 1|Robert|rob@email.com
> 2|Gerry|gerry@email.com
> 3|Frank|frank@email.com
>
> A user simply inputs a number in a web form, then the data is passed to this
> deletion script:
>
> # Get array entries
> open (ADDRESSES, "<data/addressbook.txt") or die "Can't open file: $!";
> @recipients=<ADDRESSES>;
> close(ADDRESSES);
>
> # Loop to locate and delete entry based on form data
> foreach $recipients(@recipients) {
> ($id,$name,$email)=split(/\|/,$recipients);
> $count++;
> if ($id eq "$form{'id'}") {
> $count--;
> splice(@recipients, $count, 1);
> }
> }
>
> ########
> # Missing code here to adjust ID numbers
> ########
>
> # Print the changed bata back to file
> open (ADDRESSES, ">data/addressbook.txt") or die "Can't open file: $!";
> print ADDRESSES @recipients;
> close(ADDRESSES);
>
> So far this script works correctly. Lets say I entered "2" into the web
> form, after the script is run, addressbook.txt would now contain the
> following:
>
> 1|Robert|rob@email.com
> 3|Frank|frank@email.com
>
> Notice the ID numbers are 1 & 3 ... I would like to add another peice of
> code to the above script that will change the ID number based on entries
> remaining in the text file ... when the @array data gets saved back to the
> text file, it would show as:
>
> 1|Robert|rob@email.com
> 2|Frank|frank@email.com
>
> I tried this "replacement" method in the "Missing code" section but didn't
> work:
>
> foreach $recipients(@recipients) {
> ($id,$name,$email)=split(/\|/,$recipients);
> $newid++;
> $recipients =~ s/$id/$newid/g;
> }
>
> Can someone help point me in the right direction to accomplish my goal? COde
> examples welcomed. Thanx everyone.
>
> Randy
There are several logical and technical issues with your approach.
#1. You don't seem to be doing any sanity checking on the incoming ID from the web form, this would
allow anyone who has access to reach this part of the program to delete any entry in the database.
#2. Typically, IDs assigned to a database entry should maintain uniqueness when other entries are
changed. Assume for example that in your script someone requests deletion of ID #4, then hits
refresh over and over and over. Since everyone's IDs are re-numbered after every deletion, that can
cause a malicious user to truncate your database to only 4 items !
. . and many other scenarios.
#3. Using something like $form{id} tells me that you're not using the CGI module to parse the input.
Don't do that, see perldoc CGI
Since you haven't explained the top-level use of your database I'll assume you know what you're
doing and you really want the IDs re-numbered. Here's a quick way to do it:
$filename = "data/addressbook.txt";
#
# Read data excluding item to be deleted into @addresses
#
open (FH, $filename);
while (<FH>) {
chomp;
($id, @parts) = split(/\|/);
if ($id ne $form{id}) {
push (@addresses, [@parts]);
}
}
close (FH);
#
# Write the data back into the file
#
open (FH, ">$filename");
for $index (0..$#addresses) {
print FH $index+1, "|", join("|", @{$addresses[$index]}), "\n";
}
close (FH);
Error-checking left out in above code for brevety.
And finally, if you do decide that re-numbering the IDs was a bad idea, then all of the above was
re-inventing the wheel, as what you're dealing with is called a text-file delimmited-database and
there are numerous well-tested modules on CPAN that allow you to easily and reliable modify such files.
Best of luck.
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQE/bMtqeS99pGMif6wRAozTAKCliiiPl26g1t0hQf++cKENGA7BuwCglsmd
ZeYn1Uu+moONDp4IGbl8L44=
=pPwq
-----END PGP SIGNATURE-----
------------------------------
Date: Sat, 20 Sep 2003 15:59:04 -0700
From: Chief Squawtendrawpet <cs@edu.edu>
Subject: Re: Trying to change ID value in @array
Message-Id: <3F6CDBB8.9A464608@edu.edu>
\"Dandy\" Randy wrote:
> A user simply inputs a number in a web form, then the data is passed to this
> deletion script:
Based on what you've told us, my inclination would be to use a hash instead
of an array to hold the recipients.
$recipients{ID_NUMBER} = [NAME, EMAIL]
Deleting a particular recipient becomes easy:
delete $recipients{$form{id}};
Then, when you write to the file, just sort the hash keys and use an
iterator to adjust the ID numbers [code not tested]:
$n = 1;
for my $k (sort {$a <=> $b} keys %recipients){
print ADDRESSES join('|', $n, @{$recipients{$k}}), "\n";
$n ++;
}
Chief S.
------------------------------
Date: Sat, 20 Sep 2003 23:41:20 GMT
From: "\"Dandy\" Randy" <ducott@hotmail.com>
Subject: Re: Trying to change ID value in @array
Message-Id: <AA5bb.194217$la.3860063@news1.calgary.shaw.ca>
"Mina Naguib" <spam@thecouch.homeip.net> wrote:
> And finally, if you do decide that re-numbering the IDs was a bad idea,
then all of the above was
> re-inventing the wheel, as what you're dealing with is called a text-file
delimmited-database and
> there are numerous well-tested modules on CPAN that allow you to easily
and reliable modify such files.
Mina,
Ok, good points. I have opted to not use the ID method. I am however having
a matching problem. So I have removed all of the ID numbers from the file,
leaving something like this:
Valcourt|mail@viator.com
Smith|smith@yahoo.com
Genry|jdpower@assoc.com
Genry|jdpower@assoc.com2
Now the web form as two input fields ... a name field and an email field. To
remove an entry, you must EXACTLY input the correct name and email address.
Based on that input, the delete script would remove the entry.
The following is not working and I have not located a viable solution:
#################
$inputentry = "$form{'name'}|$form{'email'}";
open (ADDRESSES, "<data/addressbook.txt") or die "Can't open file: $!";
@entrys=<ADDRESSES>;
close(ADDRESSES);
foreach $entry (@entrys) {
$remove++;
if ($entry eq $inputentry) {
$remove--;
splice(@entrys, $remove, 1);
}
}
#################
I have also tried this method, but funky things happen (see text file
entires above) ... if I type "Genry" as the name and jdpower@assoc.com2 as
the email, it removes the jdpower@assoc.com entry instead, and leaves the
jdpower@assoc.com2 entry intact:
#################
$inputentry = "$form{'name'}|$form{'email'}";
open (ADDRESSES, "<data/addressbook.txt") or die "Can't open file: $!";
@entrys=<ADDRESSES>;
close(ADDRESSES);
foreach $entry (@entrys) {
$remove++;
if ($entry =~ /$inputentry/) {
$remove--;
splice(@entrys, $remove, 1);
}
}
#################
I am at a loss. I have tried several matching operators like =~ , eq , ne ,
= , !~ , non of which are matching the $entry to the $inputentry values.
Where am I going wrong? TIA!!!!
Randy
------------------------------
Date: Sun, 21 Sep 2003 10:33:58 +0900
From: ko <kuujinbo@hotmail.com>
Subject: Re: Trying to change ID value in @array
Message-Id: <bkiv92$t17$1@pin3.tky.plala.or.jp>
"Dandy" Randy wrote:
> Mina,
>
> Ok, good points. I have opted to not use the ID method. I am however having
> a matching problem. So I have removed all of the ID numbers from the file,
> leaving something like this:
>
> Valcourt|mail@viator.com
> Smith|smith@yahoo.com
> Genry|jdpower@assoc.com
> Genry|jdpower@assoc.com2
>
> Now the web form as two input fields ... a name field and an email field. To
> remove an entry, you must EXACTLY input the correct name and email address.
> Based on that input, the delete script would remove the entry.
[snip]
> $inputentry = "$form{'name'}|$form{'email'}";
Since you have chosen to join the two form fields into a string, it
might be easier to lookup via a hash:
# build lookup hash
open (ADDRESSES, 'YOUR_FILE') or die $!;
my (@entrys, %entrys) = <ADDRESSES>;
chomp foreach @entrys;
@entrys{@entrys} = ();
# remove entry, $inputentry from previous
delete $entrys{$inputentry} if exists $entrys{$inputentry};
HTH - keith
------------------------------
Date: Sun, 21 Sep 2003 02:29:00 GMT
From: "Robert TV" <ducott_99@yahoo.com>
Subject: Re: Trying to change ID value in @array
Message-Id: <M18bb.998258$ro6.19533186@news2.calgary.shaw.ca>
"ko" <kuujinbo@hotmail.com> wrote
> Since you have chosen to join the two form fields into a string, it
> might be easier to lookup via a hash:
>
> # build lookup hash
> open (ADDRESSES, 'YOUR_FILE') or die $!;
> my (@entrys, %entrys) = <ADDRESSES>;
> chomp foreach @entrys;
> @entrys{@entrys} = ();
>
> # remove entry, $inputentry from previous
> delete $entrys{$inputentry} if exists $entrys{$inputentry};
Hi HTH, I have tried using your method as described above. In all instances,
all elements of the array except the first line get deleted instead of the
single target element as chosen by $inputentry. Here is the exact code being
used:
open (ADDRESSES, "<data/addressbook.txt") or die "Can't open file: $!";
my (@entrys, %entrys) = <ADDRESSES>;
close(ADDRESSES);
$inputentry = $form{'recipient'};
chomp foreach @entrys;
@entrys{@entrys} = ();
delete $entrys{$inputentry} if exists $entrys{$inputentry};
open (ADDRESSES, ">data/addressbook.txt") or die "Can't open file: $!";
flock (ADDRESSES, LOCK_EX) or die "Can't lock file: $!";
print ADDRESSES @entrys;
close(ADDRESSES);
Also if I replace the value of $inputentry with a definative value that is
found in the text file such as:
$inputentry = "Smith|smith@yahoo.com";
The result is the same ... all lines in the array are removed except the
first. Didi i overlook something? TIA!
RTV
------------------------------
Date: 20 Sep 2003 13:04:30 -0700
From: gvictor97@yahoo.com (Victor)
Subject: What is wrong with this system "..." command?
Message-Id: <ab759f.0309201204.8638aa0@posting.google.com>
I am getting error with following code when run unix:
!system "ls >& log" or die "cant run";
error:
sh: log: bad number
cant run at test.pl line 3.
Though it works on Linux!
Thanks,
--V
------------------------------
Date: Sat, 20 Sep 2003 22:07:00 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: What is wrong with this system "..." command?
Message-Id: <bkic6g$24j02$1@ID-184292.news.uni-berlin.de>
Victor wrote:
> I am getting error with following code when run unix:
>
> !system "ls >& log" or die "cant run";
>
> error:
> sh: log: bad number
> cant run at test.pl line 3.
>
> Though it works on Linux!
comp.infosystems.www.servers.unix
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sat, 20 Sep 2003 16:13:16 -0400
From: Chris Mattern <syscjm@gwu.edu>
Subject: Re: What is wrong with this system "..." command?
Message-Id: <3F6CB4DC.1000005@gwu.edu>
Victor wrote:
> I am getting error with following code when run unix:
>
> !system "ls >& log" or die "cant run";
>
> error:
> sh: log: bad number
> cant run at test.pl line 3.
>
> Though it works on Linux!
>
As the error message indicates, system is trying to parse
your command line with sh, so your csh syntax is not working.
Try "ls >log 2>&1"
Chris Mattern
------------------------------
Date: Sat, 20 Sep 2003 19:53:31 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: WWW::Mechanize click() returns "Unexpected field value"
Message-Id: <20cpmvgc2esnpcsp502i262v62mhmfmod4@4ax.com>
Timur Tabi wrote:
>A friend of mine wrote a Perl script that works fine on his machine
>but dies on mine. I know very little about Perl, so I need help in
>determining the problem.
Are you behind a fierwall? Do you need to use a proxy? If so, set the
HTTP_PROXY environment variable, format
http://proxy.pandora.be:8080
--
Bart.
------------------------------
Date: Sat, 19 Jul 2003 01:59:56 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re:
Message-Id: <3F18A600.3040306@rochester.rr.com>
Ron wrote:
> Tried this code get a server 500 error.
>
> Anyone know what's wrong with it?
>
> if $DayName eq "Select a Day" or $RouteName eq "Select A Route") {
(---^
> dienice("Please use the back button on your browser to fill out the Day
> & Route fields.");
> }
...
> Ron
...
--
Bob Walton
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 5535
***************************************