Madam has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have file where i need to select the template block depending on what i specify in the command line. Below is my file
template1(test1,test2) { variable_1 : input; index_1(" 1.0, 2.0"); index_2(" 1.0, 2.0"); } templat2(test1,!test2) { variable_1 : output; index_1(" 1.0, 2.0"); } template3(!test1,test2) { variable_1 : time; index_2(" 1.0, 2.0"); } template4(!test1,!test2) { variable_1 : time; index_1(" 1.0, 2.0"); }
i.e,if i specify "test1" in the command line,i need to select template2 as template2 contains test1,!test2 (because i have not specified test2 in the command line.) to explain using a table
command line template selection reason test1 template2 test1 ,no test2 not specified test2 template3 test2 specified test1 not specfied test1,test2 template1 both are specified nothing in command line template4 both are not specified
currently i know how to get the values from command line . i am storing it in an array.but i dont know how to deal with the expression "test1,!test2" (this kind of expression) in the file. -Madam

Replies are listed 'Best First'.
Re: regexp matching issue
by holli (Abbot) on May 17, 2005 at 11:31 UTC
    I can see not a single bit of perl here. Can you clarify a bit more?


    holli, /regexed monk/
Re: regexp matching issue
by prasadbabu (Prior) on May 17, 2005 at 11:48 UTC

    If i understood your question correctly, is this you want?

    $input = $ARGV[0]; undef $/; $str = <DATA>; while ($str =~ /(template?\d+)[^}]+}/gsi) { $hash{$1} = $&; } %temp = ('test1' => "template2", 'test2' => "template3", 'test1,test2' + => "template1"); @a= keys %hash; @m= grep /^$temp{$input}$/, @a; print "$m"; if (@m) { print "$hash{$m[0]}" ; } else { print "$hash{'template4'}"; } #print "$_\t$hash{$_}\n" for keys %hash; __DATA__ template1(test1,test2) { variable_1 : input; index_1(" 1.0, 2.0"); index_2(" 1.0, 2.0"); } template2(test1,!test2) { variable_1 : output; index_1(" 1.0, 2.0"); } template3(!test1,test2) { variable_1 : time; index_2(" 1.0, 2.0"); } template4(!test1,!test2) { variable_1 : time; index_1(" 1.0, 2.0"); }

    updated:

    Prasad

Re: regexp matching issue
by radiantmatrix (Parson) on May 17, 2005 at 15:42 UTC

    I think you're trying to do something like this:

    ## read the file by block, not by line: open INPUT, '<', $filename or die($!); local $/ = "}\n"; # causes the end of block to be the "eol" my ($t1, $t2) = ('!test1','!test2'); # Now, determine from command line which tests to run my $commandline = join(' ',@ARGV); $t1 = $1 if $commandline =~ m/\b(test1)\b/; $t2 = $1 if $commandline =~ m/\b(test2)\b/; # Now cycle through blocks until we find the one we want my $correct_block; while (<INPUT>) { #remember, this reads whole blocks next unless m/template[.]*?($t1&$t2)/; $correct_block = $_; last; } ## $correct_block will now contain the block you want
    this is both rough and untested. It's meant to get you on the right track, only

    I hope that helps out a bit...


    The Eightfold Path: 'use warnings;', 'use strict;', 'use diagnostics;', perltidy, CGI or CGI::Simple, try the CPAN first, big modules and small scripts, test first.