0

Am very new to Perl,

My requirement is read one directory and search or match for particular file and handle read that file

I know its very easy to you people but i cannot do that so please give some guidance to complete this

i have listed by code below.

{

#!/usr/bin/perl
use strict;
use warnings;
use File::stat;
use File::Copy;
use Sys::Hostname;

$source_raw_directory = "/dfactory/data/raw/in/";

opendir (DIR,$source_raw_directory) 

#foreach @array (grep(/hiea_._DeletedPatient\.txt$/,readdir(DIR)));
@array = grep (/\hiea_._DeletedPatient\.txt$/,readdir(DIR))
    #print "@array"; 
closedir(DIR);

} am getting error like below

"Array found where operator expected at Deleted_Patient.pl line 45, at end of line (Missing operator before ?) syntax error at Deleted_Patient.pl line 45, near ")

    #foreach @array (grep(/hiea_._DeletedPatient\.txt$/,readdir(DIR)));
     @array "

Deleted_Patient.pl had compilation errors"

Thanks pandia

3 Answers 3

1

I wouldn't. I find opendir and grep a clunky idiom to be using when you can glob instead.

foreach my $filename ( glob ( "$source_raw_directory/*iea_._DeletedPatient.txt" ) ) {
    print $filename,"\n";
}

Note - glob expands the path, so you'll have full path names. I usually consider that an advantage, because it means you don't ever forget to include the pathname with open calls.

Sign up to request clarification or add additional context in comments.

Comments

0

You say your error includes " (Missing operator before ?)". That's not particularly clear, but it seems to imply that the error comes before the code quoted in the error. The line before @array is a comment, so that's unlikely to be the problem, so let's look at the line before that.

opendir (DIR,$source_raw_directory) 

I think it's clear what the problem is. There's no comment at the end of the line.

I tested your code using Perl 5.22.2, which gives the far clearer error message:

Array found where operator expected at test line 13, near ")

#foreach @array (grep(/hiea_._DeletedPatient\.txt$/,readdir(DIR)));
@array"

(Missing semicolon on previous line?)

Comments

-1

For first, use google and official POD document, as example http://perldoc.perl.org/functions/readdir.html

And your code worked with this:

$source_raw_directory = "/dfactory/data/raw/in/";

opendir(my $dh, $source_raw_directory) || die "Can't opendir $source_raw_directory: $!";
my @dots = grep { /\hiea_._DeletedPatient\.txt$/ } readdir($dh);
closedir $dh;

2 Comments

And, if you use "strict", you must set notice for all your vars (my @array = ...)
@PANDIALAKSHMANAN what you need in final result?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.