Based on the limited information you've provided I assume you wish to execute two grep statements per filename.
But first, even though this might be a simple script, simple scripts have a habit of growing and evolving over time, so start it out properly at the beginning.
use strict *ALWAYS*
use warnings
use a standard formatting and indentation style (i like K&R)
Give this a try:
#!/usr/bin/perl
use strict;
use warnings;
my @filename = qw{filename1 filename2 filename3};
my @data =qw{DATA1 DATA2};
foreach my $file (@filename) {
foreach my $searchdata (@data) {
my $cmd_results = `grep -c $searchdata $file`;
print "$file | $searchdata | $cmd_results \n"; # or whatever t
+ags refers to...
}
}
It may be worth noting that your original method would have run into problems once $i reaches 2 (and tries to process DATA3 which doesn't exist...)
Using a foreach is nice since you don't need to care about an iterator, and you can make the code more self documenting... IMHO
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.