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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.