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

I wrote a script that takes a large data set (~30k points) and pulls out meaningful info to graph with GNUPlot. It works really well when I want to graph one file at a time. Below is the striped down version of the code.

I need to do this for a bunch of files. I would like to designate each file path at the top and then have the code run through for each file. I tried using a matrix but it didn't seem to work with the 'foreach' command.

So, What is the best way to run the same script on multiple files?

# Columns to be written to the output file and Plotted. $c1 = 4; # X-Axis $c2 = 16; # Y-Axis $c3 = 11; # Z-Axis # Single file to be Opened $file1 = '../filename.asc'; open(Read, $file1); @Read = <Read>; # Read file into an array # Visit each text line in turn and write data in specific column to ar +ray foreach $part (@Read) { $part =~ s/ /:/g; @Row = split(/:+/, $part); push(@Write, "$Row[$c1]\t"); push(@Write, "$Row[$c2]\t"); push(@Write, "$Row[$c3]\n"); } # Creates the plotted data file writeFile_ref ("PlottedData.txt", \@Write); close(Read); # Opens GNUPlot and Creates Plots open(G, "|/usr/local/bin/gnuplot") or die "/usr/local/bin/gnuplot"; print G <<ENDOFHERE set term x11 splot 'PlottedData.txt' ENDOFHERE

Please and Thank You!

Replies are listed 'Best First'.
Re: Reading Multiple Input Files
by CountZero (Bishop) on May 27, 2011 at 20:31 UTC
    1. Put the filenames in an array, say @filenames
    2. Put a for my $file1 (@filenames) { ... } around your existing code (the ... is to be replaced by your code)
    3. Delete the $file1 = '../filename.asc'; line
    Ready!

    Then try to add the strictures use strict; use warnings; and amend your code until all errors and warnings disappear.

    You may want to look into the three argument version of open.

    Finally, perhaps you need to change the output filenames so they do not get overwritten by the next file.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Good call! I used

      for ($i=0; $i<$Num_of_Files; $i++) { open(Read, $file[$i]); @Read = <Read>; ...}

      I used this instead of for my $file1 (@filenames) { ... } so I could assign a name for each of the output files.

      A coworker suggested that I use use strict but I don't see the usefulness of this. Why do you use it?

        I had a node around here about why to use strict....

        Ah, here it is -- On Commenting Out 'use strict;' .... It's a tad dusty, I wrote it three or four jobs back, but it is still applicable.

        ----
        I Go Back to Sleep, Now.

        OGB

        Ouch, C-style for loops in Perl.

        A more Perlish approach is

        while ( my ($index, $file) = each @filenames) {... }

        No need to calculate the number of files, you don't need to use indexes to access the filename and you still have the $index variable to use in composing the output filenames.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James