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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [mohan] Searching for a string: Edit Log


Jan 29, 2015, 8:39 AM

Views: 12207
Re: [mohan] Searching for a string


I see you have already received plenty of help with your problem, but wanted to provide the approach I would take. My version uses an operations array, where each element is a hash made up of a label ( in order to identity the test ), a test ( a code ref which accepts the line as an argument and returns either true or false indicating if the test passed or failed ), and a count ( dynamically added to the hash or reset while iterating over each file and incremented each time a test passes ). I find this approach more manageable than a group of conditions with individual count variables.

use strict; 
use warnings;
use Cwd;

my @operations =
label => 'non percentile font size',
test => sub { $_[0] =~ /font-size\s*(:|=)\s*[^%]+$/ },
label => 'line height',
test => sub { $_[0] =~ /line-height/ },
label => 'position absolute',
test => sub { $_[0] =~ /position\s*(:|=)\s*absolute/ },

my $dirpath = getcwd;

my @filepaths = glob( "$dirpath/*.css" );

print "no css files\n" unless @filepaths;

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

$_->{count} = 0 for ( @operations ); # set or reset counts.

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

while ( my $line = <$filehandle> )
$_->{test}->( $line ) and $_->{count}++ for ( @operations ); # for each operation, call the test and increment count if it returns true.

close $filehandle;

print "$_->{label} occurred $_->{count} times\n" for ( @operations ); # for each operation, print the outcome.



(This post was edited by Zhris on Jan 30, 2015, 6:56 AM)

Edit Log:
Post edited by Zhris (Enthusiast) on Jan 30, 2015, 6:56 AM

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

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