Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Checking value of an extracted variable

by r.biswas (Initiate)
on Dec 23, 2012 at 08:16 UTC ( #1010077=perlquestion: print w/ replies, xml ) Need Help??
r.biswas has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

How can a perl script generate an output file based on the value of some parameters existing in the the input file but their value defined in a seperate config file. In the example shown below, I am using OPT: as a keyword to find such options and then using split I am able to extract the SEL_BLOCK keyword. But the issue is I am unable to check the value of SEL_BLOCK and so unable to generate the text in the output file.

Input.txt(input file)

OPT: SEL_BLOCK

This block will only get printed in the output file if value of SEL_BLOCK = 1 in the configuration file.

Config_file(configuration file) SEL_BLOCK = 1

Comment on Checking value of an extracted variable
Re: Checking value of an extracted variable
by moritz (Cardinal) on Dec 23, 2012 at 08:28 UTC

    Please show the code you have written so far.

    using split I am able to extract the SEL_BLOCK keyword. But the issue is I am unable to check the value of SEL_BLOCK

    This surprises me. You seem to have come far enough to extract the string on the left of the = sign, why can't you work with the string on right of the = sign?

      My code is shown below. After locating the keyword(stored in $first) I need to check the value of the variable stored in $first. $first = SEL_BLOCK. But I need to check the value of the SEL_BLOCK. How can it be done?

      open(OUT,">Output.txt") or die "Can't open output.txt for writing:$!\n +"; open(IN, "<Input.txt") or die "Can't open input.txt for reading:$!\n"; @array = <IN>; foreach $line (@array) { chomp ($line); if ($line =~ /^OPT:/) { $var = $'; @param = split / /, $var; $first = $param[1]; print "$first\n"; if ($first==1) { # This is not working as desired $start = 1; } else { $skip = 1; } } elsif ($start == 1 || $skip==1) { if ($line =~ /^]/) { $start = 0; $skip = 0; } elsif ($skip == 0) { printf (OUT "$line\n"); } } else { printf (OUT "$line\n"); } }
        I also like to point out that in the input file the keyword will be declared as below
        Input.txt(input file) OPT: SEL_BLOCK [ some text to be copied to output file if SEL_BLOCK = 1 ]

        SEL_BLOCK will be defined in a config file which can be included in the perl script. The script will have visibilty of the value of SEL_BLOCK and take appropriate actions(copying contents from input to output file).

Re: Checking value of an extracted variable
by roboticus (Canon) on Dec 23, 2012 at 14:17 UTC

    r.biswas:

    I'd suggest writing a subroutine to read all your configurations into an array, and then when processing the input file you can simply load the current configuration. Something like:

    my @configurations; load_configurations("config_file"); open my $FH, '<', $input_fname or die "Can't open $input_fname: $!"; while (<$FH>) { my @config_vars; ... parse out an input entry ... @config_vars = @$configurations[$configuration_ID]; ... code to process input using configuration variables... } sub load_configurations { my $fname = shift; open my $FH, '<', $fname or die "Can't open $fname: $!"; while (<$FH>) { my @config_vars; ... parse out a configuration... $configurations[$configuration_ID] = [ @config_vars ]; } }

    This way, you have all the configurations at your fingertips, and you can process each input entry with a different configuration, if desired.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-07-24 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (156 votes), past polls