in reply to Read INI file

So your ini file contains .txt filenames to other ini data or do you want to construct your data like so?
$DATA{"VOICE_DIGIT"} = "BASIC_testcase.txt";

And then, can there be multiple entries? Tell us more about your format.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; open(INFH, "<", "a.ini") or die $!; my %INI; my $HEADER = "_"; while(<INFH>){ chomp; s/\s*#.*//; s/^\s+//; s/\s+$//; next unless $_; if(/^\[(\S+)\]$/){ $HEADER = uc($1); next; } $INI{$HEADER} = $_; } close(INFH); print Dumper(\%INI);

output:

$VAR1 = { 'Voice_Digit' => 'BASIC_testcase.txt' };

Replies are listed 'Best First'.
Re^2: Read INI file
by bhushanQA (Sexton) on Apr 05, 2016 at 08:59 UTC
    Hi Thanks for the reply, My .ini file is
    [Test_suit_name] Testcase1.txt
    Basically, this ini file have many test suits and under that many test cases I want to select the test cases based on test suits. Hope this clears.
      use strict; use warnings; open my $fh, '<', 'an.ini' or die "Failed to open an.ini for reading: +$! <$^E>\n"; my (%settings, $head); while (chomp(my $line = <$fh>)) { if ($line =~ /^\s*\[([^\[\]]+)\]\s*$/) { $head = $1; } elsif ($line !~ /^\s*$/) { push @{$settings{$head}}, $line; } } close $fh; use Data::Dumper; print Dumper \%settings;
      Building on SimonPratt's work:

      #!/usr/bin/perl use strict; use warnings; use Getopt::Long; my $VERBOSE; my @TESTSUITES; my %TESTCASES_hash; my @TESTCASES; GetOptions ( "testsuite=s" => \@TESTSUITES, "verbose" => \$VERBOSE ); # Often it is useful to allow comma-separated lists of values as well +as multiple occurrences of the options. @TESTSUITES = split(/\s*,\s*/,join(',', @TESTSUITES)); open my $fh, '<', 'an.ini' or die "Failed to open an.ini for reading: +$! <$^E>\n"; my (%settings, $head); while (chomp(my $line = <$fh>)) { if ($line =~ /^\s*\[([^\[\]]+)\]\s*$/) { $head = $1; } elsif ($line !~ /^\s*$/) { push @{$settings{$head}}, $line; } } close $fh; for my $suite (@TESTSUITES){ for my $test (@{$settings{$suite}}){ $TESTCASES_hash{$test}++; } } @TESTCASES = sort keys %TESTCASES_hash; use Data::Dumper; print Dumper \%settings; print Dumper \@TESTCASES; for my $testcase (@TESTCASES){ # now iterate through your testcases }

      Which would work like:

      ./an.pl -t Test_suit_name ./an.pl -t Test_suit_name -t AnotherTest_suit_name ./an.pl -t Test_suit_name,AnotherTest_suit_name

      latter two are the same, multiple testsuites are merged to produce a bigger list of testcases.

      Hope this extra push in the back helps you...

      Hi, Any clue on this ?
        If you changed this to:
        [Test_suit_name] Testcase1.txt = 1 Testcase2.txt = 0 TestCase3.txt = 1
        or something similar, you will have something that the standard .INI file modules will understand. And you can add the feature of "turning a test case off" without removing a line from the [Test_suit_name] section. I use this format for one program's default .INI file where almost all options are turned off (0), by default. The user can see what can be enabled without having to read a separate doc and add a line to the .ini file. Just change a 0 to a 1.

        Update: it is also possible to have something like:  test_suite_names = test1 test2 test8 In that case you get a hash key of test_suite_names and the text to the right of the "=" wind up being a string that you can parse. If possible, I would "go with the flow" and use a syntax that the standard modules can deal with.