Re: [ravi.joshi53] Reading all the files from a directory
[In reply to]

Can't Post

Hi,

As suggested by Laurent_R, glob can be used to neatly read the contents of a directory:

Code

my $dir_path = $ARGV[0];

while (<"$dir_path/*">) { print "$_\n"; }

Rather than telling you everything thats wrong with your code, here is an example that prints the first line of every file in the directory supplied. Possibly compare it against your own code to see where you have gone wrong:

Cheers Zhris, Thank you for the answer. The problem was because of wrong Open Function in my code. However i just have two doubts in your code. (1) What is the use of grep { -f "$dir_path/$_" }? The script is working fine after removing it also.

(2) While creating file path, you are using my $file_path = $dir_path . '/' . $file_name; . Currently i am on Ubuntu, so file separator ='/' is working fine, what about Windows? Will the same file separator work there? If not, then suggest some alternate to get file separator dynamically based on the OS.

Re: [ravi.joshi53] Reading all the files from a directory
[In reply to]

Can't Post

Hi ravi,

I had updated my code since you read, which I guess makes things more complicated with use of map too.

I am using grep to ensure elements readdir returns that are not files (namely directories) are removed. Grep is fundamentally a list filter operation. If you are concerned, the map is used to translate the file_name into a full file_path rather than doing it inside the loop.

I tested the code on Windows and the / file separator works fine (see the actual $dir_path I tested with below). I've actually never considered this issue before now (i'll look into it!).

Hi Zhris, Yeah... Absolutely right. The reason why grep { -f "$dir_path/$_" } is working fine for me is that there is no subdirectories inside the input directories. You cleverly handled it. Regarding use of map function here will reflect to increase in performance. Brilliant you.