Reputation: 2264
hi friends now 'm working in perl i need to check the give string occurence in a set of array using perl!i tried lot but i can't can anybody?
open FILE, "<", "tab_name.txt" or die $!;
my @tabName=<FILE>;
my @all_files= <*>;
foreach my $tab(@tabName){
$_=$tab;
my $pgr="PGR Usage";
if(m/$pgr/)
{
for(my $t=0;scalar @all_files;$t++){
my $file_name='PGR.csv';
$_=$all_files[$t];
if(m\$file_name\)
{
print $file_name;
}
}
print "\n$tab\n";
}
}
Upvotes: 0
Views: 55
Reputation: 132822
Aside from the problems you have going through the array, are you looking for substr?
Upvotes: 0
Reputation:
Here is a problem:
for(my $t=0;scalar @all_files;$t++){
The second part of the for loop needs to be a condition, such as:
for(my $t=0;$t < @all_files;$t++){
Your code as written will never end.
However, this is much better:
foreach (@all_files){
In addition, you have a problem with your regex. A variable in a regex is treated as a regular expression. .
is a special character matching anything. Thus, your code would match PGR.csv
, but also PGRacsv
, etc. And it would also match filenames where that is a part of the name, such as FOO_PGR.csvblah
. To solve this:
\Q...\E
) to make sure the filename is treated literally.^
, $
).Also, backslashes are valid, but they are a strange character to use.
The corrected regex looks like this:
m/^\Q$file_name\E$/
Also, you should put this at the top of every script you write:
use warnings;
use strict;
Upvotes: 1
Reputation: 91430
This line :
for(my $t=0;scalar @all_files;$t++){
produces an infinite loop, you'd better use:
for(my $t=0;$t < @all_files;$t++){
Upvotes: 0