On Oct 17, Aaron Petry said:
>I have 2 files. One is an error log created from an import procedure to
>a database, the other is the text file that we were trying to import.
>The error log gives me back the line number that the record was on. I
>need to cross reference this. I'm thinking that this should be no big
>deal. I'll read one file, parse it, and add the record/line # to an
>array. Then, I'll loop through the other file, and when I get to a line
>that matches one in the array, I'll write the value to a text file. The
>problem is that I can't get assign any values to my array. Why won't the
>following work?
You CAN assign them to the array. You're just not checking properly.
You're always printing $records[0] (which is an empty string as you'll see
i a minute).
>my @records="";
You've just made your array hold one element, an empty string. I think
you want
my @records = ();
or just
my @records;
>$llc = 1; ### Set the log line count = 1;
>while(<LOG>){ ### Go through that log file line by line.
> if($llc<10){ ### We're going to skip the first 10 lines here. (Header Info)
$llc isn't needed. We can use the builtin $. variable.
if ($. < 10) { }
> }
> else {
> if($_ =~ /Record#/){ ### Make sure that we're looking for the errors that we
>want to see.
You could leave out the '$_ =~' part.
> @line = split; ### We're gonna split the line.
> @records = (@records,$line[1]); ### Add the second part of this
>line to @records
That is spelled push() in Perl:
push @records, $line[1];
> print "Adding $records[0] to the record list.\n"; ### Just a little diagnostic
>tool.
You're printing the FIRST element of the array -- specifically, that pesky
empty string you put in it to begin with. You probably wanted to say
print "Adding $records[-1] to the record list.\n";
or just
print "Adding $line[1] to the record list.\n";
although using $records[-1] ensures it got in there properly.
> } ### Close the if on line 20.
> } ### Close the if block on line 18 (else on line 19)
>$llc = $llc + 1; ### Increment the line number.
That's spelled
++$llc;
and you don't need it anymore, since we use the $. variable.
>} ### Close that while block up on 17.
>close(LOG); ### Close the log file and free up some resources.
Here's how I'd write it:
my @records;
while (<LOG>) {
next if $. < 10;
push @records, (split)[1] if /Record#/;
}
If you want to draw it out a bit, that's the same as:
my @records;
while (<LOG>) {
if ($. < 10) { next; }
if (/Record#/) {
my @parts = split;
push @records, $parts[1];
# any debugging stuff here
}
}
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]