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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code not working, but it should! 3

Status
Not open for further replies.

netman4u

Technical User
Mar 16, 2005
176
US
Hello all,

Below is a snippet of code I am working on.

Code:
use strict;
open (PREFIX, "/home/mt71124/scripts/tws_prefix.txt") or die ("Error opening prefix file: $?");
my ($cpu, $sched_name, $job_name ) = @ARGV;
my $count = scalar(@ARGV);
chomp ($cpu,$sched_name,$job_name);
my $prefix;
print "Sched = $sched_name\n";
while (<PREFIX>) {
	chomp;
	next unless ($sched_name =~ /$_/);
	$prefix = $_;
	last;
}
print "prefix = $prefix\n";
close PREFIX;

As you can see it is taking three variables from the command line, one being $sched_name and then looping through a test file to match a portion of the $sched_name with each entry in the prefix file. The problem is the $prefix is never getting set. I know for certain that I input EWSRDBUD on the comand line that ends up being the $sched_name, it should match the prefix in the file I am looking for which is EW, but it does not.

Seems that my:

next unless ($sched_name =~ /$_/);

is not matching but it should when EWSRBUD is compared to EW as EWSRBUD does contain EW. I tried anchoring the expression with:

next unless ($sched_name =~ /^$_/);

but no joy!

OUTPUT:

Code:
# ./maestro_inst.pl PRDAI085 EWSRDBUD EWDBABKUP
Sched = EWSRDBUD
prefix =

Any help is appriciated.

Nick


If at first you don't succeed, don't try skydiving.
 
BTW, here is the relevant portion of the PREFIX file:

Code:
DSP   
E00   
E81   
EAD   
EBI   
EBU   
ECA   
ECB   
ECE   
ECL   
ECR   
ECS   
ED    
EF    
EIS   
ELC   
ELS   
EPA   
ET    
ETA   
ETB   
ETC   
ETD   
ETE   
ETJ   
ETO   
ETS   
ETW   
EW    
FCA

If at first you don't succeed, don't try skydiving.
 
Code:
next unless ($_ =~ /$sched_name/);

or you might want to anchor that to the beginning of the string if that is where you want the match to start from:

Code:
next unless ($_ =~ /^$sched_name/);

which can be written shorthand:

Code:
next unless (/^$sched_name/);


Obviously you are not going to find EWSRDBUD in EWS but you will find EWS in EWSRDBUD. ;)



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
When I copy-and-paste your data file, the "EW " line has got four space characters after it. These are in the match that's being attempted. Since $sched_name doesn't have spaces after the prefix, there's no match.
 
ack! I must have had a brain fart, ignore my dumb post above.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Actually, Kevin got it backwards in his first post. The OP is searching for $_ (a prefix stored in the file) inside the $sched_name variable.

Plus, it's trailing spaces in the data file that appear to be the problem. There aren't any leading spaces in the data posted.
 
Your right ishnid. I had it right the first time. The way I read it:

next unless EWSRDBUD contains EW; #correct

next unless EW contains EWSRDBUD; # does not work

Thanks for all the help all! The trailing spaces were the problem:

Code:
use strict;
open (PREFIX, "/home/mt71124/scripts/tws_prefix.txt") or die ("Error opening prefix file: $1");
my ($cpu, $sched_name, $job_name ) = @ARGV;
my $count = scalar(@ARGV);
print "count=$count\n";
chomp ($cpu,$sched_name,$job_name);
my $prefix;
print "Sched = $sched_name\n";
while (<PREFIX>) {
	chomp;
	s/^ *//; # remove leading spaces
	s/ *$//; # remove trailing spaces
	#next unless (/^$sched_name/);
	next unless ($sched_name =~ /^$_/);
	$prefix = $_;
	last;
}
print "prefix = $prefix\n";
close PREFIX;


If at first you don't succeed, don't try skydiving.
 
When you are removing spaces in your regexes, you might find it better to use the shorthand \s instead of an actual space character, as this will match any kind of whitespace including tabs.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top