Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have an issue with regex and getting the whole substrings

Status
Not open for further replies.

skatun

Programmer
Oct 16, 2008
5
DE
I am trying to get out the filenames I haved defined in my $string, but it only prints :
so the first printed line should look like:

cfx5pre -batch test.cse

any help would be great

Code:
$string = "pre:test.cse;solve:test.def,test.ccl,test.res,test;post:test.cse";

@array = split /;/ , $string;

foreach (@array) {


 if (/pre:/) {

     print "cfx5pre - batch $1 \n" if /(,|:).*cse/i;

   }

 if (/solve:/) {

   print  "cfx5solve - batch $1 " if /(,|:).*def/i;

   print  "-interpolate-iv $1 " if /(,|:).*res/i;;

   print  "-name $1 " if /(,|:).*(;|,)/i;

   print  "-ccl $1 " if /(,|:).*.ccl/i;;

   print "\n";

 }


 if (/post:/) {

   print "cfx5post - batch $1 " if /(,|:).*cse/i;;

 }

}
 
Code:
for(@array){
  if(/^pre:/){
    print"cfx5pre - batch $1\n"if/(\w*\.cse)\b/i;
  }elsif(/^post:/){
    print"cfx5post - batch $1\n"if/(\w*\.cse)\b/i;
  }elsif(/^solve:/){
    print"cfx5solve - batch $1 "if/(\w*\.def)\b/i;
    print"-interpolate-iv $1 "if/(\w*\.res)\b/i;;
    print"-name $1 "if/[,:](\w+?)(,|$)/i;
    print"-ccl $1 "if/(\w*\.ccl)\b/i;;
    print"\n";
  }
}

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
not sure what's wrong with your script.. but how about instead of all those regexp you do something like
Code:
$string = "pre:test.cse;solve:test.def,test.ccl,test.res,test;post:test.cse";

@array = split /;/ , $string;
foreach (@array) {
 @tmp = split /\:/, $_;
 $hash{$tmp[0]} = $tmp[1];
}

for my $key (keys %hash) {
 if ($key eq pre) {
  print "cfx5pre - $hash{$key}\n";
 }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
hes doing it again!

i;; <---

[wink]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I;; don't;; think;; he;; sees;; anything;; wrong;; with;; a;; few;; extra;; ;;;;;;;'s :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I am running some large calculations in fluiddynamics which can be executed with cfx5pre, cfx5solve and cfxpost commands with appropiate attributes.

i would like to run several calculations after each other. I was thinking about adding lines to a textfile with the commands. jobs.txt

then i wan't my script to loop forever and always check if there are some lines in the textfile. If there are then it excecutes those lines write a log and remove the line from the file. I started writing some code.. I have a problem getting the evaluateReply routine to work properly, writing both the command that was executed and the error/succsefullmessage. The Command does not show up in the files.

Also I am not sure how I should do the reading and deleting of lines in the jobs.txt file. Syncronisation. How do I read just one line and then execute this line, remove this line from the file and then go to the next line. (Maybe I reordered the lines while the first line was executed). Do i add a sleep function if there are no lines in the file, or can event be added, as soon as the file is modificated then it starts executing lines again?

All help is welcome

Cheers Kim


Code:
#####################################################
### MAIN ############################################
#####################################################


$filename      = "jobs.txt";
$filenameError = "error.txt";
$filenameDone  = "completed.txt";

open ( FILE, $filename) or die "Cannot open file: $!";
open ( LOGFILE, ">>$filenameError") or die "Cannot open file: $!";
open ( SUCCESSFULLFILE, ">>$filenameDone") or die "Cannot open file: $!";


@lines = <FILE> ;

close FILE;


# @ lines contains strings like these, "pre:testPre.cse;solve:testSolve.def,testSolve.ccl,testSolve.res,test;post:testPost.cse";
foreach(@lines){
  &executeLine($_);
  splice(@lines,0,1);
}
#open ( OUTFILE, ">$filename" );
#print ( OUTFILE @lines );
#close ( OUTFILE );
close ( LOGFILE );
close ( SUCCESSFULLFILE );






sub executeLine(){
  my $line = shift;
  #print $line;

  foreach(&decodeString($line)){
  print "$_\n";
  @evaluateReply = `$_ 2>&1`;
  &evaluateReply(@evaluateReply,$_);
  }

}

sub decodeString{
  my $textString = shift;   # parameters


  ################################################
  my @outCmd;

  @array = split /;/ ,$textString;



 for(@array){
   if(/^pre:/){
     $temp = "cfx5pre - batch $1"if/(\w*\.cse)\b/i;
     push (@outCmd,$temp);
   }
   elsif(/^solve:/){

     $temp = "cfx5solve - batch $1 "if/(\w*\.def)\b/i;
     $temp = $temp." -interpolate-iv $1 "if/(\w*\.res)\b/i;
     $temp = $temp." -name $1 "if/[,:](\w+?)(,|$)/i;
     $temp = $temp." -ccl $1 "if/(\w*\.ccl)\b/i;

     push (@outCmd, $temp);

   }

   elsif(/^post:/){
     $temp = "cfx5post - batch $1"if/(\w*\.cse)\b/i;
     push (@outCmd,$temp);
   }
 }
   return  @outCmd;
}




 # would also like to write which command that was executed
sub evaluateReply(\@$)  {
     my  (@cmd_output,$cmd) = @_;


     $outputString = join '\n', @cmd_output   ;   # maybe this is not neccesary????
     if ($outputString =~/error/ig) {
       print LOGFILE "Error in the command: $cmd \n";
       # Print the echo from the command
     foreach (@cmd_output) {
       print LOGFILE "$_\n";
     }
       $temp = 0;
     }
     else {
       print SUCCESSFULLFILE "Successfull in the command: $cmd \n";
       # Print the echo from the command
     foreach (@cmd_output) {
       print SUCCESSFULLFILE "$_\n";
     }
       $temp = 1;
     }

     # Print the echo from the command
     foreach (@cmd_output) {
       print  "$_\n";
     }
     return $temp;
 }
 
my first code does not work when I use absolute file names:

Code:
$filename      = "c:/Perl/jobs.txt";
$filename2      = "c:/Perl/batch.txt";

my @outlines;

open ( FILE, $filename) or die print "help";

@lines = <FILE> ;

close FILE;

# @ lines contains strings like these, "pre:testPre.cse;solve:testSolve.def,testSolve.ccl,testSolve.res,test;post:testPost.cse";
 foreach(@lines){
    push (@outLines,decodeString($_));
 }


open ( OUTFILE, ">$filename2" );
 print ( OUTFILE @outLines );
 close ( OUTFILE );



sub decodeString{
  my $textString = shift;   # parameters


  ################################################
  my @outCmd;

  @array = split /;/ ,$textString;



 for(@array){
   if(/^pre:/){
     $temp = "cfx5pre - batch $1"if/(\w*\.cse)\b/i;
     push (@outCmd,"$temp\n");
   }
   elsif(/^solve:/){

     $temp = "cfx5solve - batch $1 "if/(\w*\.def)\b/i;
     $temp = $temp." -interpolate-iv $1 "if/(\w*\.res)\b/i;
     $temp = $temp." -name $1 "if/[,:](\w+?)(,|$)/i;
     $temp = $temp." -ccl $1 "if/(\w*\.ccl)\b/i;

     push (@outCmd, "$temp\n");

   }

   elsif(/^post:/){
     $temp = "cfx5post - batch $1"if/(\w*\.cse)\b/i;
     push (@outCmd,"$temp\n");
   }
 }
   return  @outCmd;
}

with this input file:

solve:c:\CFX\DefFiles\330399_0018.def
solve:c:\CFX\DefFiles\330399_0019.def
solve:c:\CFX\DefFiles\330399_0020.def
solve:c:\CFX\DefFiles\330399_0021.def

and it produces this output:
cfx5solve - batch 330399_0018.def
cfx5solve - batch 330399_0019.def
cfx5solve - batch 330399_0020.def
cfx5solve - batch 330399_0021.def

when I wanted it to specify full filenames in the output

Thanks


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top