Perl: Counting Files With File::Find
So this is my code:
Code:
#!/usr/bin/perl Q1: Why is each filter hit counted only when the conditional is not true? Q2: I've tried taking the file type, (.old), and put it into a variable for better usability, but then the script fails. |
Quote:
Code:
use warnings; Code:
use strict; Secondly, publish here full non-working code. |
And looking at code at given by the OP http://forums.devshed.com/showthread...24#post1473024 I do not see usage of $/ Perl built-in variable; I do no understand what it has to do with the task.
|
Quote:
Code:
print "$File::Find::name$/" |
Quote:
If you are familiar with Perl and just don't quite get File::Find - which does have an odd API at first - you might check out this article on it: http://www.stonehenge.com/merlyn/LinuxMag/col45.html |
Quote:
... FWIW, if one needs to count files only, he/she should make sure that directories are not counted. |
First of all, use warnings does not answer my questions.
Second of all, I'd still love to have the questions answered. :) Thirdly, thank you very much Telemachos, for giving me a heads up on the $/. I'm now in the process of reading the article you recommended. |
Quote:
If you do not understand the documentation, ask specific questions about the things you do not understand. "use warnings;" is must - Perl runtime will tell you about things which might be wrong (if any). If you do not use "use warnings;", you assume your code has no runtime problems - which is not a good assumption. You shouldn't be even asking questions unless you have Code:
use strict; |
In a nutshell, when you call the find method, you pass it two parameters: first a reference to a subroutine (usually) and second a directory location.
The File::Find module starts from the directory location you specify and travels downward recursively (that is into every sub-dir and sub-dir of that sub-dir etc.). The module then calls the code in the subroutine on every item (file or directory) that is found in that downward spiral. So here's a case somewhat like yours: let's imagine that I want to find all the files ending in '.rb' in the current directory ('.') or any subdirectory of the current directory. I could do this: Code:
#!/usr/bin/env perl If the subroutine is simple like this, you can include it inline in the call to find, but if it's more complicated, you can store it separately and pass a reference to the subroutine. For example: Code:
find(\&find_rubies, $dir); Code:
my $find_rubies = sub { |
Quote:
|
Quote:
What I wrote is all in the docs, but I didn't include everything in the docs. Sometimes it helps to start with a short, somewhat simplified version. |
Quote:
... It looks to me that the OP's problem is not specifically File::Find module, but much more basic things in Perl. Or even in programming in general - like the issue of types and comparing apples to oranges. |
Please stop trolling, Sergei. Telemachos is doing a great job explaining. Thanks T! :)
Here's what use warnings; returns: Code:
Argument "" isn't numeric in numeric ne (!=) at countfilesoftype.pl line 9. Again, the (quite specific) questions are: Q1: Why is each filter hit counted only when the conditional is not true? You should think it would work when it was set to true. (I've tested the script, and it always returns the correct number of files.) Q2: I've tried taking the file type, (.old), and put it into a variable for better usability, but then the script fails. I gather this is an issue with how Regular Expressions are used, and I have a hunch it may have something to do with line breaks or something in that order, but I just don't know for sure - or how to fix it. I will be very greatful for help with this. :) |
Quote:
So, what exactly you don't understand in the item in bold ? It's a sentence in English. The warning illustrates your fundamental problem WRT types, i.e. what types (in programming) are for, which types are compatible and which are not, etc. Or apples vs oranges. All the rest of your questions are secondary - because the warning tells you that a particular line of your code is senseless, and the senseless line is the main one in a sense it is supposed to produce the correct hit. And, by the way, are familiar with the concept of WEB search ? I.e. have you tried to enter Argument "" isn't numeric in numeric ne (!=) into a WEB search engine ? Third match from Yahoo appears to be relevant. ... Are you familiar with the RTFM concept ? How about 'man perl' for starters ? In my case it produces among other things: Code:
67 perldiag Perl diagnostic messages |
Sergei, When people get pissed off by your posts or the way you act, do you really think your advice is wanted? If you stopped being so rude and condescending, I might choose to listen to you. Heck, if you just bothered to answer the questions plain and simple, instead of complaining about my mental abilities, when it's obviously you who's lacking in mental abilities for explaining things in an understandable and polite fashion, then I might listen to you. However as it is, I choose to ignore you. :)
|
Quote:
Alas, I had a very unpleasant experience of working with people like you - a big pain from neck to lower back. Regarding mental abilities - do you understand RTFM, i.e. "Read The Fine Manual" ? If not, programming is not for you, if yes, why haven't you read it ? |
All times are GMT -5. The time now is 05:19 PM. |