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

I've got a bunch of files I need to pull from various locations and process them individually. I'd like to store the information on their location and filename along with an ID to process them by in an array, and split on a delimiter like say a comma.

This is all basicly sudo code I'm writing as i think about it, I know that I don't know what i need but I'm hopfully close enough to explain my need :D

Here's a rough example.
# Source Location, Filename, An ID that I need for file processi +ng @array('C:\Source1\', 'File1.txt', 'ReferenceIDForProcessing' 'D:\Source2\sub\', 'File2.txt', 'ReferenceIDForProcessing' 'E:\Source3\sub2\', 'File3.xml', 'ReferenceIDForProcessing' 'C:\Source4\', 'File4.pgp', 'ReferenceIDForProcessing'); # Assuming the syntax is even close, how do I then irrerate over that +array to process each line or grouping? # I'm hoping to do a loop something like this $centralLocation = "Z:\Processing\"; open(FILES, "<@array") or die $!) ; while (<FILES>) { $line = $_ ; chomp($line) ; @FILE = split(/,/, $line) ; # Split each line into the 3 differ +ent parts. (Example in first line of the array) Move (@FILE[0]@FILE[1],$centralLocation); # Now all files would be + in a central location for processing (ie: Move C:\Source1\File1.txt +to Z:\Processing) Process ($centralLocation@FILE[1], @FILE[2]); # How the sending wo +rks is irrevlant, only that I can put the @info[2] there for each fil +e to process. } close(FILES);
Please let me know if you have trouble following what I'm after, I'm sure what i need is relatively simple, I just don't do much in the way of arrays
  • Comment on Howto build an (associative?) array to process files in different locations
  • Download Code

Replies are listed 'Best First'.
Re: Howto build an (associative?) array to process files in different locations
by CountZero (Bishop) on Mar 01, 2012 at 19:43 UTC
    Use an array of arrayrefs.

    @array = ( ['C:\Source1\\', 'File1.txt', 'ReferenceIDForProcessing'], ['D:\Source2\sub\\', 'File2.txt', 'ReferenceIDForProcessing'], ['E:\Source3\sub2\\', 'File3.xml', 'ReferenceIDForProcessing'], ['C:\Source4\\', 'File4.pgp', 'ReferenceIDForProcessing'], );
    "Walking" this structure is easy.:
    for my $record (@array) { my $path = $record->[0]; my $name = $record->[1]; my $id = $record->[2]; print "$path $name $id\n"; }

    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

    My blog: Imperial Deltronics
Re: Howto build an (associative?) array to process files in different locations
by kcott (Archbishop) on Mar 01, 2012 at 19:33 UTC

    A hash (associative array) is probably the way to go. Maybe something like:

    my %proc_data = ( ID1 => [qw{location1 filename1}], ID2 => [qw{location2 filename2}], ... );

    To process your files, use something like:

    for my $id (keys %proc_data) { my $location = $proc_data{$id}[0]; my $filename = $proc_data{$id}[1]; # file processing code here }

    I strongly recommend you put the following two lines at the start of your script to get feedback on errors and possibly dangerous code:

    use strict; use warnings;

    If you have trouble understanding error or warning messages (they can be quite terse), also add:

    use diagnostics;

    In addition, I'd recommend the following documentation:

    -- Ken

Re: Howto build an (associative?) array to process files in different locations
by GrandFather (Saint) on Mar 01, 2012 at 19:42 UTC

    Your code is completely hopeless, even as pseudo code so I'm pretty much ignoring it completely. I suspect you are very new to Perl and maybe haven't actually written very much working code. As a hint for the future: always use strictures (use strict; use warnings;).

    However your first paragraph and node title resonate with each other and strongly suggest you want a Perl hash in the mix somewhere. It's a little more complicated than just a hash though because you have a list of files to process and lists of things are stored in an array. So the data structure you probably need is and array of hashes. Consider:

    use warnings; use strict; # Source Location, Filename, An ID that I need for file processi +ng my @files = ( {path => 'C:\Source1\\', name => 'File1.txt', id => 'Referenc +e1'}, {path => 'D:\Source2\sub\\', name => 'File2.txt', id => 'Referenc +e2'}, {path => 'E:\Source3\sub2\\', name => 'File3.xml', id => 'Referenc +e3'}, {path => 'C:\Source4\\', name => 'File4.pgp', id => 'Referenc +e4'}, ); my $to = "Z:\\Processing\\"; for my $file (@files) { print "Move '$file->{path}$file->{name}' to '$to$file->{name}' ref + $file->{id}'\n"; }

    prints:

    Move 'C:\Source1\File1.txt' to 'Z:\Processing\File1.txt' ref Reference +1' Move 'D:\Source2\sub\File2.txt' to 'Z:\Processing\File2.txt' ref Refer +ence2' Move 'E:\Source3\sub2\File3.xml' to 'Z:\Processing\File3.xml' ref Refe +rence3' Move 'C:\Source4\File4.pgp' to 'Z:\Processing\File4.pgp' ref Reference +4'
    True laziness is hard work
Re: Howto build an (associative?) array to process files in different locations
by shadowfox (Beadle) on Mar 02, 2012 at 14:53 UTC
    Thanks guys, I wasn't "too far" off on what I needed, I just didn't know where to start on doing it.

    I appreciate the multiple examples, I deffinitly understand it better now.