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: Beginner:
Searching for specific text

 



mohan
User

Oct 18, 2015, 11:05 PM

Post #1 of 12 (2326 views)
Searching for specific text Can't Post

Hi Guys,

Need your help with a few modifications in an existing code. I got this code with the help of this forum and would need help in editing this a little bit.

This code reads the .opf and .css files in the directory and throws the result if the conditions are met.

The code is :


Code
use strict;  
use warnings;
use Cwd;

my @filedefs =
(
{
extension => 'css',
preaction => sub { $_[0]->{count} = 0 },
testaction => sub { $_[0]->{count}++ },
postaction => sub { print "$_[0]->{label} !! $_[0]->{count} Instance found\n" },
operations =>
[
{
label => 'Harcoded font size value',
test => sub { $_[0] =~ /font-size\s*(:|=)\s*[^%]+$/ },
},
{
label => 'Forced Line height value',
test => sub { $_[0] =~ /line-height/ },
},
{
label => 'position absolute value',
test => sub { $_[0] =~ /position\s*(:|=)\s*absolute/ },
},
{
label => 'Forced font color',
test => sub { $_[0] =~ /^\s*color\s*(:|=)/ },
},
],
},
{
extension => 'opf',
operations =>
[
{
label => 'language',
test => sub { $_[0] =~ /language/ },
testaction => sub { print "$_[1]\n" },
},
{
label => 'layout or fixed get',
test => sub { $_[0] =~ /(pre-paginated|\Qname="fixed-layout" content="true"\E)/ },
testaction => sub { $_[0]->{done} = 1 },
postaction => sub { print +( $_[0]->{done} ) ? 'Fixed layout tag is found' : 'Fixed layout tag is not found', "\n" },
},
],
}
);

my $dirpath = getcwd;

for my $filedef ( @filedefs )
{
my @filepaths = glob( "$dirpath/*.$filedef->{extension}" );

print "\nno $filedef->{extension} files\n" unless @filepaths;

for my $filepath ( @filepaths )
{
print "\n>>> $filepath <<<\n";

do { $_->{done} = 0; $_->{preaction} // $filedef->{preaction} // sub { } }->( $_ ) for ( @{$filedef->{operations}} );

open my $filehandle, '<', $filepath or die "could not open '$filepath': $!";

while ( my $line = <$filehandle> )
{
# note the below two lines could be handled more efficiently in a testaction, which
# returns a true or false value to indicate if last etc, because its unnecessary to check
# for every line.
my @operations = grep { not $_->{done} } @{$filedef->{operations}};
last unless @operations;

chomp $line;

do { $_->{test} // sub { 0 } }->( $line ) and do { $_->{testaction} // $filedef->{testaction} // sub { } }->( $_, $line ) for ( @operations )
}

close $filehandle;

do { $_->{postaction} // $filedef->{postaction} // sub { } }->( $_ ) for ( @{$filedef->{operations}} );
}
}

END
{
print "\nPress enter to exit\n";
<>;
}



I want to make the below modifications:

1. Currently, the css and opf file should be place in the directory for the search to happen. If these files are inside the subdirectory then it wont detect these files. I want to modify in such a way that the program can search for files inside the sub directoriesas well.

2. It should read".html" files and should look out for the usage of "svg" tags in it. If found it should dispay that "svg tag has been found in xyz.html"

3. It should read opf for the text "cover". if its found then display cover is found.


mohan
User

Oct 20, 2015, 1:15 AM

Post #2 of 12 (2287 views)
Re: [mohan] Searching for specific text [In reply to] Can't Post

Any leads guys?


FishMonger
Veteran / Moderator

Oct 20, 2015, 6:24 AM

Post #3 of 12 (2276 views)
Re: [mohan] Searching for specific text [In reply to] Can't Post

You forgot to ask a question; you just stated what you wanted to achieve.

My suggestion is for you to attempt to make the changes you specified and when you get stuck, post the updated code as well as all errors/warnings it generates. And, don't forget to include a specific question regarding the section of code that is giving you trouble.


Laurent_R
Veteran / Moderator

Oct 20, 2015, 8:29 AM

Post #4 of 12 (2269 views)
Re: [mohan] Searching for specific text [In reply to] Can't Post

I agree with FishMonger that you should show the code that you attempted.

On the one hand because it would show your efforts at solving the problem (other than asking here), and also, in the other hand, because it might clarify your requirement, which is not clear to me at this point.

Item 1: do you want to search the whole subdirectory tree of your current directory, the whole directory tree of your disk or file system? Or just one specific subdirectory?

Items 2 and 3: are you looking for an "svg" tag anywhere in your file, and, if found, just report that and exit? Same question with the occurrence of the word "cover"? Or should the program continue doing what it currently does, and look for both the "svg" tag and the cover keyword? These are just the few questions that comes to my mind immediately, I am sure I would have more if I were getting into the details of trying to satisfy your request.

In short, please be more specific with what you need. It will certainly also help you defining a possible solution.


mohan
User

Oct 20, 2015, 9:17 AM

Post #5 of 12 (2262 views)
Re: [Laurent_R] Searching for specific text [In reply to] Can't Post

@FishMonger and Laurent_R - I will try working on the code.

And here is more clarification:

Item 1: do you want to search the whole subdirectory tree of your current directory, the whole directory tree of your disk or file system? Or just one specific subdirectory?

ANSWER------- I want to search all the subdirectory of my directory. For example: Assume there is a folder Parent folder->Child 1 folder and Child 2 folder etc etc... I run the program from Parent folder and it should search all the folders inside it.

Items 2 and 3: are you looking for an "svg" tag anywhere in your file, and, if found, just report that and exit? Same question with the occurrence of the word "cover"? Or should the program continue doing what it currently does, and look for both the "svg" tag and the cover keyword?

ANSWER-------Yes. If an svg tag is found in any of the HTML file, then say "svg is found in xyz.html". (the program already exits when the enter key is pressed)


FishMonger
Veteran / Moderator

Oct 20, 2015, 9:37 AM

Post #6 of 12 (2259 views)
Re: [mohan] Searching for specific text [In reply to] Can't Post

To search all sub-directories, you'll want to use the File::Find or File::Find::Rule module.

http://search.cpan.org/~shay/perl-5.20.3/ext/File-Find/lib/File/Find.pm
http://search.cpan.org/~rclamp/File-Find-Rule-0.33/lib/File/Find/Rule.pm


Laurent_R
Veteran / Moderator

Oct 20, 2015, 10:35 AM

Post #7 of 12 (2256 views)
Re: [mohan] Searching for specific text [In reply to] Can't Post

BTW, this is cross posted on Perl Monks: http://www.perlmonks.org/?node_id=1145392 and there are quite a few answers there.

It is considered polite, when cross posting, to inform the reader about it, so as to avoid duplication of work at various places on the Internet.


mohan
User

Oct 20, 2015, 10:45 PM

Post #8 of 12 (2249 views)
Re: [Laurent_R] Searching for specific text [In reply to] Can't Post

Yes I did post it there. Sorry for not giving a heads up about it.

There was no reply here (posted this on Oct 18 and posted in perl monk on Oct 20), so tried my luck there :)

no offense guys ! :)


Laurent_R
Veteran / Moderator

Oct 21, 2015, 6:13 AM

Post #9 of 12 (2236 views)
Re: [mohan] Searching for specific text [In reply to] Can't Post


In Reply To
no offense guys ! :)


No problem, really, and nothing personal either.

I was just giving a small part of the very commonly accepted "netiquette" on technical forums about cross-posting, because it is not fun to spend for free valuable time in trying to answer a question just to find out that an answer (perhaps even a better one) has already been given before by someone else somewhere else on the Net.


mohan
User

Oct 21, 2015, 9:29 PM

Post #10 of 12 (2218 views)
Re: [Laurent_R] Searching for specific text [In reply to] Can't Post

I solved the below problem:
"3. It should read opf for the text "cover". if its found then display cover is found. "


Code
{  
label => 'test',
test => sub { $_[0] =~ /(type="toc"|\Qname="reference type="toc"" content="true"\E)/ },
testaction => sub { $_[0]->{done} = 1 },
postaction => sub { print +( $_[0]->{done} ) ? 'ToC is found' : 'ToC is not found', "\n" },
},


This did the job!!

I'm trying to work out other points now !


mohan
User

Oct 21, 2015, 11:03 PM

Post #11 of 12 (2214 views)
Re: [mohan] Searching for specific text [In reply to] Can't Post

I figured out the search for "svg" tag in the HTML files:


Code
{  
extension => 'xhtml',
operations =>
[

{
label => 'svg',
test => sub { $_[0] =~ /(svg|\Qname="svg" content="true"\E)/ },
testaction => sub { $_[0]->{done} = 1 },
postaction => sub { print +( $_[0]->{done} ) ? 'SVG tag is found' : 'SVG is not found', "\n" },

},
]
},



But can you help me with the below points?

1. This code should also search for ".html" files.
2. This reads all the html files and displays the O/P of each files. Instead, I just want to display the files having svg tags.


mohan
User

Oct 25, 2015, 8:25 PM

Post #12 of 12 (2192 views)
Re: [mohan] Searching for specific text [In reply to] Can't Post

Any help guys?

 
 


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

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