CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Array question

 



Warren Bell
Deleted

Jun 12, 2000, 8:25 AM

Post #1 of 4 (829 views)
Array question Can't Post

What I'm trying to do is search through an array and if it finds a certain thing, do a command until it finds somthing else I specify. I've been searching through the docs but can't find how to make this work.

Somthing like this:

foreach $line (@array) {
# start doing the substitution if it
# finds a line with <html> in it.
if ($line =~ /(<html>.*)/i) {
$line =~ s/$1//;
# but I need it to keep substituting
# each line for nothing until it
# finds </html>
}
}

How can I do this?

TIA


japhy
Enthusiast

Jun 12, 2000, 8:51 AM

Post #2 of 4 (829 views)
Re: Array question [In reply to] Can't Post

You should use the .. operator:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


for $line (@data) {
if ($line =~ m!<html>! .. $line =~ m!</html>!) { $line = "" }
}
</pre><HR></BLOCKQUOTE>

This can be written in other ways:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


for (@data) { $_ = "" if m!<html>! .. m!</html>! }
</pre><HR></BLOCKQUOTE>

or:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@data = map m!<html>! .. m!</html>! ? () : $_, @data;
</pre><HR></BLOCKQUOTE>

or

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


@data = grep not(m!<html>! .. m!</html>!), @data;
</pre><HR></BLOCKQUOTE>

That last two are slightly different, because they don't change the elements to "", they remove them. The grep() version is slightly faster than the map() version.

[This message has been edited by japhy (edited 06-12-2000).]


Warren Bell
Deleted

Jun 12, 2000, 8:58 AM

Post #3 of 4 (829 views)
Re: Array question [In reply to] Can't Post

Thanks for the quick response!

I had a reply in a newsgroup also that told me the same thing and it works.

Thanks again.


Cure
User

Jun 16, 2000, 6:54 PM

Post #4 of 4 (829 views)
Re: Array question [In reply to] Can't Post

Hi
@data = grep not(m!<html>! .. m!</html>!), @data;

that wont work becuase of not but ! will work
@data = grep !(m!<html>! .. m!</html>!), @data;

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives