Perl300 has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I am using perl v5.24.1
I have to run the code recursively for a set of 4 values, multiple times. Each time the set of 4 values will be different but I'll know these before running the program
So what I have done is created a text file and use it as input file for the program.
The filename is stored in a variable in program and that variable is used each time input file needs to be accessed. I add the list of input values in this input file in space separated format. So the input file looks like:
contents of input.txt:
text1 string1 word1 number1
text2 string2 word2 number2
text3 string3 word3 number3
text4 string4 word4 number4
In code I am reading this input.txt one line at a time, spliting that line on space as delimiter ad storing each value in variable. Then run the program for these list of variables. So my program runs as many times as the number of lines in the input.txt
It is working fine but I have few questions:
Q-1) Is this approach fine or is there any better way to do it?
Q-2) Will it be better to embed the data in input.txt in the code itself rather than keeping it in a separate file?
Q-3) If YES to question 2 then what data structure should I use that is easy to edit in future as well as easy to loop over?
open (my $fh, '<', $input_file) or die "Can't read from '$input_file': + $!"; while (<$fh>) { my ($var1, $var2, $var3, $var4) = split (' ', $_); #Call to subroutine1 (by passing $var1, $var2, $var3, $var4) which + calls subroutine2, which calls subroutine3. }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: What is better: Static input data in separate file or embedding static input data in code.
by stevieb (Canon) on Nov 02, 2017 at 20:45 UTC | |
|
Re: What is better: Static input data in separate file or embedding static input data in code.
by Discipulus (Canon) on Nov 02, 2017 at 21:05 UTC | |
|
Re: What is better: Static input data in separate file or embedding static input data in code.
by hippo (Archbishop) on Nov 03, 2017 at 10:37 UTC | |
|
Re: What is better: Static input data in separate file or embedding static input data in code.
by Anonymous Monk on Nov 02, 2017 at 23:03 UTC | |
|
Re: What is better: Static input data in separate file or embedding static input data in code.
by Anonymous Monk on Nov 03, 2017 at 13:26 UTC | |
|
Re: What is better: Static input data in separate file or embedding static input data in code.
by Perl300 (Friar) on Nov 03, 2017 at 18:38 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |