in reply to searching two pattern sequentially from one file

Hi mrityunjaynath,

When I create a simple test input file

ORIGINAL_QUARTUS_VERSION a b c d LAST_QUARTUS_VERSION w x y z

Your code works for me (after commenting out the initial opendir, which appears unnecessary), giving the (I assume) expected output "the original quartus c" and "the latest quartus y".

Are you sure your input file contains the string "LAST_QUARTUS_VERSION"? Could you show a sample input file for which your code fails? See Short, Self Contained, Correct Example.

Note that you don't need to open the file twice, you can combine your two loops into one:

my $originalquartus; my $latestquartus; open( my $OFILE, $synthfilelist ) || die "couldnt open file: $!"; while ( my $readlineqo = <$OFILE> ) { chomp $readlineqo; if ( $readlineqo =~ /ORIGINAL_QUARTUS_VERSION/ ) { my @originalqarray = ( split / +/, $readlineqo ); $originalquartus = $originalqarray[3]; print "\nthe original quartus $originalquartus\n"; } if ( $readlineqo =~ /LAST_QUARTUS_VERSION/ ) { my @latestqarray = ( split / +/, $readlineqo ); $latestquartus = $latestqarray[3]; print "\nthe latest quartus $latestquartus\n"; } next if $readlineqo =~ /^ $/; } close($OFILE);

Also, you use glob in scalar context, which only returns one file at a time, but you call the results variable $synthfilelist and you output it as "input files in current directory" - this might lead to some confusion as it will only ever be one file per glob call.

Small update: Using a next statement as the very last thing in a loop is not necessary as the loop will go to the next iteration anyway, so unless you have code following it you can remove that line.

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: searching two pattern sequentially from one file
by mrityunjaynath (Acolyte) on May 13, 2016 at 10:13 UTC

    thanks Haukex.. this a part of long code so opendir is needed . for next statement i will comment it as u are right it seems unnecessary. the code is working fine but i i am not getting last quartus version value rather i am getting a infinite loop of into the second loop along with $readlineql value. following is the snippet from input file bifrost.qsf

    set_global_assignment -name FAMILY "Cyclone III"
    set_global_assignment -name DEVICE EP3C40F484C7
    set_global_assignment -name TOP_LEVEL_ENTITY Emap8S
    set_global_assignment -name ORIGINAL_QUARTUS_VERSION 9.1
    set_global_assignment -name PROJECT_CREATION_TIME_DATE "13:05:54 JUNE 27, 2011"
    set_global_assignment -name LAST_QUARTUS_VERSION 11.1
    set_global_assignment -name USE_GENERATED_PHYSICAL_CONSTRAINTS OFF -section_id

      Hi mrityunjaynath,

      When I run your code against this sample input, I get the output "the original quartus 9.1" and "the latest quartus 11.1" - so it would seem that the problem is happening elsewhere in your code.

      To narrow down where the problem is occurring, sometimes the simplest thing to do is print debug output at strategic places in your code to monitor its progress, see also the Basic debugging checklist. Another option is stepping through your code with the debugger (tutorial).

      Once you've narrowed down where your problem is occurring, boil down your code and sample input into a Short, Self Contained, Correct Example, that is, a piece of code that compiles and reproduces the problem, along with its sample input and the expected output vs. actual output including error messages (see also How do I post a question effectively?), and post it here.

      Often this process will help you discover the bug and the fix too! :-)

      Hope this helps,
      -- Hauke D

        i have tried ur links and put some debug but it seem the second if doesnt encounter. only n times display of "into the second loop .." please fine the full code

        #! C:/perl/bin/perl.exe use strict; use warnings; use File::Copy; use File::Basename; use Cwd; use Data::Dumper; use File::Copy::Recursive qw(dircopy) ; my $fullpath = cwd(); my $value = 0; my $file = basename($fullpath); my $dir = dirname($fullpath); print("$fullpath, $file, $dir"); opendir(my $pathindex, $dir ) or die "Couldn't read index : $!\n"; while (my $currentfile = readdir($pathindex)) { print "\nCurrent Directory File $value\t"; print "$currentfile","\n"; $value++; } print "\nENTER THE PROJECT NAME FROM ABOVE LIST\n"; my $projectdir = <STDIN>; chop($projectdir); my $projectdirpath = "/"."$projectdir"; my $workingdirectory = ($dir.$projectdirpath); chdir($workingdirectory."/trunk"); my $newpath = cwd(); my $source_dirrtl; my $target_dirrtl ; # = ("$newpath"."/rtl2"); opendir(my $index, $newpath ) or die "Couldn't read index : $!\n"; while (my $file = readdir($index)) { if ($file eq "rtl" ) { if(chdir($newpath."/rtl")) { my $currentworkingdir = cwd(); $source_dirrtl = $currentworkingdir; $target_dirrtl = ("$newpath"."/rtl2"); } last; } elsif ($file eq "vhdl") { if(chdir($newpath."/vhdl")) { my $currentworkingdir = cwd(); $source_dirrtl = $currentworkingdir; $target_dirrtl = ("$newpath"."/vhdl2"); } last; } } closedir($index); my $configdirc = $newpath."/configdir"; chdir($configdirc); opendir( my $indexconfig,$configdirc ) or die "Couldn't enter config d +irectory: $!\n"; print("\nCONFIGURATION FILES IN THE CONFIG DIRECTORY \n"); my @rtlfilelist = glob "*.icfg"; print ("\n@rtlfilelist \n"); closedir($indexconfig); my $cChipId; my $cSubVersId; my %data_ids; my $readline; my $toplevelentity; my $ivariant_name; print "\nENTER THE CONFIGURATION FILE NAME FROM THE ABOVE LIST \n"; my $configfile = <STDIN>; open (my $CFILE,$configfile ) || die "couldnt open file: $!"; while ($readline = <$CFILE>) { chomp $readline; my ($key , $value) = (split /::/, $readline); $data_ids{$key} = $value if $key; next if $readline =~ /^ $/; } close($CFILE); for my $key (keys %data_ids) { $cChipId = $data_ids{'chipid'}; $cSubVersId = $data_ids{'subversid'}; $toplevelentity = $data_ids{'Top_level_entity'}; $ivariant_name = $data_ids{'Variant_Name'}; } my $source_dirsim = ("$newpath"."/sim"); my $target_dirsim1 = ("$newpath"."/sim2"); my $source_dirsynth; my $target_dirsynth1 ; opendir(my $indexs, $newpath ) or die "Couldn't read indexs : $!\n"; while (my $file = readdir($indexs)) { if ($file eq "par" ) { if(chdir($newpath."/par")) { my $currentworkingdir = cwd(); $source_dirsynth = $currentworkingdir; $target_dirsynth1 = ("$newpath"."/par2"); } last; } elsif ($file eq "synth") { if(chdir($newpath."/synth")) { my $currentworkingdir = cwd(); $source_dirsynth = $currentworkingdir; $target_dirsynth1 = ("$newpath"."/synth2"); } last; } } closedir($indexs); mkdir($target_dirrtl,0777); mkdir($target_dirsim1,0777); mkdir($target_dirsynth1,0777); my $filertl = dircopy($source_dirrtl, $target_dirrtl); my $filesim = dircopy($source_dirsim, $target_dirsim1); my $filesynth = dircopy($source_dirsynth, $target_dirsynth1); opendir $source_dirsynth: $!"; chdir("$target_dirrtl"); my $m4file = "$projectdir".".m4"; open(MYFILE,">>$m4file"); print MYFILE ("define(`m4_cChipId',`16#$cChipId#,16')\n"); print MYFILE ("define(`m4_cSubVersId',`16#$cSubVersId#,16')\n"); print MYFILE ("define(`m4_$projectdir',`1')\n"); print MYFILE ("define(`m4_$ivariant_name',`1')\n"); print MYFILE ("define(`m4_rtl',`rtl2')\n"); if(my $output1 = `test.bat`){ print("\n succesfully executed\n"); } chdir("$target_dirsynth1"); ###################################################################### +############################################## #added on 13/5/2016 opendir( my $synthdir,$target_dirsynth1) or die "Couldn't enter config + directory: $!\n"; print("\nSYNTHESIS INPUT FILES IN CURRENT DIRECTORY \n"); my $synthfilelist = glob "*.qsf"; print ("\n$synthfilelist \n"); #closedir($target_dirsynth1); my $originalquartus ; my $latestquartus ; my $readlineqo; my $readlineql ; #print "\nENTER THE CONFIGURATION FILE NAME FROM THE ABOVE LIST \n"; #my $configfile = <STDIN>; open (my $OFILE,$synthfilelist) || die "couldnt open file: $!"; while ($readlineqo = <$OFILE>) { chomp $readlineqo; if ($readlineqo =~ /ORIGINAL_QUARTUS_VERSION/) { my @originalqarray = (split / +/ , $readlineqo); $originalquartus = $originalqarray[3]; print "\nthe original quartus $originalquartus\n"; } #next if $readlineqo =~ /^ $/; # } # close($OFILE); #open (my $LFILE,$synthfilelist) || die "couldnt open file: $!"; # while ($readlineql = <$LFILE>) { # chomp $readlineql; print "into the second loop $readlineqo\n"; if ($readlineqo =~ /LAST_QUARTUS_VERSION/) { my @latestqarray = (split / +/ , $readlineqo); $latestquartus = $latestqarray[3]; print "\nthe latest quartus $latestquartus\n"; } next if $readlineqo =~ /^ $/; } close($OFILE); closedir($target_dirsynth1); ###################################################################### +############################################## print("\n Enter y or n to continue with synthesis \n"); my $choice = <STDIN>; chop($choice); if ($choice eq "y") { system ("qcmd.bat $toplevelentity"); } else { print "continue manually with synthesis\n"; }
        block that is causing problem is inside #line with date of today. the whole code is running and even the last print statement also comes but after n times print of "into the second loop..".