Tim McGeary wrote:
>
> Greetings,
Hello,
> I am a new subscriber and I do know a little perl, but I am trying to
> pick up a lot more very quickly. I am having an issue with a conditional
> checking a regular expression
> that doesn't make sense and every book or online resource I have found
> shows the same syntax I have.
>
> Script background:
> I have one file of 4 letter codes (e.g. PART, PBIO, PENG), one code per
> line. I read this file into an array.
>
> I have another file of pipe delimited data, in which one of these fields
> may include a code from the previous file or - for NULL. This file is
> too large to read in all at once, so I am reading it in line by line.
> The problem I am having is with this section:
You should have warnings and strict enabled while developing your
program to help eliminate obvious mistakes.
use warnings;
use strict;
> while (<SIRSI>) {
> $temp = $_;
while ( my $temp = <SIRSI> ) {
> $counter = $counter + 1;
> print "$counter \n";
if you just want the current line number of the file you could use the
$. variable.
print "$.\n";
> foreach $code (@fundcodes) {
foreach my $code (@fundcodes) {
> if ($temp =~ /$code/) {
> print "$temp \n";
> push(@fund_array, $temp);
> last;
> }
> }
> }
>
> @fundcodes is the array of codes read in from the first file
Did you chomp() the values of @fundcodes when you read them in?
> $counter is just for my debugging purposes.
>
> The problem is that the code above thinks every line has $code in it.
> But when I hard-code PBIO instead of $code in the if conditional, I only
> get those files with PBIO.
>
> Now, a secondary problem, which I also haven't figured out is how do I
> get only those lines that have a code between two pipes and not lines
> that also happen to have a code that can be used in a word (e.g. PART or
> PACT)?
Put the pipes in the regular expression:
if ( $temp =~ /\|$code\|/ ) {
> Is there an easier way for me to do this?
Is the fundcode always in the same field?
You could do something like this:
# setup fundcode hash
my %fundcodes;
@fundcodes{ @fundcodes } = ();
while ( my $temp = <SIRSI> ) {
print "$.\n";
chomp;
for my $field ( split /\|/, $temp ) {
if ( exists $fundcodes{ $field } ) {
print "$temp\n";
push @fund_array, $temp;
last;
}
}
}
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]