more useful options | |
PerlMonks |
Re: Creating an array from a text fileby aufrank (Pilgrim) |
on Aug 13, 2002 at 15:59 UTC ( [id://189841]=note: print w/replies, xml ) | Need Help?? |
Greetings and welcome to the Monastery! Your first node may very well be an interesting question, but it's hard to tell because it is a bit vague. Statements like "[use] each element of the array to perform some action" are hard for readers to respond to because they don't give enough details about exactly what you're trying to do. One way to help your fellow monks help you is to post code along with your question. You say that you have the actions working-- if we could see that code it would greatly help us to understand the nature of your question. That said, there are a few things that might help you. The first is reading some of the docs available at this site (use the search box), at http://www.perldoc.com, or included with your local perl distribution (type perldoc perldoc for meta help. look into the -f and -q options, specifically). It seems like what you're looking for is very easily accomplished. perldoc -f open would be a good place to learn how to access a file. As for getting the contents of a file into an array, consider this block of code:
You might also want to read the node that inspired that code, which can be found here. Also, if you are trying to perform some operation on every element of the array once you have created it, you should definitely get comfortable with map-- one of the most useful tools in perl, IMO :) Check out this excellent tutorial by jeffa for help with map. If you have any questions about what that code is doing, please feel free to respond and I'll be more than happy to explain. good luck! UPDATE: I should have explained that this code will make each separate word an element of the array-- to use whole lines rather than individual words, definitely use the far simpler code in Ovid's response. If you do go line by line, be sure to check out chomp. I also added a close statement to my code, 'cause it should have been there in the first place.
In Section
Seekers of Perl Wisdom
|
|