|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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); |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
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. ![]() |
|
#4
|
||||
|
||||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > filtering out elements from an array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|