Scanning a file and taking the info

They have: 314 posts

Joined: Nov 1999

Hello

I have a perl question. It isn't a problem... yet ... but I am stumped as to how i would make a script.

I want to make a script to scan one file, line by line, for the same input. Each line looks like:

minton:bob:[email protected]:Thomas:Minton:::::::::::::::::::::::::0

the first part is the username, second is the password and the third, the bit i want, is the email address. In this one file there are all our customers info displayed like the above.

I am going to make a simple mailinglist to email all of our customers (using account manager lite they took ourt the mass mail customers feature so i have to make my own! Laughing out loud).

i want to create a scanner to scan the member info file for their email addressses but i dont know how to make the script take in the email and the email only.

I think it has something to do with putting ech line in an array and then calling it by the array name and a [3] at the end but that is ony what I think i have seen in other scripts so is most likely wrong. Anyhows, to cut a long (very) story short, how do I call in one piece of info from each line?

Cheers

They have: 193 posts

Joined: Feb 2000

Hi Thomas,

You have the write idea. Here's some code for you se see that does what you want:

open(FILE, "<$file") || die "Can't open $file: $!\n";
@array = <FILE>; # I'm now assigning the data of the file to an array so we can loop through it.
close(FILE);

foreach $lines (@array) {
chomp($lines); #this will removed the \n at the end of the lines
@info = split(/:/, $lines); #this now splits the line up by :
print $info[2];
}
'

In order to get data, use $info[number] Make sure to place number with the appropriete one to the data you want (make sence?). Also, remember that the first item, is labeled 0, not 1.

Because you're only reading the data, you shouldn't need to flock the file.

Another thing to remember is when writing to this "database," make sure the user does NOT enter a :, otherwise your data will get out of order when retreiving it. I beleive this is one mistake the UBB had (I think it's fixed now) that allowed people to make themselves admins.

Hope that helped! Post any problems you mgith get. Smiling

[edit]I need to get the code tag right.[/edit]

Richard

[email protected]

Everyone here has a website. It's just that not all are worth posting (Mine! Smiling).

They have: 314 posts

Joined: Nov 1999

hey Richard,

I will try your code and report back,

what I will do will be to take the email addresses and create a new file and put all of them in, I shuld be able to do that without too many problems though Wink

Thanks alot for your help

They have: 193 posts

Joined: Feb 2000

Just curious...but why are you "exporting" the emails? Do you have another program like Pegasus to send out the mail?

Just a tip: make the script send out the email for you. Wink

Richard

[email protected]

Everyone here has a website. It's just that not all are worth posting (Mine! Smiling).

They have: 314 posts

Joined: Nov 1999

no, I want to email all of our customers. we are using account manager lite. I dont know if you are familiar with it but it is a great script. it is though, unfortunately, expensive to buy the full version and so some of the features in the lite version have gone, namely the email customers feature.

So unless I want to individually email all the users, i need a way to email all the people at once. and here enters

*drum role*

my script Smiling

All the customer is stored in one file with each customers info being stored in the format i showed earlier. I will use your scanner (albeit a supped up version) and scan the customer file for all the emails. I will then write them to a email file so in the end i will have a file seperate from the customer file with all their email's.

i can then use this file in conjunction with my not yet existing mail script...

simple Laughing out loud

They have: 193 posts

Joined: Feb 2000

Ok. Smiling

BTW, I have had experience with account manager lite in the past.

I hope it all works out for you. Wink

Richard

They have: 314 posts

Joined: Nov 1999

hi again,

I have just got around to trying it all out and have come to this hitch.

it seems to work but when i look at the mememails.txt file it is empty.

the code

#!/usr/bin/perl
############################################
# Scan contents of Account Manager Lite
# members detals file and put in a seperate
# file named "mememails.txt"
############################################
print "Content-type: text/html\n\n";
print "Completed\n";

open(FILE, "</WebSites/hosted/dotcomper.co.uk/cgi-bin/testscan/amdata.db") || die "Can't open $file: $!\n";
@array = <FILE>; # I'm now assigning the data of the file to an array so we can loop through it.
close(FILE);

foreach $lines (@array) {
chomp($lines); #this will removed the \n at the end of the lines
@info = split(/:/, $lines); #this now splits the line up by :
&add;
}
#############################################

sub add {
open (LIST, ">>/WebSites/hosted/dotcomper.co.uk/cgi-bin/testscan/mememails.txt") || die "Can't open"; {
print "\n$info[2]\n";
close (LIST)
}
}
'

Any help would be great.

They have: 193 posts

Joined: Feb 2000

Who programmed that? I see two errors in it...

Also, remember that *nix is case sensitive, so check the file names.

I see the error, when printing to the "file," you have not specified a handle...so it would be printing out to the screen. You need to use print LIST "";

Here's the file with any problems I see fixed:

#!/usr/bin/perl
############################################
# Scan contents of Account Manager Lite
# members detals file and put in a seperate
# file named "mememails.txt"
############################################
print "Content-type: text/html\n\n";
print "Completed\n"; # <strong>This should be at the end of the program, sometimes Perl won't do something right and will stop, but you really don't know when your "confirmation" print is at the beginning.</strong>

open(FILE, "</WebSites/hosted/dotcomper.co.uk/cgi-bin/testscan/amdata.db") || die "Can't open $file: $!\n"; # <strong>Check this for case sensitivity.</strong>
@array = <FILE>; # I'm now assigning the data of the file to an array so we can loop through it.
close(FILE);

foreach $lines (@array) {
chomp($lines); #this will removed the \n at the end of the lines
@info = split(/:/, $lines); #this now splits the line up by :
&add;
}
#############################################

sub add {
open (LIST, ">>/WebSites/hosted/dotcomper.co.uk/cgi-bin/testscan/mememails.txt") || die "Can't open"; # <strong>Check for case sensitivity</strong>
{ # <strong>Ommit this</strong>
print "\n$info[2]\n"; # <strong>The first \n will cause this to print every other line, also, you need to print to LIST</strong>
close (LIST) # <strong>Where's the ; ?</strong>
} # <strong>Ommit this</strong>
}
'

Another thing is that the above code opens and closes the file being written to for evey line in the old file...so say you have 400 lines, then that program will open and closee the new file 400 times.

Suggested code:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

open(FILE, "</WebSites/hosted/dotcomper.co.uk/cgi-bin/testscan/amdata.db") || die "Can't open handle FILE: $!\n";
@array = <FILE>;
close(FILE);

open (LIST, ">>/WebSites/hosted/dotcomper.co.uk/cgi-bin/testscan/mememails.txt") || die "Can't open handle LIST: $!\n";

foreach $lines (@array) {
chomp($lines);
@info = split(/:/, $lines);
print LIST "$info[2]\n";
}

close (LIST);

print "done";
'

Hope that helped.

Richard
richjb::401

[email protected]

Everyone here has a website. It's just that not all are worth posting (Mine! Smiling).

They have: 314 posts

Joined: Nov 1999

OK, I will use your version,

Thanks alot Smiling

They have: 193 posts

Joined: Feb 2000

Glad to be of help. Smiling

Richard
richjb::406

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Do you want to append the file each time? If you do that you could get duplicates. If you append to the file each time, you should make a routine that eliminates duplicates.

I would just re-write the file each time.
open (LIST, ">/WebSites/hosted/dotcomper.co.uk/cgi-bin/testscan/mememails.txt") || die "Can't open handle LIST: $!\n";
Just one > in the open()

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 314 posts

Joined: Nov 1999

I am looking at how to get rid of duplications but I will have to play around wityh it all when I get a chance and find out the best way of doing things.

Thanks

[edit] Just tried the script and after some tiny tweaking it works fine Wahay:D
thanks[/edit]

[Edited by minton on 08-17-2000 at 09:26 AM]

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.