garbage777 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a small question.

let us say we have an array @myArr = {1,2,3,4,5,6,7,8,9,10,11};

I want to write each element of this array into a unique file. That is, element $myArr[0] is written to file1, element $myArr[1] is written to file2, $myArr[2] is written to file3 and so on.

How can I do this? As always, any advice is greatly appreciated.

--A Beginner

Replies are listed 'Best First'.
Re: writing each array element to a unique file
by liverpole (Monsignor) on Dec 15, 2006 at 00:25 UTC
    Hi garbage777,

    Here's one way to do it:

    use strict; use warnings; use FileHandle; for (my $i = 0; $i < @myArr; $i++) { my $value = $myArr[$i]; my $index = $i + 1; my $file = "file${index}"; write_value_to_file($value, $file); } sub write_value_to_file { my ($val, $fname) = @_; my $fh = new FileHandle; open($fh, ">", $fname) or die "unable to write $fname ($!)\n"; print $fh "$val\n"; close $fh; }

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

      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!

        <nit>

        When you explained the for loop you omitted to mention that $i is incremented at the end of the code section before the completion test is done and the section is repeated.

        </nit>

        If tutorials are intended for complete beginners, it probably pays to explain every last detail.

        Cheers,

        JohnGG

        Hi, Thank you very much for a detailed explanation. Best, garbage777
      Hi liverpole, Thank you very much. Best, garbage777
Re: writing each array element to a unique file
by rje (Deacon) on Dec 15, 2006 at 00:12 UTC
    To help you better, please let us know if you have experience programming in C, Pascal, Java, or some other language. That would allow us to use those languages to explain Perl's analogous concepts to you.

    I'd suggest iterating over the array, and building a filename with a string plus the current array index. You may then create the file using that filename, and store the current value. Without going into Perl specifics yet, does that make sense in general?
      Hi rje, Thank you for asking. I am just a high school student trying to learn Perl....I see my dad doing a lot of stuff with perl, verilog and some other stuff he does to do chip designs. I just think Perl is cool so I am trying to learn it. unfortunately, this is the first language I ever touched! Best Regards, garbage777
Re: writing each array element to a unique file
by vladdrak (Monk) on Dec 15, 2006 at 00:05 UTC
    First, an array is declared like so:
    @myArr = ( 1,2,3,4,5,6,7,8,9,10,11 );
    You can write each of these to a new files like this:
    @myArr = ( 1,2,3,4,5,6,7,8,9,10,11 ); for my $item (@myArr) { open(ITEM ">$item") or die "could not open $item: $!\n"; print ITEM $item; close ITEM; }

      Except that an array is declared like so:

      my @myArr = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);

      :)

      You were thinking of an array ref, maybe?

      my $myArr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ];
        duh, yeah. fixed.