Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

mikevanhoff--

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:

sub words_from_file { my $file_path = shift; my @word_list; open WORD_FILE, "< $file_path" or die "Could not open $file_path: $!\n"; @word_list = map { split /,\s*|\n/ } <WORD_FILE>; close WORD_FILE or die "Could not close $file_path: $!\n"; return \@word_list; }

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!
--au

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 reply to Re: Creating an array from a text file by aufrank
in thread Creating an array from a text file by mikevanhoff

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-16 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found