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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.