I have a script that reads a text file of items and puts them in a pipe delimted format...
example:
----text file---
bob
sue
john
----text file---
will come out bob|sue|john
The problem I have is if someone adds blank spaces or lines:
----text file---
bob
sue
john
----text file---
(then you get bob|sue|john|)
I've tried using substitute (s/^$//g; and s/^\n//g;) to remove blank lines but can't quite seem to get it right (although this is what I would prefer).
So I came up with another idea - what about using pop and shift in this manner:
----text file---
########Add Names BETWEEN These Lines - NO BLANK LINES ########
bob
sue
john
########Add Names BETWEEN These Lines - NO BLANK LINES ########
----text file---
Then doing pop(@text); and shift(@text) to get rid of the comment lines. Is this a good idea?
Ps: Here is how I read the text file and do what I need to do with it
open(FILTER, "<spam.filter") or die "no such file or invalid path\n";
@check = <FILTER>;
$filter = join "",@check;
$filter =~ s/\n/|/g;
$filter =~ s/([^\w\s.|])/\\$1/g;
close(FILTER);
the reason for doing this is to create a variable called $filter that contains bob|sue|john and then stick that variable here:
foreach ($Body =~ /($filter)/) { blah
This makes the script more "user friendly" so that co-workers can simply modify a text file of names instead of having to worry about pipes and escaping special characters, etc.
Is using: foreach ($Body =~ /($filter)/) a good way of doing this? Would some other method be better/faster/more stable?
I'm doing this is as a learning project so please feel free to point out any better examples of code (as long as you explain why).
example:
----text file---
bob
sue
john
----text file---
will come out bob|sue|john
The problem I have is if someone adds blank spaces or lines:
----text file---
bob
sue
john
----text file---
(then you get bob|sue|john|)
I've tried using substitute (s/^$//g; and s/^\n//g;) to remove blank lines but can't quite seem to get it right (although this is what I would prefer).
So I came up with another idea - what about using pop and shift in this manner:
----text file---
########Add Names BETWEEN These Lines - NO BLANK LINES ########
bob
sue
john
########Add Names BETWEEN These Lines - NO BLANK LINES ########
----text file---
Then doing pop(@text); and shift(@text) to get rid of the comment lines. Is this a good idea?
Ps: Here is how I read the text file and do what I need to do with it
open(FILTER, "<spam.filter") or die "no such file or invalid path\n";
@check = <FILTER>;
$filter = join "",@check;
$filter =~ s/\n/|/g;
$filter =~ s/([^\w\s.|])/\\$1/g;
close(FILTER);
the reason for doing this is to create a variable called $filter that contains bob|sue|john and then stick that variable here:
foreach ($Body =~ /($filter)/) { blah
This makes the script more "user friendly" so that co-workers can simply modify a text file of names instead of having to worry about pipes and escaping special characters, etc.
Is using: foreach ($Body =~ /($filter)/) a good way of doing this? Would some other method be better/faster/more stable?
I'm doing this is as a learning project so please feel free to point out any better examples of code (as long as you explain why).