cvcrud has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Beginner Question
by Spidy (Chaplain) on Dec 19, 2005 at 17:23 UTC | |
| [reply] |
|
Re: Beginner Question
by Roy Johnson (Monsignor) on Dec 20, 2005 at 15:19 UTC | |
No Perl for you! | [reply] |
|
Re: Beginner Question
by jdporter (Paladin) on Dec 20, 2005 at 12:46 UTC | |
Your problem is relatively simple - it boils down to three things: (1) how to make a directory with a given name, (2) how to do something for every value in a given list of values, and (3) how to generate the range of string values 'a' to 'z'. The first part is quite easy to do. There's a Perl built-in function called mkdir. The second part uses a Perl structural building block for looping, or "iteration". It looks like this:
foreach ( values )
{
do something
}
This raises a question: How does the code inside the code block (which is delimited by the curly braces) know which value in the list is the right one for the current iteration? Well, by default, the foreach construct sets the special variable $_ to each value in the list, in turn. So do something merely needs to access the $_ variable to get the current value.
The third part is somewhat more magical, but fortunately the syntax is quite simple. Using the so-called "range operator" — which is simply written as two dots (..) — between a start an and end value, perl automatically generates the entire list of all the values in between them. Now, putting it all together:
If you throw this in a text file and run perl on it (e.g. perl mkdir_a2z.pl), it will do what you want. However, there are lots of things you could do to make it "better", such as adding the ability to accept arguments (perhaps to specify the start and/or end directory name), error checking/handling, and reporting. If you need this to be run in a CGI script, that adds a whole 'nother layer of complexity as well.
We're building the house of the future together.
| [reply] [d/l] [select] |
|
Re: Beginner Question
by tirwhan (Abbot) on Dec 19, 2005 at 17:36 UTC | |
I would like to create a PERL script Well, that'll be hard because first you'll have to write this PERL language which lots of people talk about despite it not actually existing. Maybe you should use the fantastic language Perl which lets you do this easily. As for your question, try to put the following lines together in the right order (I'm not trying to be an ass, but your question feels like homework and you should at least have to work a little bit ;-)
Update: well, I'm sorry I helped now that you've changed your node to insult people who have tried to help you and made clear that you're not here to learn but have your work done for you. Won't happen again. Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan | [reply] [d/l] |
|
Re: Beginner Question
by ptum (Priest) on Dec 20, 2005 at 15:12 UTC | |
I'm sorry, [id://cvcrud], that your first posting here has taken such a negative turn. I think you'll find that the monks here are generally a helpful and kindly lot, in spite of any first impression you may have formed. Given some of the recent discussions that have been rattling around the monastery, I think the suggestions you received were meant to be helpful, and should be taken as such. You are operating under a misconception: you did, indeed, come here to be 'schooled', as did we all. You may have received more than you bargained for, but a little humility will go a long way in helping to accept criticism, especially the kind that seems nit-picking. Don't give up on PerlMonks, but rather roll up your sleeves, wade in and make it a better place. You're welcome here, however you spell 'Perl'. Just a little more unsolicited advice: I've generally found that people are more willing to listen to me if I refrain from calling them names. :) No good deed goes unpunished. -- (attributed to) Oscar Wilde | [reply] |
|
Re: Beginner Question
by blue_cowdawg (Monsignor) on Dec 20, 2005 at 16:26 UTC | |
My son, let me engage in some kindly advice. First off, as Roy Johnson pointed out I am not possessed by the Nazis, but I am a Perl Nazi. If being such means that I want to see things done correctly then I'm a Perl Nazi! If being a Nazi means that I am passionate about my chosen favorite programming/scripting language then I'm guilty as charged. However I refuse to be associated in spirit with those monsters that 50 years ago or so attempted to obliterate an entire ethnicity based on a madman's vision of humanity. Secondly, you come to the Monestary asking for advise without consulting the Tutorials and I know this because if you had you'd realize that there is a right way and a wrong way to ask for help here at the Monastery. If you ask the wrong way don't be surprised if you have scorn heaped upon you. While I don't necessarily condone the act of heaping scorn on people, I can't help but think you brought it upon yourself. Does how we capitilize Perl mattter?When I see something in all capital letters I assume that it is a acronym for something else. A phrase, a compound word or whatever. It can be argued that PERL is an acronym for a lot of things. Those of us that have been around the language for a while have read or heard some of those and the two that come to mind right now are: As to the originally asked questionGrasshopper, let me share with you some more wisdom. In your post you made it difficult indeed for anyone to understand what went wrong with your attempts since you did not post any code. Some of us monks may well approach omnicience (sp?) but I don't think there are any here that are omnipotent. I for one never aquired the skill of clairvoyance and cannot read your code from afar and troubleshoot it for you. It has to be posted here on PM for the world to see and so we can guide you on the path of Perl rightousness. Also having an understanding of what went wrong can help as well. Post your error messages as well. It may well be that your code is correct, but when run there is a lack of permissions to do the work. That being said, let's examine what you are trying to do:
Simple enough. First you need to iterate. There are many ways to do this from the simple to the extravagant but here is one such construction:
Now, you need to invoke one of many tools that allow you to programmatically create the named directories within your iteration. I have a favorite, but I'll keep it simple for now. Also I want to make sure I do any error checking that is available to me in order to know what went wrong if things indeed go wrong. If I consult perldoc I find that mkdir returns a value if it fails. Further it sets $! which allows me to print what happened. Finishing this thought we get: If the mkdir fails the code will exit and you'll have a nice error message telling you why it failed. Now, I made a horrible assumption here. You also failed to tell me what platform your code is expected to run under or anything else about your environment. My comment about umask is valid on a *nix environment but doesn't make a hill of beans on a Evil Empire® based system. Good luck on your journey towards Perl nirvana, careful of the pitfalls and snares along the way, and do ask questions here at PM. Just be smart about how you ask...
Peter L. Berghold -- Unix Professional Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg | [reply] [d/l] [select] |
|
Re: Beginner Question
by thedoe (Monk) on Dec 19, 2005 at 17:25 UTC | |
What is this for, and what have you tried so far? It is very important to see what you have attempted so that we can point you in the correct direction and help show you what you have done wrong and why it is wrong. This is certainly something Perl Monks can help you with, and I look forward to doing so. But first, I need to know that you have been trying and that this is for a legitimate reason. | [reply] |
|
Re: Beginner Question
by Roy Johnson (Monsignor) on Dec 19, 2005 at 17:26 UTC | |
PS: Writing PERL is not recommended. Stick with Perl. :-) Caution: Contents may have been coded under pressure. | [reply] |
|
Re: Beginner Question
by phaylon (Curate) on Dec 20, 2005 at 15:12 UTC | |
Ordinary morality is for ordinary people. -- Aleister Crowley | [reply] |