Gossamer Forum
Home : General : Perl Programming :

Question about pop and shift

Quote Reply
Question about pop and shift
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).
Quote Reply
Re: [Watts] Question about pop and shift In reply to
Why not just make sure you aren't reading in the blank lines?

Instead of slurping the whole file into the array, use a while loop to iterate through the open file. Verify the line doesn't start with a whitespace or # (or whatever else you want to ignore), and if all is well, push that ine into the array.

The difference in speed and memory is likely negligable between the two techniques.
Quote Reply
Re: [Mark Badolato] Question about pop and shift In reply to
Much appreciated. I've seen something like that in db.cgi - using 'next'.

Thanks once again. Smile
Quote Reply
Re: [Watts] Question about pop and shift In reply to
Yeah, the following should work ok;

Code:
open(FILTER, "<spam.filter") or die "no such file or invalid path\n";

while (<FILTER>) {
if ($_ =~ /^(\n|#| )/) { next; }

# do whatever else you want to do here,...

} # end while
close(FILTER);

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Question about pop and shift In reply to
FYI, you can compact

if ($_ =~ /^(\n|#| )/) { next; }

a little bit to

next if /^(\n|#| )/;

no need to specify the $_ as it's implied. Just looks a little neater (to me at least)