PDA

View Full Version : Here's a humdinger


Jacob Stetser
02-16-1999, 11:21 PM
I have a script. A friendly PERL script.

It takes in a URL and an email address. I want to write the email addresses to a file, and I want it to weed out duplicate instances of the same email address and write each new email address on a new line.

How do I do it?

Jake

dean
02-16-1999, 11:48 PM
Solution Scripts has just the program for you. Beside maintaining the list, weeding out duplicates (not allowing duplicates) it also has a password protected newletter mailing form.

I use it I love it
Dean

http://216.89.190.203/vault/index.shtml

demo here:
http://216.89.190.203/vault/powerlist/mail.cgi

good luck,
Dean

Jacob Stetser
02-17-1999, 12:09 AM
Thanks...

actually, I have a script. I want to add in a subroutine that will take a certain form variable (email) and slip it in there..

I found a snippet of code that I think might work...

open(LIST, ">>maillist.txt");
print LIST "$email\n";
close(LIST);

but now I need to figure out file locking so my list doesn't get corrupted. Any ideas?

Terra
02-17-1999, 12:14 AM
Hello Jacob, time for me to go into geek mode...

Heres a snippet that was written for the EMail_MGR CNC module...

It takes an array and can return any/all of Union/Intersection/Difference of 2 arrays...

Just a little bit of Perl deep magic... http://www.aota.net/ubb/wink.gif

@one = qw(1 2 3 4 5);
@two = qw(4 5 6 7 8);
my (@union, @intersection, @difference) = ();
my %count = ();

foreach my $element (@one, @two) {
$count{$element}++;
}
foreach my $element (keys %count) {
push @union, $element;
push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;
}

print "Union:\n", map { "$_\n" } @union;
print "\n";
print "Intersection:\n", map { "$_\n" } @intersection;
print "\n";
print "Difference:\n", map { "$_\n" } @difference;

Whoooooo...

--
Terra
--Perl = 99 different complex cool ways, to do 1 simple thing--
FutureQuest

[This message has been edited by ccTech (edited 02-17-99).]

Terra
02-17-1999, 12:19 AM
You will want to use 'flock' call...

I use it quite often, and the Linux kernel is quite robust with it's advisory file locking...

--
Terra
--Beware of seagulls overhead--
FutureQuest

Jacob Stetser
02-17-1999, 11:52 AM
*blink blink*

Andrew, you are far beyond mere mortals.

I have no idea what that CNC module stuff means http://www.aota.net/ubb/smile.gif

Anyhow, I think you're trying to tell me how to weed out duplicates.

http://www.aota.net/ubb/smile.gif

Hooboy..

Jacob Stetser
02-17-1999, 01:31 PM
I did it!

This is what I eventually used.... feel free to add to any scripts you need to add mail logging too

########### Add email to log file

$file = "addressbook.txt";
$htmlurl = "http://url.to.success.page/";
#replace $FORM('email') with the variable in your script that contains the user's email.

open(LIST,"$file");
if ($lock){
flock(LIST, $lock);
}
@addresses=<LIST>;
close(LIST);

foreach $member(@addresses)
{
chomp($member);
if ($FORM{'email'} eq $member){&already_subscribed}
}

push (@addresses,$FORM{'email'});
open(LIST,">>$file");
if ($lock){
flock(LIST, $lock);
}
print LIST "$FORM{'email'}\n";
close(LIST);


sub already_subscribed
{
print "Pragma: no-cache\n";
print "Location: $htmlurl\n\n";

exit;
}

[This message has been edited by Jacob Stetser (edited 02-17-99).]

hearts
02-17-1999, 02:26 PM
i have a couple of questions, just out of idle curiousity.

what does "chomp" and "flock" mean?

I just install prewrote scripts, but I am reading this one. Does this script eat and gather info?

I am really not meaning to create humor about this script or any, I would really like to understand what they mean.

and how come there are $ in front of some of the commands?

----

hearts

Justin
02-17-1999, 03:32 PM
As with most languages, a $ in front of a word makes it a string variable. An & in front tells it that you are refering to a sub or function. An @ makes it an array variable.

So $Message = "Hello, world"; assignes Hello, world to the variable $Message. So if you typed:

print $Message;

it would print Hello, world.

flock is file lock. It keeps the script from trying to write to the same file twice at the same time, like if two people post at the exact same moment. It locks the file.

I don't know what chomp means - I should probably go check that out... I know what split means, though. If you said @Query = split(env{'QUERY_STRING'},"\&"); or something like that, I think, it would take the query string (action=intro&name=Justin&password=hehe) and split it into an array, using the & as the delimiter. So @Query(1) becomes action=intro and so on.

I'm not sure if I got that all right, but it's something like that. You could then further split each element of the array using the "=" to separate a name from a value. That's how the UBB does it.

I'm probably not very accurate on most of the actual syntax, so don't quote me. I just use my favorite programming technique - trial and error http://www.aota.net/ubb/smile.gif

I hope that clears it up a wee little bit. Oh, and the "\" goes in front of special charactors so that they don't confuse the interpreter. So print "Justin\@VDJ.Net"; would output Justin@VDJ.Net.


------------------
Justin Nelson, SFE Inc.
www.vdj.net (http://www.vdj.net)

[This message has been edited by Justin (edited 02-17-99).]

Jacob Stetser
02-17-1999, 04:24 PM
Hearts, that's priceless.. I laughed for quite a while.. especially when I looked back at my scriptamabob and saw the line

chomp($member);

I guess it eats my members http://www.aota.net/ubb/smile.gif

I know you weren't trying to be funny, but in a way, that's what it does. I think chomp takes a bite out of the data (a chunk, equal in this case to an email address), passes it onto the next line, which checks if that chunk matches one that's already in there. If so, it spits it out and ends the script.

I kinda fudged this. I took two scripts... my original and one that made email lists through a Subscribe box and I rewrote the list-making subroutine to work for my original script...

Plus a little dab of style here and there. http://www.aota.net/ubb/smile.gif

Jake

hearts
02-17-1999, 04:45 PM
Wellll.. to be honest, I was laughing too, but I really meant my question. *sometimes I just laugh at myself*

I have a question, if you don't mind, Jake...
can I see the whole script? Cuz I got a fever to learn this stuff. Seems I kinda adapt to it for some reason and would like to understand how this stuff really works.

I don't know if it is outta line to ask something like that. But, if it is okay to.. can ya zip it and send it to me? If it is outta line, then I apologize.

-----

hearts

Jacob Stetser
02-17-1999, 04:53 PM
Not at all.. It's an FFA link script from Link-o-Matic and I modified it to save the emails and subscribe them to my mailing list.

I'll send it to you via email later this evening, as I'm heading home right now!

Later!

Jake

Del
02-18-1999, 01:45 AM
Hi all. Thought I'd share here. Actually, chomp(@array); is quite a handy little function. What it does is eat all the trailing \n (newline) characters of whatever's in the array. For example, an address file is stored as follows;

addy1\n
addy2\n
addy3\n
etc...\n


We read that file in, and assign it to the @addies array, so $addies['0'] holds the value of 'addy1\n', and so on. Now, if we say chomp(@addies), $addies['0'] now holds the value of 'addy1' (notice, no more newline character). This is very useful in that 'bob' is not the same as 'bob\n', so by chomping the array bob lives in, the newline is removed, and pattern matching lines in a file works right.

Del

D'oh!

[This message has been edited by Del (edited 02-18-99).]