Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a file and storing contents as array.

Status
Not open for further replies.

ErrolDC2

MIS
Apr 6, 2005
43
US
First and foremost, I'm a novice.
Just want to make that clear because some of what I may ask may sound stupid but I'm trying to understand here so any help would be appreciated.

I'm working on extending a function in MailScanner ( that will allow me to implement priority service levels on my gateways. So looking through the MailScanner code, I found some examples and managed to write some functions that look like they should work. What it is supposed to do is to read a directory, store all the files in that directory as an array then open up each file and store the values as another array that I can use later to make queuing decisions on later. The direcotry will contain at least two files. One file will be named "default". It will contain a single column file of all domains that are supposed to get priority treatment. All other files will be domain related files where individual email addresss can get priority treament.

So I don't fill the post with text, I'll only post snippets of code where I am a bit confused or need further explanation. Google is hard to search for stuff like "perl -> array" :)

Code:
my($dirname, $prioritydefaultlist) = @_;
my($dir, $filename, $fh, $domains, $lines);
$dir = new DirHandle;
$dir->open($dirname) or return 0;
$lines = 0;
$domains = 0;
 while ($filename = $dir->read()) {
  next if $filename =~ /^\./;

Just for clarification, the last line in that says "read any file" right?

Code:
$fh = new FileHandle;
$fh->open("$filename");
$filename = lc($filename);
 while(<$fh>) {
  chomp;
  s/#.*$//; # Strip comments
  s/\S*:\S*//g; # Strip any words with ":" in them
  s/^\s+//g; # Strip leading whitespace
  s/^(\S+)\s.*$/$1/; # Use only the 1st word
  s/^\*\@//; # Strip any leading "*@" they might have put in
  s/^\s*\@//; #No usernames allowed here
  next if /^$/; # Strip blank lines
  $lines++;
  $prioritydefaultlist->{$filename}{lc($_)} = 1;
 }

Now, this is where i'm confused a bit. I think this is where the array gets stored as "$prioritydefaultlist". Is this the case? And what does the last line do? And what does the '1' signify? Furthermore I actually need to process two files; the default file and the other domain files. But I'm not sure how I go about doing that. I want to read the directory then first, process the default file. After that, I wan to then process all other files. How do I do this?

TIA

Errol
 
Code:
next if $filename =~ /^\./;

Don't process this file. It starts with a "." Skip to the next file.

Code:
while(<$fh>) {
Read in the contents of the file - line by line

Code:
$prioritydefaultlist->{$filename}{lc($_)} = 1;
Store the filename in an array. Store the lines of that file in the array and make sure they are unique to that file. The filename and its lines are both stored as keys. Keys are not saved in a hash unless they have a defined value. Set a "1" so that the keys stick around.

When pulling values back out of hashes, the results are in no particular order. You can specify a sort operation on the hash. If your default file has a character that causes it to come up first in the sort, you could process it and then the rest.

Hth,

Raklet




 
Excellent!

Okay. Now, I have two functions. One of the functions needs to only find the file named default. The other needs to process every other file. How do I do that?


Also, would it make any difference if the the '1' were a '2'? What would happen if I set another array to use '1'?

Now matching against that array. This is what I have so far:

Code:
sub LookupQueue {
  my($message, $prioritydefaultlist, $prioritydomainlist) = @_;
	
  return '/var/spool/mqueue' unless $message;
  my($todomain, @todomain, $to, @to, $isspam);
  @todomain = @{$message->{todomain}};
  $todomain = $todomain[0];
  @to	    = @{$message->{to}};
  $to	    = $to[0];
  $isspam   = $message->{isspam};
   
  return '/var/spool/mqueue.priority' if $prioritydefaultlist->{$todomain};
  return '/var/spool/mqueue.priority' if $prioritydomainlist->{$to};
  return '/var/spool/mqueue.spam' if $isspam;
  return '/var/spool/mqueue';
}

But this doesn't work. Any idea?


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top