Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Is there a way around grep?

by toolic (Bishop)
on Oct 11, 2011 at 01:20 UTC ( #930722=note: print w/replies, xml ) Need Help??

in reply to Is there a way around grep?

In this case, think of grep as a for loop. It loops through all 5 of the filenames in your array variable (@index_file_names). For each filename ($_), it compares against the $filename value. If they are equivalent, an internal counter is incremented. grep returns the number of matches. If at least one match occured, the if clause is executed.
use File::Basename; #list of filenames to normalize my @index_file_names=qw(index.html index.htm index.php index.asp index +.cgi); normalize_url('foo'); normalize_url('bar/index.php'); sub normalize_url { my $old_url = $_[0]; chomp($old_url); #saves name at the end my $filename=basename($old_url); if (grep {$_ eq $filename} @index_file_names) { #saves the directory part my $normalized_url=dirname($old_url); #$normalized_url; print "$old_url: found\n"; }else{ #don't need to normalize url #$old_url; print "$old_url: not found\n"; } } __END__ foo: not found bar/index.php: found
This is a common use of grep.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://930722]
[Corion]: Hi all! After 13 years or so in the same room, I've now moved to a new room (one door further) - weird :-D

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2017-10-17 08:51 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (224 votes). Check out past polls.