Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 6th, 2001, 12:25 PM
kumars kumars is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Posts: 9 kumars User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Red face filtering out elements from an array

I think I bumped into a pretty complex problem. Appreciate Perl experts can give me a hand:

I have 2 arrays. @input has lines of text from a document (.html) and @filterin has words, phrases I want to filterout from the html document.

Here is my code which is not producing the result I want: I know I can write it with a complex " if ((condition1) && (condition2) && (condition...n)) " statement, but I want the code to be more modular and easy to update.

The problem withthis code is it produce multiple duplicate of the same line (eqaul to the number of elements in the filerin array and the word "")

open FH, "result4.html";
@input = <FH>;
close FH;
@filterin = ("Click Here", "Sponsored by", "blabla", "bllaablla", "blaabla", "blabbla", "blaablaa");
$FT=0;
foreach $line (@input)
{
foreach $filter (@filterin)
{
if( $line !~ /$filter/)
{
$line0 = $line0.$line;

}
}

}


open(OUTPUT, ">result4a.html");
print OUTPUT $line0;
close (OUTPUT);

Reply With Quote
  #2  
Old August 6th, 2001, 01:09 PM
jdk's Avatar
jdk jdk is offline
phpkid ~~~~~~ :o)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2000
Location: NJ, USA
Posts: 2,535 jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 11 m 11 sec
Reputation Power: 10
Send a message via Yahoo to jdk
ok..can i suggest u some way ??

make a function, which takes two arguments. mainly 2 strings.

first is the string to search in for, and to search 2nd string.

i mean something liek

sub string_exists
{
my($string_search_in,$string_search_for) = $_;

$result = $string_search_in =~ /$string_search_for/;

if($result)
{
return 1;
}
else
{
return 0;
}
}

now in ur foreach block call this function to check if the string contains the filtering word

like

foreach $line(@input)
{
$check = 1;// just a variable which defines if we want to write the line.
foreach $filter_word($filterin)
{
if( string_exists($line,$filter_word) )
{
$check = 0;// setting it to zero means that we dont want to write the line.
}
}
if($check) //write only if it doesnt have word to be filtered.
{
$line_to_write .= $line;
}
}


$line_to_write is the filtered string.

i havent checked the code, but i think u get some idea,
hope it helps,
jd
__________________
_____________________________
d.k.jariwala (JD)
~ simple thought, simple act ~
I blog @ http://jdk.phpkid.org

Reply With Quote
  #3  
Old August 6th, 2001, 02:46 PM
kumars kumars is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Posts: 9 kumars User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Cool Great! it worked

Appreciate your extremely quick response to my question. I was able to achieve what I was aiming for. I think there is only one typo i replace the line
my($string_search_in,$string_search_for) = $_;
with
my($string_search_in,$string_search_for) = @_;
to make it work.

Thanks once again.
I love this site.


Reply With Quote
  #4  
Old August 7th, 2001, 12:45 AM
jdk's Avatar
jdk jdk is offline
phpkid ~~~~~~ :o)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Nov 2000
Location: NJ, USA
Posts: 2,535 jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level)jdk User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 11 m 11 sec
Reputation Power: 10
Send a message via Yahoo to jdk
hi kumars,

actually lately i have been developing more on php than perl but believe me i still luv perl as much as i was used to.just that u know programming syntax gets messed up,

all i was trying is to give u was a idea, and its great that u got the solution,
jd

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > filtering out elements from an array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT