http://www.perlmonks.org?node_id=973812


in reply to Re: running multiple loops
in thread running multiple loops

Here is a solution which outputs the same as you showed us

It would help us, if you give us a brief explanation of the purpose of your files

use strict; use warnings; ### ### !!! ALWAYS use strict and warnings !!! ### print "$0 Begin at ", scalar localtime, "\n\n"; open my $hsome_numbers, '<', "File1.text" or die "cannot open file File1"; #File1 should be renamed to something like some_numbers! my @some_numbers = <$hsome_numbers>; #slurp it! close $hsome_numbers; #remember every element of the array now has a line ending at its end, + so chomp! open my $hstring_file, '<', "File2.text" or die "cannot open file File2"; #File2 should be renamed to something like string_file??? #what about the content in string_file, you showed us 7 lines containi +ng "IT"??? #maybe should only unique content be used??? #I decided to only use the unique elements and do a slurp! my @strings = <$hstring_file>; #remember every element of the array now has a line ending at its end, + so chomp! my @uniq_strings; my %seen = (); foreach my $item (@strings) { chomp $item; push(@uniq_strings, $item) unless $seen{$item}++; } # resulting array is @uniq_strings; open my $houtput_files_array, '<', "File3.text" or die "cannot open file File3"; #File3 should be renamed to something like output_files_array! #File3 is an array of filenames, so slurp them! my @filenames = <$houtput_files_array>; close $houtput_files_array; #remember every element of the array now has a line ending at its end, + so chomp! for my $filename (@filenames) { chomp $filename; open my $hout, '>', "${filename}.output" #918.output, 908.outpu +t or die "cannot open file ${filename}.output for writing"; #now you have an output file per line in the array #per line in file some_numbers you want to write the line SR-<line + in string_file>-SD<line in some_numbers>-IBV-sw<line in output_files +_array>-A and the line SR-<>-SD<>-IBV-sw<>-B for my $string (@uniq_strings) { #per (unique) line in line in +string_file for my $number (@some_numbers) { #per line in some_numbers chomp $number; print {$hout} "SR-$string-SD$number-IBV-sw$filename-A\n"; print {$hout} "SR-$string-SD$number-IBV-sw$filename-B\n"; # What you can see here is, that names matter. use variabl +e names coming from your domain, so you will understand your program +better. # Never use anything like file1, file2, line1, line2, this + is BAD style and does not reflect your problem! } } close $hout; print "...processed output file ${filename}.output\n"; } print "$0 Ended at ", scalar localtime, "\n\n"; 1;

You should be able to make a working program out of this.

Look at the comments in the code!

I decided to slurp in every file, maybe this is not the best way to read a multi GByte file

Replies are listed 'Best First'.
Re^3: running multiple loops
by ddrew78 (Beadle) on Jun 01, 2012 at 17:09 UTC

    Thank you, that did it. It works like a champ now.

    As far as what it does, this piece of code is part of a large script that generates configuration files for network elements based on numerous inputs by the user. Some of these inputs include identifiers for various elements, ip addresses, etc. The longest 'final' output i had received thus far was about 29 pages in a word document.