Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: running multiple loops

by Anonymous Monk
on Jun 01, 2012 at 16:18 UTC ( #973812=note: print w/ replies, xml ) Need Help??


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


Comment on Re^2: running multiple loops
Download Code
Re^3: running multiple loops
by ddrew78 (Sexton) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://973812]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2014-09-17 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (92 votes), past polls