Bobby wrote:
Could someone please tell me what's wrong with my use of the
substr function below? I keep on getting "use of uninitialized
value in substr". Thanks.
$newpsc = substr($PSC,0,$count);
Here's the complete code:
#!/usr/bin/perl
use strict;
use warnings;
my $exclude_psc = 'exclude_psc.txt';
my $current_base = 'base.txt';
my $output = 'new_excludepsc.txt';
my %ex_psc;
my $exkeyword;
open EXCLUDE, '<', $exclude_psc or die "Could not open
'$exclude_psc' $!";
while ( <EXCLUDE> ) {
next if $. == 1; # exclude header
chomp;
my ($excpsc,$keyword) = split /\|/;
%ex_psc = (exclpsc=>$excpsc,exkeyword =>$keyword );
You are assigning to the hash so it will only contain data from the last
record in the 'exclude_psc.txt' file. For more efficiency use
File::ReadBackwards and only read one record.
}
close EXCLUDE;
open BASE, '<', $current_base or die "Could not open '$current_base'
$!";
open OUT, '>', $output or die "Could not open '$output' $!";
while (<BASE>) {
my ($base_no, $name, $description, $PSC) = split /\|/;
my $newpsc;
my $wordlength;
my $expsc=$ex_psc{exclpsc};
my $count = $ex_psc{exclpsc}=~ s/([a-z])/$1/gi;
You can do that more efficiently with tr///:
my $count = $ex_psc{ exclpsc } =~ tr/a-zA-Z//;
But since you are not (it would seem) changing the value of $ex_psc{
exclpsc } inside the loop why then are you calculating $count inside the
loop?
if ($count eq 4){
You are using a string comparison operator on a number, better to use a
numerical comparison operator:
if ( $count == 4 ) {
$newpsc = substr($PSC,0,$count);
Since 0 is a literal that means that you are getting the warning because
either $PSC or $count has the value undef instead of a valid value.
if (uc($ex_psc{exclpsc}) eq uc($newpsc)){
$_= join '|',$base_no,$ex_psc{exclpsc},$ex_psc{exkeyword};
print OUT;
}
}
}
close(OUT);
__END__
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/