Since you mentioned that you're new to Perl and programming in general, let me explain this line by line. I've been working introductory tutorials for work, so I figure this is good practice. My apologies if you already know some of these.

use strict; use warnings;

These two lines are called "pragmas" and make your program less tolerant of sloppy code. You should start every Perl program with these two lines.

use FileHandle;

Use the built-in FileHandle module, which allows you to treat file handles (variables in the program that represent files) as objects (a programming construct that allows you to treat a variable as a self-contained entity with properties and "things it can do", also called methods).

for (my $i = 0; $i < @myArr; $i++) {

Create a new variable called $i, make it equal to 0. Repeat the following section (the section down to the matching '}') as long as the value of $i is less than the number of elements in the @myArr array. After finishing each loop, increment $i and do the test again.

# set $value to the value held in the array position we're on my $value = $myArr[$i]; # set $index to one more than our current array position my $index = $i + 1; #create our filename - when i = 0, it will be "file1" my $file = "file${index}"; write_value_to_file($value, $file); # call our function below with our current value and filename } # end the for block

Now we'll look at the function (or subroutine) write_value_to_file:

#we're starting a new subroutine sub write_value_to_file { # set two variables from the values given to this function my ($value, $fname) = @_; #create a new FileHandle object my $fh = new FileHandle; # open the file name in $fname for writing, and refer to it by $fh open ($fh, ">", $fname) # if opening the file failed, tell the user why and exit or die "unabel to write $fname($!)\N" #write the value, followed by a newline character, to the file print $fh "$val\n"l # tell the program we're done with this file. close $fh; #close the function }

Hopefully this was at least a little helpful.

UPDATED: mention $i++. Thanks johngg!


In reply to Re^2: writing each array element to a unique file by geekphilosopher
in thread writing each array element to a unique file by garbage777

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.