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 a string

 

First page Previous page 1 2 3 Next page Last page  View All


mohan
User

Jan 27, 2015, 2:17 AM

Post #1 of 51 (13705 views)
Searching for a string Can't Post

Hi Guys,

Need some help with the below Code :

Scenario :
I want to read a file and search for the keyword "font-size" used in it. If this is present then the CMD says "Font-size is found". The problem I'm now facing is when I use the below code, it reads all the lines and displayed the O/P many times. I want the O/P to come only once. If the font size is found anywhere in the file then it should come only once. And also this keyword font-size should not be considered it it's specified in "%" example font-size=100%.

Can someone help me with this?
Code :
#!/usr/bin/perl

$file = 'style.css';
open(txt, $file);
while($line = <txt>) {

if ($line =~ /font-size/) {
print "\n Forced Font is found \n";
}
}
close(txt);


Tejas
User

Jan 27, 2015, 3:32 AM

Post #2 of 51 (13703 views)
Re: [mohan] Searching for a string [In reply to] Can't Post


Code
#!/usr/bin/perl 

$file = 'style.csv';
#my $regex = qr/
# (?=.*font-size) &&
# (?!.*%)
# /ix;
open(txt, $file);
while($line = <txt>) {

if ($line =~ /font-size/) {
if($line !~ /%/){
print "Forced Font is found ,$line\n";
}
else {
print "Line has % in it , $line \n"
}
}
}
close(txt);


Please do not worry about the regex in the code.i tried to match the regex in the if condition first

Code
if  ($line =~ /$regex/)


it does work but it wont print any line with % in it, which i dont think is ur requirement

Thanks
Tejas


(This post was edited by Tejas on Jan 27, 2015, 3:32 AM)


BillKSmith
Veteran

Jan 27, 2015, 5:38 AM

Post #3 of 51 (13697 views)
Re: [mohan] Searching for a string [In reply to] Can't Post

It is not necessary to read all the lines. Exit the loop right after you find and print the first match. Use last; after the print.


Code
if ($line =~ /font-size/) {  
print "\n Forced Font is found \n";
last;
}

Good Luck,
Bill


mohan
User

Jan 27, 2015, 7:42 PM

Post #4 of 51 (13687 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

@Tejas,

This works perfectly fine as per my need !! Thanks a ton !

But there is a little modification to be done.

1. If the "font-size" is used 20 times the CMD displays it 20 times...I don't want this to happen..I just need the count of how many time it is used.

2. Also we use "$file = 'style.css';" to specify the file name here. Is it possible to read any CSS file in the current directoy without the need to specify the file name ?


Tejas
User

Jan 27, 2015, 8:49 PM

Post #5 of 51 (13685 views)
Re: [mohan] Searching for a string [In reply to] Can't Post

i dint really get this.u mean, its printing 20 times per line ?
Can u please elaborate ?

Quote
1. If the "font-size" is used 20 times the CMD displays it 20 times...I don't want this to happen..I just need the count of how many time it is used.


Yes, u can send the file name as parameter , something like below

Code
./myfile.pl <filename>


Code



      
    


mohan
User

Jan 27, 2015, 10:21 PM

Post #6 of 51 (13682 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

If the CSS file has "font-size" specified 20 times in different lines, then the CMD prints the same 20 times..But i dont want this to happen...The CMD should just say "font-size found in 20 instances"


mohan
User

Jan 27, 2015, 10:23 PM

Post #7 of 51 (13681 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

And for this :


Yes, u can send the file name as parameter , something like below
Code

./myfile.pl <filename>
-------------------------------------------

I'm actually planning to convert this pl file into an exe file so that when you just run the exe file, the program should detect all the CSS file in that present directory and throws the O/P..So is there a possible way to do this?


(This post was edited by mohan on Jan 27, 2015, 10:32 PM)


Tejas
User

Jan 27, 2015, 10:45 PM

Post #8 of 51 (13673 views)
Re: [mohan] Searching for a string [In reply to] Can't Post

Yes, it is possible.
Please follow Chris's post .

http://perlguru.com/gforum.cgi?post=79347;search_string=read%20files%20from%20a%20directory;#79347

You can do something like this


Code
use strict; 
use warnings;
my @files;
opendir(Dir, $Directoryname) or die "cannot open directory $Directoryname";
@files = grep(/\.txt$/,readdir(Dir));


All your filenames would a part of array @files.

to access them



Code
foreach $file (@files) { 
open my $handle, '<', $file or die "could not open '$file': $!";
while (my $line = <$handle>) {
#Your checks per line

}

}


The code pasted above is untested and probably would work as per ur requirements

Thanks
Tejas


(This post was edited by Tejas on Jan 27, 2015, 11:45 PM)


mohan
User

Jan 27, 2015, 11:55 PM

Post #9 of 51 (13668 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

I just tried the above code and merged it together as . Please bear with me since i'm trying to learn perl : (The directory to be read is "bin" )

#!/usr/bin/perl

my @files;
opendir(bin, $Directoryname) or die "cannot open directory $Directoryname";
@files = grep(/\.css$/,readdir(bin));
#my $regex = qr/
# (?=.*font-size) &&
# (?!.*%)
# /ix;
foreach $file (@files) {
open my $handle, '<', $file or die "could not open '$file': $!";
while (my $line = <$handle>) {

if ($line =~ /font-size/) {
if($line !~ /%/){
print "\n Forced Font is detected. -- $line\n";
}
else {
print "\n Font is specified in % -- $line \n"
}
}

if ($line =~ /line-height/) {

print "\n Forced Line-height is detected. \n";
}


if ($line =~ /position:absolute/) {

print "\n position:absolute is detected. \n";

}
}

}
close(txt);

This is giving the O/P as :

cannot open directory at C:\Perl64\bin\sample1.pl line 4.


Tejas
User

Jan 28, 2015, 12:26 AM

Post #10 of 51 (13663 views)
Re: [mohan] Searching for a string [In reply to] Can't Post

You have specified the wrong parameter as directory name in opendir
Also, please read about opendir func in perl docs

And u can specify all the patterns u want in an array , so that u wouldnt need so many if conditions in the code, which will end up confusing you
something like below and loop through the array


Code
  
my @patterns = ('font-size','line-height','position:absolute');


if u dint understand it properly,below is the way you have tried
Below should work.

Code
my @files;  

my $Directoryname = '/bin';
opendir(bin, $Directoryname) or die "cannot open directory $Directoryname";
@files = grep(/\.css$/,readdir(bin));

foreach $file (@files) {
open my $handle, '<', $file or die "could not open '$file': $!";
while (my $line = <$handle>) {

if ($line =~ /font-size/) {
if($line !~ /%/){
print "\n Forced Font is detected. -- $line\n";
}
else {
print "\n Font is specified in % -- $line \n"
}
}

if ($line =~ /line-height/) {
print "\n Forced Line-height is detected. \n";
}


if ($line =~ /position:absolute/) {
print "\n position:absolute is detected. \n";
}

}

}
close(txt);



mohan
User

Jan 28, 2015, 12:53 AM

Post #11 of 51 (13659 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

I tried the above code and still getting the same error :

C:\Perl64\bin>sample1.pl
cannot open directory /bin at C:\Perl64\bin\sample1.pl line 4.


Tejas
User

Jan 28, 2015, 1:11 AM

Post #12 of 51 (13649 views)
Re: [mohan] Searching for a string [In reply to] Can't Post

try to change the variable to your present working diectory

my $Directoryname = "your directory" ;


mohan
User

Jan 28, 2015, 1:14 AM

Post #13 of 51 (13647 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

Actually "bin" is the current working directory..and it's not working :(


mohan
User

Jan 28, 2015, 1:19 AM

Post #14 of 51 (13643 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

But when i use "my $Directoryname = getcwd ; " the code is working....does this mean when i convert the code into exe and place that exe in a current folder and run, it will execute without any errors?


Tejas
User

Jan 28, 2015, 1:25 AM

Post #15 of 51 (13641 views)
Re: [mohan] Searching for a string [In reply to] Can't Post

OK
Just try the code below
and try to se whats it is doing first


Code
use strict; 
use warinings;

use Cwd;

my $Directoryname = getcwd();

print "My Current Dir is $Directoryname \n";
my @files;

opendir(bin, $Directoryname) or die "cannot open directory $Directoryname";
@files = map { "$Directoryname/$_" } grep(/\.css$/,readdir(bin));.

foreach $file (@files) {
open my $handle, '<', $file or die "could not open '$file': $!";
while (my $line = <$handle>) {

if ($line =~ /font-size/) {
if($line !~ /%/){
print "\n Forced Font is detected. -- $line\n";
}
else {
print "\n Font is specified in % -- $line \n"
}
}

if ($line =~ /line-height/) {
print "\n Forced Line-height is detected. \n";
}


if ($line =~ /position:absolute/) {
print "\n position:absolute is detected. \n";
}

}
close($handle);
}


I will explain it later.
But you try to see what the code is doing

Thanks
Tejas


mohan
User

Jan 28, 2015, 1:29 AM

Post #16 of 51 (13637 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

Just got this error when i ran the code :

C:\Perl64\bin>sample1.pl
Can't locate warinings.pm in @INC (@INC contains: C:/Perl64/site/lib C:/Perl64/l
ib .) at C:\Perl64\bin\sample1.pl line 2.
BEGIN failed--compilation aborted at C:\Perl64\bin\sample1.pl line 2.

What does this mean?


Tejas
User

Jan 28, 2015, 1:37 AM

Post #17 of 51 (13632 views)
Re: [mohan] Searching for a string [In reply to] Can't Post

use warnings;

at the top
its a typo


mohan
User

Jan 28, 2015, 2:12 AM

Post #18 of 51 (13624 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

got the below errors :

C:\Perl64\bin>sample1.pl
Unquoted string "bin" may clash with future reserved word at C:\Perl64\bin\sampl
e1.pl line 11.
Unquoted string "bin" may clash with future reserved word at C:\Perl64\bin\sampl
e1.pl line 12.
syntax error at C:\Perl64\bin\sample1.pl line 12, near ";."
Global symbol "$file" requires explicit package name at C:\Perl64\bin\sample1.pl
line 14.
Global symbol "$handle" requires explicit package name at C:\Perl64\bin\sample1.
pl line 16.
syntax error at C:\Perl64\bin\sample1.pl line 38, near "}"
Execution of C:\Perl64\bin\sample1.pl aborted due to compilation errors.


Tejas
User

Jan 28, 2015, 2:58 AM

Post #19 of 51 (13597 views)
Re: [mohan] Searching for a string [In reply to] Can't Post

Dude, try to change the code to your requirements and do not run it blindly
as i have not tested it on my box.

i will try and look into it
Thankss
Tejas


Tejas
User

Jan 28, 2015, 3:00 AM

Post #20 of 51 (13595 views)
Re: [mohan] Searching for a string [In reply to] Can't Post

@files = map { "$Directoryname/$_" } grep(/\.css$/,readdir(bin));
change this to what ever worked for u for time being.


Tejas
User

Jan 28, 2015, 5:03 AM

Post #21 of 51 (13546 views)
Re: [mohan] Searching for a string [In reply to] Can't Post


Code
use strict;  
use warnings;

use Cwd;

my $Directoryname = getcwd();

print "My Current Dir is $Directoryname \n";

my @files;

opendir(mydir, $Directoryname) or die "cannot open directory $Directoryname";
@files = map { "$Directoryname/$_" } grep(/\.css$/,readdir(mydir));

foreach my $file (@files) {
open my $handle, '<', $file or die "could not open '$file': $!";
while (my $line = <$handle>) {

if ($line =~ /font-size/) {
if($line !~ /%/){
print "\n Forced Font is detected. -- $line\n";
}
else {
print "\n Font is specified in % -- $line \n"
}
}

if ($line =~ /line-height/) {
print "\n Forced Line-height is detected. \n";
}


if ($line =~ /position:absolute/) {
print "\n position:absolute is detected. \n";
}

}
close($handle);
}

Try Testing above..its the same but removing an extra .


(This post was edited by Tejas on Jan 28, 2015, 5:40 AM)


Laurent_R
Veteran / Moderator

Jan 28, 2015, 10:05 AM

Post #22 of 51 (13530 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

You should rather use a lexical dirhandle (just as you used a lexical filehandle). Also the glob function makes it easier to retrieve the file names:


Code
use strict;   
use warnings;
use Cwd;

my $dir = getcwd();
my @files = glob("$dir/*.css");
for my $file (@files) {
# ...
}


The additional good thing about glob it that you get the file names with their (relative) path, whereas readdir does not (it is not a problem here since your a reading in the default directory, but it is often quite useful to get the path).

Since you are reaading from thee default directory, the above code could be further simplified as follows:

Code
use strict;   
use warnings;

my @files = glob("*.css");
for my $file (@files) {
# ...
}


Or, simplifying another step further (untested, but should work):

Code
use strict;   
use warnings;

for my $file (glob("*.css")) {
# ...
}



Tejas
User

Jan 28, 2015, 10:38 AM

Post #23 of 51 (13528 views)
Re: [Laurent_R] Searching for a string [In reply to] Can't Post

Hi
I have used lexical dir handle
Can u please tell me where is the issue
Or am I confused about what a lexical dir handle is
Thanks
Tejas


FishMonger
Veteran / Moderator

Jan 28, 2015, 11:25 AM

Post #24 of 51 (13526 views)
Re: [Laurent_R] Searching for a string [In reply to] Can't Post


Code
for my $file (glob("*.css")) {


That could be further simplified to this:

Code
for my $file (<*.css>) {



mohan
User

Jan 28, 2015, 11:51 PM

Post #25 of 51 (13519 views)
Re: [Tejas] Searching for a string [In reply to] Can't Post

Already tried removing the extra ".". Still shows the same error .

First page Previous page 1 2 3 Next page Last page  View All
 
 


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

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